pyVision
A Machine Learning and Signal Processing toolbox

Download .zip Download.tar.gz View on GitHub


Apache Avro RPC Framework using Netty Server

Apache Avro is more than a data serialization framework. It provides support for RPC Mechanisms

Avro protocols describe RPC interfaces. Like schemas, they are defined with JSON text.

Let us consider example of UserLoginRPC

{
   "namespace": "com.pyvision",
   "protocol": "loginRPC",
   "version" : "1.6.2",
   "types": [
       {   "name": "userLoginRequest","type": "record",
            "fields": [
            {"name": "name", "type": "string"},
            {"name": "password", "type": "string"},
            {"name": "devicetoken", "type": "string"}
            ]
       },
       {
            "name": "userLoginResponse","type": "record",   
            "fields": [
               {"name": "name", "type": "string"},      
               {"name": "loginToken", "type": "string"}
            ]
       }       
   ],
 "messages": {
     "send": {
         "request": [{"name": "request", "type": "userLoginRequest"}],
         "response": "userLoginResponse"
     } 
 }
}

  • protocol - the name of the RPC
  • types - list of schema definitions of named types
  • messages - keys are message name and value is the message object

The message object contains the attribute.

  • request - containing the input to the RPC
  • response - contains the output to the RPC

In the above example the message name is send

Generating the java files

To generate the java files from the above schema files run the command

java -jar avro-tools-1.8.1.jar compile protocol Login.json .

This will generate the java files


com/pyvision/loginRPC.java
com/pyvision/userLoginResponse.java
com/pyvision/userLoginRequest.java

This contains the schema definitions for the input request and output response and also the RPC file.

Using the java File

Netty is a NIO client server framework which enables quick and easy development of network applications such as protocol servers and clients.

Avro provides support for using Netty framework to build networked applications

The Avro Netty server listens on a port for events sent over by Avro RPC.Using Netty we need to configure a port for each type of RPC.

The code to create the server is

private static void createServer() throws IOException {
        server = new NettyServer(new SpecificResponder(loginRPC.class, new loginRPCImpl()), new InetSocketAddress(65111));
        }
				

The code to create a client


private static void connectClient() throws IOException
{
              client = new NettyTransceiver(new InetSocketAddress(65111));
}

The code for creating RPC handler


public static class loginRPCImpl implements loginRPC {
    public userLoginResponse send(userLoginRequest request) throws AvroRemoteException {
      System.out.println("received RPC");
      userLoginResponse r = new userLoginResponse();
      r.setName(request.getName());
      r.setLoginToken("1234");
      return r;
    }

  }

The above code describes thesend RPC function. The loginRPC can contain many RPC functions defined in the protocol file.

The code for calling the RPC is as follows

private static void sendRPC() throws AvroRemoteException, IOException
        {
            loginRPC proxy = (loginRPC) SpecificRequestor.getClient(loginRPC.class, client);
            System.out.println("AA"+" sending the rpc");
            userLoginRequest m=new userLoginRequest();
            m.setName("pi");
            m.setPassword("123");
            m.setDevicetoken("123");
            proxy.send(m);
            
        }
				

The above code should enable you to call the server RPC from the client and get a suitable response back.

Multiple client threads are not supported by Netty.However they are supported by Jetty/HTTP mechanism You cannot create two threads to call the server in the client application. They will cause issue while calling the server.

In the later articles we will look at using the Avro RPC with Jetty framework that supports multiple threads

This structure enables us to create login service as a microservice and expose the functionality as a RPC which can be called by other frontend or backend componenets.

RPC also send the data in encoded binary format but not encrypted.We will also look at methods to provide encryption security over RPC.

References

  • http://avro.apache.org/docs/current/spec.html#Protocol+Declaration
blog comments powered by Disqus