grpc 工具
BloomRPC
GRPC - 使用
protobuf
定义protobuf,并将protobuf文件,通过java的plugin打包生成java-grpc相关文件。参照:grpc-protobuf
mvn依赖
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
<version>3.21.7</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>1.30.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.30.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.30.0</version>
</dependency>
grpc-server:
// Configuration 定义
@Bean
public GrpcServer grpcServer(@Value("${gprc.port}") Integer grpcPort)){
GrpcServer grpc = new GrpcServer(grpcPort);
grpc.init();
}
/**
* Grpc interface
*/
public class TestGrpcServer extends TestServiceGrpc.TestServiceImplBase{
@Override
public void searchUser(TestInfo request, StreamObserver<TestResult> responseObserver) {
TestResult result = TestResult.newBuilder().setCode("123").setMsg("testOK").build();
log.info(String.valueOf(request));
responseObserver.onNext(result);
responseObserver.onCompleted();
}
}
/**
* grpc server
*/
public class GrpcServer {
private int grpcPort;
public GrpcServer(Integer grpcPort){
this.grpcPort = grpcPort;
init();
}
public void init() throws Exception {
ServerBuilder.forPort(grpcPort)
.addService(new TestGrpcServer())
.build()
.start();
}
}
grpc-client:
@Bean
public GrpcClient grpcClient(@Value("${grpc.ip}") String grpcIp, @Value("${grpc.port}")Integer grpcPort){
return new GrpcClient(grpcIp, grpcPort);
}
public class GrpcClient {
private Channel channel = null;
public GrpcClient(String grpcIp, Integer grpcPort){
this.grpcIp = grpcIp;
this.grpcPort = grpcPort;
channel = channel();
}
public TestResult run(TestInfo testInfo) {
TestServiceGrpc.TestBlockingStub serviceStub = TestServiceGrpc.newBlockingStub(channel);
TestResult testResult = serviceStub.searchUser(testInfo);
return testResult;
}
/**
* Channel
* @return
*/
private Channel channel() {
return ManagedChannelBuilder.forAddress(grpcIp,grpcPort).usePlaintext().build();
}
}
Test试用
@RestController
@RequestMapping("/grpc")
public class TestGrpcController {
@Autowired
GrpcClient grpcClient;
@RequestMapping("/test")
public String grpcTest(){
TestInfo testInfo = TestInfo.newBuilder().setCode("AAAA").build();
TestResult testResult = grpcClient.run(testInfo);
String json = null;
try {
json = JsonFormat.printer().print(testResult);
//JsonFormat.parser().merge(json, TestInfo.newBuilder());
} catch (InvalidProtocolBufferException e) {
e.printStackTrace();
}
return json;
}
异常
1. io.grpc.netty.shaded.io.netty.handler.ssl.NotSslRecordException: not an SSL/TLS record:
因client端采用TLS访问,而服务端默认是PlainText返回信息。
2. Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class com.google.protobuf.UnknownFieldSet$Parser]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class com.google.protobuf.UnknownFieldSet$Parser and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.yumchina.share.app.auth.hub.test.proto.AuthResult["unknownFields"]->com.google.protobuf.UnknownFieldSet["parserForType"])
因grpc产生的对象,不具备set方法,因此无法直接jackson序列化返回。需要引入protobuf-java-util工具类。
3. 指定method没有找到
因client端和server端使用的package路径不一致导致,要定义统一的protobuf的package。
4.NettyChannelProvider class is not found
多个使用的grpc的jar包冲突导致,如果无法排除对方的NettyChannelProvider,则直接改用最基本的grpc-netty的maven依赖jar。
即:grpc-netty-shaded -》 grpc-netty。 因grpc-netty下NettyChannelProvider在io.grpc.netty下,具有最priority。