GRPC Java client, server-side communication using JSON format

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.