JSON RPC standard
- it is a standard communication protocol between client and server.
- The communication can be bidirectional, it means that both client and server can send/receive messages from the opposite part
- the request is sent to a server that implements the protocol
- JSON-RPC is used to implement the LSP (Language Server Protocol)
- compilers can expose their services implementing interfaces provided by the Language Server
- tools can consume new services by connecting to the server.
- The communication is defined by JSON messages sharing that have a fixed structure:
{
  "jsonrpc": "2.0",
  "id":0,
  "method":"sayHelloWorld",
  "params":["UserFirstName UserLastName"]
}
- method is the string with the name of the method to be invoked.
- params is an Object or Array of values to be passed as parameters to the defined method.
- id is a value of any type used to match the response with the request that it is replying to.
And this could be a typical response from the server:
content-type: application/json-rpc
content-length: 60
date: Mon, 25 Jun 2018 08:20:15 GMT
{
"jsonrpc": "2.0",
"id": 0,
"result": "Hello world, UserFirstName UserLastName"
}
Underhood implementation
RPC implementation in Java
Jsonrpc4j is the most easy/important implementation of the JSON-RPC procedures for Java language. A good tutorial on how simply turn on the lights is available here
Deploy on docker container
- Perform a build to create the jar file
  bash mvn package
- Create a Docker file with instruction on how spawn the image with the jar inside of it:
FROM java:8
WORKDIR /
ADD bootrpc-0.1.0.jar bootrpc-0.1.0.jar
EXPOSE 8080
CMD java -jar bootrpc-0.1.0.jar
- Spawn the image with the command:
docker build . -f Dockerfile -t bootrpc
- Run the container:
docker run bootrpc
- Verify that the container is up:
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
9dead33f5efa        bootrpc                "/bin/sh -c 'java -j…"   2 hours ago         Up 2 hours          8080/tcp            jovial_euler
- Perform a Curl to verify that everything works as expected:
curl -H "Content-Type:application/json" -d '{"id":0, "method":"sayHelloWorld", "params":["Andera Doe"]}' http://172.17.0.2:8080/rpc/myservice
- If everything works fine we can receive a response with '200 ok' http response code with a JSON in the response body:
{"jsonrpc": "2.0", "result": "Hello visitor!", "id": 3}