gRPC java 客戶端,服務器端通信使用json格式

使用 protobuf 做爲通信內容序列化的簡單例子請看:http://www.cnblogs.com/ghj1976/p/5458176.htmlhtml

本文是使用 json 作爲內容序列化的簡單例子。java

新建例子項目,從 proto 文件產生 通信包的方式跟以前的徹底同樣。git

本文的源碼在:github

https://github.com/grpc/grpc-java/tree/master/examples/src/main/java/io/grpc/examples/helloworld 這裏的 json

HelloJsonServer.java 和  HelloJsonClient.java 這兩個文件中。服務器

 

這個文件跟 protobuf 處理的文件不一樣的地方以下:網絡

定義一個JSON解析的Stub

整個類的定義文件以下:async

package com.ghj1976;ide

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<HelloWorldJSONStub>
        implements io.grpc.examples.helloworld.GreeterGrpc.GreeterBlockingClient {

    static final MethodDescriptor<HelloRequest, HelloReply> METHOD_SAY_HELLO =
            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);
    }
}
具體的解析用的 ProtoUtils.jsonMarshaller() 這個函數。

 

服務器端的修改

服務器端代碼封裝個函數, bindService, 用於服務器的數據解析分層。

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<HelloRequest,HelloReply>(){
                            @Override
                            public void invoke(HelloRequest request,StreamObserver<HelloReply> responseObserver){
                                serviceImpl.sayHello(request,responseObserver);
                            }
                        }
                    ))
            .build();
}
這裏用到了咱們前面定義的方法

com.ghj1976.HelloWorldJSONStub.METHOD_SAY_HELLO,

增長服務時用這個 bindService 作封裝。

image

 

服務端的代碼改造就這些。

 

 

客戶端的代碼改造

只須要修改 Stub 爲咱們剛剛創建的 HelloWorldJSONStub  接口。

image

 

執行方法跟以前徹底同樣, 啓動 main 方法便可。

 

使用 Wireshark 監聽網絡請求,能夠看到這時候發送的數據包:

客戶端請求的數據包:

image

服務器端返回的包:

image

使用 protobuf 時,則會是以下的截圖:

image

image

相關文章
相關標籤/搜索