依赖

  1. <!--grpc和spring-boot集成框架,包括grpc-server和grpc-client -->
  2. <dependency>
  3. <groupId>net.devh</groupId>
  4. <artifactId>grpc-spring-boot-starter</artifactId>
  5. </dependency>

服务端

配置项

  1. grpc:
  2. server:
  3. port: 0
  4. address:

接口实现类与服务器进行绑定,注解@GrpcService
image.png

客户端

配置项

  1. grpc:
  2. server:
  3. port: 0
  4. client:
  5. #这个名称很重要
  6. userservice:
  7. #address: static://localhost:9999
  8. enable-keep-alive: true
  9. keep-alive-without-calls: true
  10. negotiation-type: plaintext

获取stub,注解@GrpcClient,参数要对应配置
image.png
三种类型的stub
image.png
**newStub**:Creates a new async stub that supports all call types for the service
**newBlockingStub**: Creates a new blocking-style stub that supports unary and streaming output calls on the service(仅支持简单RPC和服务端流式)
**newFutureStub**:Creates a new ListenableFuture-style stub that supports unary calls on the service

优雅
image.png