使用ProtocolBuffer实现RPC简单示例03

接第01部分,本节用来说明Java语言的代码实现。

使用Protoc.exe生成java代码之后,会生成两个java文件,Client与Server都需要包含这两个文件。

首先是Server端:
1、新建一个java项目,引用以下jar包
protobuf-java-3.0.0-beta-2.jar
grpc-all-0.13.1.jar
guava-18.0.jar
netty-all-4.1.0.CR1.jar
okhttp-2.5.0.jar
okio-1.6.0.jar

2、项目中添加生成的两个java文件。

3、新建一个类MyGRPCServer,实现JustATestGrpc.JustATest接口

package com.neohope.protobuf.grpc.test;

import io.grpc.stub.StreamObserver;

public class MyGRPCServer implements JustATestGrpc.JustATest{

    @Override
    public void add(JustATestOuterClass.AddRequest request, StreamObserver<JustATestOuterClass.AddResponse> responseObserver) {
        JustATestOuterClass.AddResponse rsp =  JustATestOuterClass.AddResponse.newBuilder().setC(request.getA() + request.getB()).build();

        responseObserver.onNext(rsp);
        responseObserver.onCompleted();
    }

    @Override
    public void sayHelloTo(JustATestOuterClass.Person request, StreamObserver<JustATestOuterClass.HelloResponse> responseObserver) {
        JustATestOuterClass.HelloResponse rsp =  JustATestOuterClass.HelloResponse.newBuilder().setRsp("Hello "+ request.getName()).build();

        responseObserver.onNext(rsp);
        responseObserver.onCompleted();
    }
}

4、修改TestServer.java

package com.neohope.protobuf.grpc.test;

import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.netty.NettyServerBuilder;

import java.io.IOException;
import java.util.Scanner;

public class TestServer {
    public static void main(String[] args) throws IOException {
        Server server = NettyServerBuilder.forPort(1900)
                .addService(JustATestGrpc.bindService(new MyGRPCServer()))
                .build()
                .start();

        Scanner scanner =new Scanner(System.in);
        scanner.nextLine();
    }
}

5、编译运行
然后是Client端:
1、新建一个java项目,引用以下jar包
protobuf-java-3.0.0-beta-2.jar
grpc-all-0.13.1.jar
guava-18.0.jar
netty-all-4.1.0.CR1.jar
okhttp-2.5.0.jar
okio-1.6.0.jar

2、项目中添加生成的两个java文件。

3、修改TestClient.java

package com.neohope.protobuf.grpc.test;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;

public class TestClient {

    public static void main(String[] args)
    {
        ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 1900)
                .usePlaintext(true)
                .build();

        JustATestGrpc.JustATestBlockingStub blockingStub = JustATestGrpc.newBlockingStub(channel);

        JustATestOuterClass.AddRequest areq = JustATestOuterClass.AddRequest.newBuilder().setA(1).setB(2).build();
        JustATestOuterClass.AddResponse arsp = blockingStub.add(areq);
        System.out.println(arsp.getC());

        JustATestOuterClass.Person preq = JustATestOuterClass.Person.newBuilder().setAge(30).setName("neohope").setSex(JustATestOuterClass.Person.SexType.MALE).build();
        JustATestOuterClass.HelloResponse prsp = blockingStub.sayHelloTo(preq);
        System.out.println(prsp.getRsp());

        channel.shutdown();
    }
}

4、编译运行

Comments are closed.