环境配置
JDK7及其以上
获取实例代码
$ git clone -b v1.42.1 https://github.com/grpc/grpc-java
进入examples文件夹
cd grpc-java/examples
启动实例代码
1.编译客户端实例代码和服务端实例代码
$ ./gradlew installDist
2.启动服务端
$ ./build/install/examples/bin/hello-world-server
INFO: Server started, listening on 50051
3.开启另外一个终端窗口,启动客户端
$ ./build/install/examples/bin/hello-world-client
INFO: Will try to greet world ...
INFO: Greeting: Hello world
输出上面的提示信息,就说明已经运行成功了。
第一个gRPC服务
接下来,我们以官方案例的为基础,尝试着写一个简单的服务。gRPC的服务需要先在protocol buffers 里定义。在上面的例子中,服务端和客户端都有个sayHello() RPC方法,服务端从客户端里接受了一个HelloRequest消息后,返回了HelloReply消息。这个SayHello()的定义如下:
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
我们打开src/main/proto/helloworld.proto 文件,添加一个新的方法SayHelloAgain()。
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
// Sends another greeting
rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
(IDEA 只需打开example目录即可)
然后点编译,gRPC会重写GreeterGrpc.java,找到这个类
会发现它多出了我们新写的方法。
更新服务端和客户端代码
打开 src/main/java/io/grpc/examples/helloworld/HelloWorldServer.java 增加新的方法的调用。
static class GreeterImpl extends GreeterGrpc.GreeterImplBase {
@Override
public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + req.getName()).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}
public void sayHelloAgain(HelloRequest req,StreamObserver<HelloReply> responseObserver){
HelloReply reply = HelloReply.newBuilder().setMessage("Hello again " + req.getName()).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}
}
打开src/main/java/io/grpc/examples/helloworld/HelloWorldClient.java,在greet方法中新增sayHelloAgain()的调用
public void greet(String name) {
logger.info("Will try to greet " + name + " ...");
HelloRequest request = HelloRequest.newBuilder().setName(name).build();
HelloReply response;
try {
response = blockingStub.sayHello(request);
} catch (StatusRuntimeException e) {
logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
return;
}
logger.info("Greeting: " + response.getMessage());
try {
response = blockingStub.sayHelloAgain(request);
} catch (StatusRuntimeException e) {
logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
return;
}
logger.info("Greeting: " + response.getMessage());
}
重新启动项目
./gradlew installDist
启动服务端
$ ./build/install/examples/bin/hello-world-server
INFO: Server started, listening on 50051
打开一个新的终端,启动客户端
$ ./build/install/examples/bin/hello-world-client
INFO: Will try to greet world ...
INFO: Greeting: Hello world
INFO: Greeting: Hello again world
输出了新的提示
INFO: Greeting: Hello again world
这个例子就完成了。