概述
什么是gRPC
实践案例
小编以java版进行案例展示,其它语言类似,可自行测试
< !-- gRPC配置 -- >
< dependency >
< groupId >io.grpc< /groupId >
< artifactId >grpc-netty-shaded< /artifactId >
< version >1.29.0< /version >
< /dependency >
< dependency >
< groupId >io.grpc< /groupId >
< artifactId >grpc-protobuf< /artifactId >
< version >1.29.0< /version >
< /dependency >
< dependency >
< groupId >io.grpc< /groupId >
< artifactId >grpc-services< /artifactId >
< version >1.29.0< /version >
< /dependency >
< dependency >
< groupId >io.grpc< /groupId >
< artifactId >grpc-stub< /artifactId >
< version >1.29.0< /version >
< /dependency >
< !-- proto插件 -- >
< plugins >
< plugin >
< groupId >org.xolstice.maven.plugins< /groupId >
< artifactId >protobuf-maven-plugin< /artifactId >
< version >0.6.1< /version >
< configuration >
< protocArtifact >com.google.protobuf:protoc:3.11.0:exe:${os.detected.classifier}< /protocArtifact >
< pluginId >grpc-java< /pluginId >
< pluginArtifact >io.grpc:protoc-gen-grpc-java:1.29.0:exe:${os.detected.classifier}< /pluginArtifact >
< /configuration >
< executions >
< execution >
< goals >
< goal >compile< /goal >
< goal >compile-custom< /goal >
< /goals >
< /execution >
< /executions >
< /plugin >
< /plugins >
syntax = "proto3";
//包所在路径
option java_package = "com.greatom.dockerdemo.rule";
option java_multiple_files = true;
package rule;
//声明服务和方法
service RuleService {
//查询并更新规则
rpc getArchivesDic (RuleRequest) returns (RuleResponse);
//获取当前规则字典
rpc getRule (Request) returns (Response);
}
//定义请求对象
message RuleRequest {
// message RuleRPCDTO {
// int32 ruleCode = 1;
// string administrativeCost = 2;
// }
Response ruleRPCDTO = 1;
int32 basicId = 2;
}
//定义响应对象
message RuleResponse {
int32 id = 1;
}
message Request {
}
//定义响应消息
message Response {
int32 ruleCode = 1;
string administrativeCost = 2;
}
// 继承生成的RuleServiceGrpc.RuleServiceImplBase
// 实现接口具体逻辑
@Component
public class RuleGRPCServer extends RuleServiceGrpc.RuleServiceImplBase {
// 更新规则字典
@Override
public void getArchivesDic(RuleRequest request, StreamObserver< RuleResponse > responseObserver) {
Response ruleRPCDTO = request.getRuleRPCDTO();
RuleDTO ruleDTO = new RuleDTO();
BeanUtils.copyProperties(ruleRPCDTO, ruleDTO);
RuleResponse ruleResponse = RuleResponse.newBuilder().setId(1).build();
responseObserver.onNext(ruleResponse);
responseObserver.onCompleted();
}
// 查询规则字典
@Override
public void getRule(Request request, StreamObserver< Response > responseObserver) {
Response response = Response.newBuilder().setRuleCode(1)
.setAdministrativeCost("2222").build();
responseObserver.onNext(response);
responseObserver.onCompleted();
}
}
public static void main(String[] args) throws Exception {
// 设置service接口.
Server server = ServerBuilder.forPort(9999).addService(new RuleGRPCServiceImpl()).build().start();
System.out.println(String.format("GRpc服务端启动成功, 端口号: %d.", port));
server.awaitTermination();
}
日志 --- GRpc服务端启动成功, 端口号: 9999.
public static void main(String[] args) throws Exception {
// 1. 拿到一个通信的channel
ManagedChannel managedChannel = ManagedChannelBuilder.forAddress("localhost", 9999).usePlaintext().build();
try {
// 2.拿到道理对象
RuleServiceGrpc.RuleServiceBlockingStub rpcDateService = RuleServiceGrpc.newBlockingStub(managedChannel);
Request rpcDateRequest = Request
.newBuilder()
.build();
// 3. 请求
Response rpcDateResponse = rpcDateService.getRule(rpcDateRequest);
// 4. 输出结果
System.out.println(rpcDateResponse.getRuleCode());
} finally {
// 5.关闭channel, 释放资源.
managedChannel.shutdown();
}
}
日志:
- 16:05:44.628 [grpc-nio-worker-ELG-1-2] DEBUG io.grpc.netty.shaded.io.grpc.netty.NettyClientHandler - [id: 0x8447cc92, L:/127.0.0.1:60973 - R:localhost/127.0.0.1:9999] INBOUND DATA: streamId=3 padding=0 endStream=false length=12 bytes=0000000007086f1203323232
- 16:05:44.648 [grpc-nio-worker-ELG-1-2] DEBUG io.grpc.netty.shaded.io.grpc.netty.NettyClientHandler - [id: 0x8447cc92, L:/127.0.0.1:60973 - R:localhost/127.0.0.1:9999] INBOUND HEADERS: streamId=3 headers=GrpcHttp2ResponseHeaders[grpc-status: 0] padding=0 endStream=true
- 输出结果-----111
- 16:05:44.664 [grpc-nio-worker-ELG-1-2] DEBUG io.grpc.netty.shaded.io.grpc.netty.NettyClientHandler - [id: 0x8447cc92, L:/127.0.0.1:60973 - R:localhost/127.0.0.1:9999] OUTBOUND GO_AWAY: lastStreamId=0 errorCode=0 length=0 bytes=
总结
全部0条评论
快来发表一下你的评论吧 !