For a simple example of using PROTOBUF as a serialization of communication content, see: http://www.cnblogs.com/ghj1976/p/5458176.html.
This article is a simple example of using JSON as a serialization of content.
New example project, the way to generate a communication packet from the proto file is exactly the same as before.
The source code in this article is:
Https://github.com/grpc/grpc-java/tree/master/examples/src/main/java/io/grpc/examples/helloworld here.
Hellojsonserver.java and Hellojsonclient.java in these two files.
This file differs from the file processed by Protobuf as follows:
Defines a stub for JSON parsing
The definition file for the entire class is as follows:
Package com.ghj1976;
Import Io.grpc.examples.helloworld.GreeterGrpc;
Import io.grpc.examples.helloworld.HelloReply;
Import Io.grpc.examples.helloworld.HelloRequest;
Import io.grpc.stub.AbstractStub;
Import static Io.grpc.stub.ClientCalls.blockingUnaryCall;
Import io.grpc.CallOptions;
Import Io.grpc.Channel;
Import Io.grpc.MethodDescriptor;
Import Io.grpc.protobuf.ProtoUtils;
/**
* Created by ghj1976 on 16/5/4.
*/
public class Helloworldjsonstub extends abstractstub
Implements Io.grpc.examples.helloworld.GreeterGrpc.GreeterBlockingClient {
static final methoddescriptor methoddescriptor.create (
GreeterGrpc.METHOD_SAY_HELLO.getType (),
GreeterGrpc.METHOD_SAY_HELLO.getFullMethodName (),
Protoutils.jsonmarshaller (Hellorequest.getdefaultinstance ()),
Protoutils.jsonmarshaller ( Helloreply.getdefaultinstance ()));
Protected helloworldjsonstub (Channel channel) {
Super (channel);
}
Protected helloworldjsonstub (channel channel, Calloptions calloptions) {
Super (channel, calloptions);
}
@Override
Protected helloworldjsonstub build (channel channel, Calloptions calloptions) {
return new Helloworldjsonstub (channel, calloptions);
}
@Override
Public helloreply SayHello (Hellorequest request) {
Return Blockingunarycall (
Getchannel (), Method_say_hello, Getcalloptions (), request);
}
}
The Protoutils.jsonmarshaller () function for the specific parsing.
Server-side Modifications
Server-side code encapsulates a function, Bindservice, for the server's data parsing hierarchy.
Private Serverservicedefinition Bindservice (final greetergrpc.greeter Serviceimpl) {
Return Io.grpc.ServerServiceDefinition.builder (Greetergrpc.service_name)
. Addmethod (
Com.ghj1976.HelloWorldJSONStub.METHOD_SAY_HELLO,
Asyncunarycall (
New Servercalls.unarymethod@Override
public void Invoke (Hellorequest request,streamobserverServiceimpl.sayhello (Request,responseobserver);
}
}
))
. build ();
}
Here's the method we defined earlier.
Com.ghj1976.HelloWorldJSONStub.METHOD_SAY_HELLO,
Use this bindservice for encapsulation when adding services.
The service-side code transformation is just that.
Code transformation for the client
Just modify the Stub for the Helloworldjsonstub interface that we just established.
The execution method is exactly the same as before, starting the main method.
Using Wireshark to listen for network requests, you can see the packets sent at this time:
Packets requested by the client:
Server-side returned packages:
When using PROTOBUF, the following will be true:
GRPC Java client, server-side communication using JSON format