maven依赖

  1. <!--jar包依赖-->
  2. <dependencies>
  3. <dependency>
  4. <groupId>io.grpc</groupId>
  5. <artifactId>grpc-all</artifactId>
  6. </dependency>
  7. </dependencies>
  8. <!--protobuf-maven-plugin插件-->
  9. <plugin>
  10. <groupId>org.xolstice.maven.plugins</groupId>
  11. <artifactId>protobuf-maven-plugin</artifactId>
  12. <version>0.6.1</version>
  13. <configuration>
  14. <!--suppress UnresolvedMavenProperty -->
  15. <protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}
  16. </protocArtifact>
  17. <pluginId>grpc-java</pluginId>
  18. <!--suppress UnresolvedMavenProperty -->
  19. <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}
  20. </pluginArtifact>
  21. </configuration>
  22. <executions>
  23. <execution>
  24. <goals>
  25. <goal>compile</goal>
  26. <goal>compile-custom</goal>
  27. </goals>
  28. </execution>
  29. </executions>
  30. </plugin>

proto文件定义

  1. syntax = "proto3"; // 协议版本
  2. // 选项配置
  3. option java_package = "com.example";
  4. option java_outer_classname = "RPCDateServiceApi";
  5. option java_multiple_files = true;
  6. // 定义包名
  7. package com.example;
  8. // 服务接口.定义请求参数和相应结果 会生成RPCDateServiceGrpc接口
  9. service RPCDateService {
  10. rpc getDate (RPCDateRequest) returns (RPCDateResponse) {
  11. }
  12. }
  13. // 定义请求体
  14. message RPCDateRequest {
  15. string userName = 1;
  16. }
  17. // 定义相应内容
  18. message RPCDateResponse {
  19. string serverDate = 1;
  20. }

生成Bean和接口

image.png

image.png

定义接口实现类 (RPCDateServiceGrpc的实现类)

  1. public class RPCDateServiceImpl extends RPCDateServiceGrpc.RPCDateServiceImplBase {
  2. @Override
  3. public void getDate(RPCDateRequest request, StreamObserver<RPCDateResponse> responseObserver) {
  4. // 请求结果,我们定义的
  5. RPCDateResponse rpcDateResponse = null;
  6. String userName = request.getUserName();
  7. String response = String.format("你好: %s, 今天是%s.", userName,
  8. new SimpleDateFormat("yyyy-MM-dd HH:mm:ss: a").format(new Date()));
  9. try {
  10. // 定义响应,是一个builder构造器.
  11. rpcDateResponse = RPCDateResponse
  12. .newBuilder()
  13. .setServerDate(response)
  14. .build();
  15. } catch (Exception e) {
  16. responseObserver.onError(e);
  17. } finally {
  18. // 这种写法是observer,异步写法,老外喜欢用这个框架.
  19. responseObserver.onNext(rpcDateResponse);
  20. }
  21. responseObserver.onCompleted();
  22. }
  23. }

服务端

  1. public class GRPCServer {
  2. private static final int port = 9999;
  3. public static void main(String[] args) throws Exception {
  4. // 设置service接口.
  5. Server server = ServerBuilder.
  6. forPort(port)
  7. .addService(new RPCDateServiceImpl())
  8. .build().start();
  9. System.out.println(String.format("GRpc服务端启动成功, 端口号: %d.", port));
  10. server.awaitTermination();
  11. }
  12. }

客户端

  1. public class GRPCClient {
  2. private static final String host = "127.0.0.1";
  3. private static final int serverPort = 9999;
  4. public static void main(String[] args) throws Exception {
  5. // 1. 拿到一个通信的channel
  6. ManagedChannel managedChannel = ManagedChannelBuilder.forAddress(host, serverPort).usePlaintext().build();
  7. try {
  8. // 2.拿到道理对象
  9. RPCDateServiceGrpc.RPCDateServiceBlockingStub rpcDateService = RPCDateServiceGrpc.newBlockingStub(managedChannel);
  10. for (int i = 0; i < 5; i++) {
  11. RPCDateRequest rpcDateRequest = RPCDateRequest
  12. .newBuilder()
  13. .setUserName("anthony"+i)
  14. .build();
  15. // 3. 请求
  16. RPCDateResponse rpcDateResponse = rpcDateService.getDate(rpcDateRequest);
  17. // 4. 输出结果
  18. System.out.println(rpcDateResponse.getServerDate());
  19. Thread.sleep(2000);
  20. }
  21. } finally {
  22. // 5.关闭channel, 释放资源.
  23. managedChannel.shutdown();
  24. }
  25. }
  26. }

结果

image.pngimage.png