- GRPC快速整合springboot实战
- 一、引入maven依赖包
- 二、相关配置文件
- 三、编写proto文件
- 三、编写服务端代码
- 四、编写客户端
- 五、测试客户端调用服务
gRPC是一个高性能,开放源代码的通用RPC框架。默认情况下,它使用协议缓冲区来定义公开的服务。
该框架提供了双向流等功能,并支持许多不同的编程语言。
gRPC最初由Google开发,现已获得Apache 2.0的许可。
为了展示gRPC的工作原理,我们来构建一个客户端和相应的服务器,以公开一个简单的Hello World gRPC服务。 一、引入maven依赖包
2.x.x.RELEASE 支持 Spring Boot 2.1.x/2.2.x 和 Spring Cloud Greenwich / Hoxton。
最新版本: 2.13.0.RELEASE
( 2.4.0.RELEASE 用于 Spring Boot 2.0.x & Spring Cloud Finchy).
1.x.x.RELEASE 支持 Spring Boot 1 & Spring Cloud Edgware, Dalston, Camden.
最新版本: 1.4.2.RELEASE
注意: 该项目也可以在没有 Spring-Boot 的情况下使用,但是您需要手动配置一些 bean
本次使用的是Spring Boot 2.0.x 版本
二、相关配置文件1.8 1.6.1 0.6.1 net.devh grpc-spring-boot-starter2.4.0.RELEASE kr.motd.maven os-maven-plugin${os-maven-plugin.version} org.springframework.boot spring-boot-maven-pluginorg.projectlombok lombokorg.xolstice.maven.plugins protobuf-maven-plugin${protobuf-maven-plugin.version} com.google.protobuf:protoc:3.5.1-1:exe:${os.detected.classifier} grpc-java io.grpc:protoc-gen-grpc-java:1.16.1:exe:${os.detected.classifier} compile compile-custom
application.yml
server:
port: 8600
grpc:
server:
in-process-name: grpc
# 修改服务端默认入参最大大小,默认值为4M ,这里修改为20M 20*1024*1024
max-inbound-message-size: 20971520
# grpc 端口号
port: 8989
client:
GLOBAL:
negotiation-type: plaintext
# 修改客户端端默认入参最大大小,默认值为4M ,这里修改为20M 20*1024*1024
max-inbound-message-size: 20971520
# 客户端指定连接服务端地址
address: 'static://127.0.0.1:8989'
三、编写proto文件
gRPC服务是使用协议缓冲区定义的。这些是Google的语言无关,平台无关的可扩展机制,用于序列化结构化数据。
通过在.proto文件中定义协议缓冲区消息类型,您可以指定要序列化的信息的结构。每个协议缓冲区消息都是一个小的逻辑信息记录,其中包含一系列名称-值对。
对于此示例,我们定义了一条包含有关MyService的信息的第一条消息,以及一条包含Greeting的第二条消息。然后,这两者都在sayHello() RPC方法中使用,该方法从客户端获取人员消息并从服务器返回问候语。
除了包名称和一个选项(用于为不同的类生成单独的文件)之外,我们还定义了所使用的协议缓冲区语言的版本(proto3)。
有关更多信息,**请点击链接查看proto文件语法指南官方文档(需fq访问) **:协议缓冲区语言指南。
所有proto文件放在 /src/main/proto 文件夹下
MyService.proto
syntax = "proto3";
package net.devh.boot.grpc.example;
option java_multiple_files = true;
option java_package = "net.devh.boot.grpc.examples.lib";
option java_outer_classname = "HelloWorldProto";
// The greeting service definition.
service MyService {
// 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;
}
编写完成proto文件放入到 /src/main/proto 文件夹下 后需要编译 :
三、编写服务端代码import io.grpc.stub.StreamObserver;
import net.devh.boot.grpc.examples.lib.HelloReply;
import net.devh.boot.grpc.examples.lib.HelloRequest;
import net.devh.boot.grpc.examples.lib.MyServiceGrpc;
import net.devh.boot.grpc.server.service.GrpcService;
@GrpcService
public class MyServiceImpl extends MyServiceGrpc.MyServiceImplbase {
@Override
public void sayHello(HelloRequest request, StreamObserver responseObserver) {
HelloReply reply = HelloReply.newBuilder()
.setMessage("Hello ==> " + request.getName())
.build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}
}
四、编写客户端
import net.devh.boot.grpc.client.inject.GrpcClient;
import net.devh.boot.grpc.examples.lib.HelloRequest;
import net.devh.boot.grpc.examples.lib.MyServiceGrpc;
import org.springframework.stereotype.Service;
import java.util.Arrays;
@Service
public class GrpcClientExample {
@GrpcClient("myService")
private MyServiceGrpc.MyServiceBlockingStub myServiceStub;
@GrpcClient("demMultipleQueryGRpcService")
private DemMultipleQueryGRpcServiceGrpc.DemMultipleQueryGRpcServiceBlockingStub demMultipleQueryGRpcServiceBlockingStub;
public String receiveGreeting(String name) {
HelloRequest request = HelloRequest.newBuilder()
.setName(name)
.build();
return myServiceStub.sayHello(request).getMessage();
}
}
五、测试客户端调用服务
import cn.mesmile.grpc.client.GrpcClientExample;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/api/v1/grpc")
@RestController
public class GrpcController {
private final GrpcClientExample grpcClientExample;
public GrpcController(GrpcClientExample grpcClientExample) {
this.grpcClientExample = grpcClientExample;
}
@GetMapping("/get/{name}")
public Object grpc(@PathVariable("name")String name){
StringBuilder stringBuilder = new StringBuilder(name);
for (int i = 0; i < 500000; i++) {
// 模拟入参超过默认 4M 的情况下,需要在配置文件中修改 默认入参大小
stringBuilder.append(RandomUtil.randomNumbers(20));
}
String names = grpcClientExample.receiveGreeting(stringBuilder.toString());
System.out.println("response.getMessage() = " + names);
return names;
}
相关参考资料:
- https://yidongnan.github.io/grpc-spring-boot-starter/zh-CN/
- https://gitee.com/mirrors/grpc-spring-boot-starter/tree/master/examples
- https://blog.csdn.net/suprezheng/article/details/121294291?spm=1001.2014.3001.5501



