一、首先声明gRPC接口
这里引入的是最新的gRpc-core 1.37版本, 采用的grcp-spring-boot-starter封装的版本进行实现,github地址:
https://github.com/yidongnan/grpc-spring-boot-starter
要实现gRpc通讯, 先定义接口以及入参出参信息
syntax = "proto3";
option java_multiple_files = true;
option java_package = "com.grpc.spring.api.order";
option java_outer_classname = "OrderServiceProto";
// The service definition.
service OrderService {
// The service method
rpc GetOrderInfo (GetOrderRequest) returns (GetOrderReply) {
}
}
// The request message
message GetOrderRequest {
string orderNo = 1;
}
// The response message
message GetOrderReply {
string orderNo = 1;
string userName = 2;
string address = 3;
int64 price = 4;
}
定义好之后, 就需要通过protoc工具,生成对应的JAVA代码。
为便于使用, 这里封装了bat脚本,并提供了对应的protoc执行程序。
@echo off
for %%i in (proto/*.proto) do (
d:/TestCode/protoc.exe --plugin=protoc-gen-grpc-java=d:/TestCode/protoc-grpc.exe --java_out=../java --grpc-java_out=../java ./proto/%%i
echo generate %%i to java file successfully!
)
该脚本意思, 会扫描当前proto目录下的所有proto脚本文件, 通过protoc-grpc插件, 在指定目录下生成gRpc通讯接口的JAVA代码。
二、如何在服务端集成gRPC
首先整理配置好POM依赖,这里将共用的依赖抽取到父级POM文件中。
父级POM依赖:
<dependencies>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
dependency>
<dependency>
<groupId>net.devhgroupId>
<artifactId>grpc-spring-boot-starterartifactId>
<version>${gprc.spring.version}version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>${jackson.verson}version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-coreartifactId>
<version>${jackson.verson}version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-annotationsartifactId>
<version>${jackson.verson}version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
<version>1.18.8version>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.grpc.springgroupId>
<artifactId>grpc-spring-apiartifactId>
<version>${grpc.api.version}version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>${spring.boot.version}version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>${spring.boot.version}version>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>2020.0.2version>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>2021.1version>
dependency>
dependencies>
dependencyManagement>
服务端POM配置
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>net.devhgroupId>
<artifactId>grpc-server-spring-boot-starterartifactId>
<version>${gprc.spring.version}version>
dependency>
<dependency>
<groupId>com.grpc.springgroupId>
<artifactId>grpc-spring-apiartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
<version>3.0.2version>
<exclusions>
<exclusion>
<artifactId>servlet-apiartifactId>
<groupId>javax.servletgroupId>
exclusion>
exclusions>
dependency>
dependencies>
作为服务端, 需要加入gRpc服务端的依赖grpc-server-spring-boot-starter, 同时引入eureka, 可以实现微服务之间的调用以及负载。
服务端接口实现
这里为作测试验证, 定义个订单服务接口, GrpcOrderService:
/**
* 订单服务接口实现
*/
@GrpcService
public class GrpcOrderService extends OrderServiceGrpc.OrderServiceImplBase {
@Value("${server.port}")
private String serverPort;
@Override
public void getOrderInfo(GetOrderRequest request, StreamObserver<GetOrderReply> responseObserver) {
GetOrderReply reply =GetOrderReply.newBuilder().setOrderNo(request.getOrderNo())
.setAddress("广东省深圳市,端口号" + serverPort)
.setUserName("tester")
.setPrice(System.currentTimeMillis())
.build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}
}
要实现gRpc服务端接口, 必须做声明, 采用@GrpcService注解, 然后继承gRpc生成的接口OrderServiceGrpc,重新getOrderInfo获取订单信息接口, 实现具体的处理逻辑。
服务端的配置
application.yml
server:
port: 18082
spring:
application:
name: grpc-spring-server
grpc:
server:
port: 0
eureka:
instance:
prefer-ip-address: true
client:
register-with-eureka: true
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname:eureka.kyeapi.com}:${eureka.server.port:18761}/eureka/
服务端的gRpc端口配置为0代表随机分配, gRpc服务信息会注册至eureka。
三、 如何在客户单集成gRPC:
客户端的POM依赖配置
<dependencies>
<dependency>
<groupId>com.grpc.springgroupId>
<artifactId>grpc-spring-apiartifactId>
dependency>
<dependency>
<groupId>net.devhgroupId>
<artifactId>grpc-client-spring-boot-starterartifactId>
<version>${gprc.spring.version}version>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
<version>3.0.2version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
<version>3.0.2version>
<exclusions>
<exclusion>
<artifactId>servlet-apiartifactId>
<groupId>javax.servletgroupId>
exclusion>
exclusions>
dependency>
dependencies>
加入提供的客户端依赖grpc-client-spring-boot-starter,这里注意,添加的eureka-client依赖, 要排除servlet-api, 以免启动冲突。
客户端调用实现
GrpcOrderClientService:
/**
* 订单信息获取客户端接口
*/
@Service
@Log4j2
public class GrpcOrderClientService {
@GrpcClient("grpc-server")
private OrderServiceGrpc.OrderServiceBlockingStub orderServiceBlockingStub;
/**
* 获取订单信息
* @param orderNo
* @return
*/
public String getOrderInfo(String orderNo) {
try {
//将远程调用的Grpc服务端接口信息, 返回给客户端。
GetOrderRequest request = GetOrderRequest.newBuilder().setOrderNo(orderNo).build();
final GetOrderReply response = orderServiceBlockingStub.getOrderInfo(request);
String result = "orderNo: " + response.getOrderNo() + ", userName: " + response.getUserName() + ", address: " + response.getAddress();
return result;
}catch (Exception e) {
log.error(e.getMessage(), e);
return "error! " + e.getMessage();
}
}
}
这里通过@GrpcClient注解, 引入gRpc接口,这里的gRpc服务端名称grpc-server要与配置文件的名称保持一致。
客户端配置
application.yml
server:
port: 18080
spring:
application:
name: grpc-spring-client
grpc:
client:
grpc-server:
address: 'discovery:///grpc-spring-server'
enableKeepAlive: true
keepAliveWithoutCalls: true
negotiationType: plaintext
eureka:
instance:
prefer-ip-address: true
status-page-url-path: /actuator/info
health-check-url-path: /actuator/health
client:
register-with-eureka: false
fetch-registry: true
service-url:
defaultZone: http://${eureka.instance.hostname:eureka.kyeapi.com}:${eureka.server.port:18761}/eureka/
这里定义了一个gRpc的连接信息grpc-server,与前面注解的名称保持一致, adress要配置gRpc服务端的名称, 不需要配置具体的IP和端口,会通过eureka来拉取服务端的具体连接信息。
四、 测试验证
启动服务
启动Eureka服务、gRpc服务端与客户端,为便于测试负载, 这里启动两个服务端实例, 端口号分别为18081与108082。
调用验证
第一调用:
第二次调用:
可以看到, 能够正常调用gRpc服务接口, 同时gRpc也是可以支持负载均衡。
最后, 需要的完整的源码, 可以从以下链接获取:
https://download.csdn.net/download/hxx688/22041124