gprc-java与golang分别实现服务端,客户端,跨语言通信(一.java实现)
2024-08-30 00:26:38
1.在pom中引入
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<version>1.18.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.grpc/grpc-protobuf -->
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.18.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.grpc/grpc-stub -->
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.18.0</version>
</dependency>
2.maven配置
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId><!--引入操作系统os设置的属性插件,否则${os.detected.classifier} 操作系统版本会找不到 -->
<version>1.5.0.Final</version>
</extension>
</extensions>
<plugins>
<!--添加编译proto文件的编译程序和对应的编译插件-->
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.5.1-1:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.14.0:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
3.编写IDL文件, 因为要夸语言调用, 所以和golang的使用同一个文件,除了option之外, 所有的东西不要改!!!
syntax = "proto3"; option java_multiple_files = true;
option java_package = "cn.com.xu.grpc";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW"; package helloworld; // The greeting service definition.
service Greeter {
// 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;
}
4.生成文件
5.客户端代码
public class GrpcClient {
private final ManagedChannel channel;
private final GreeterGrpc.GreeterBlockingStub blockingStub; public GrpcClient(String host,int port){
channel = ManagedChannelBuilder.forAddress(host,port)
.usePlaintext(true)
.build(); blockingStub = GreeterGrpc.newBlockingStub(channel);
} public void shutdown() throws InterruptedException {
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
} public void greet(String name){
HelloRequest request = HelloRequest.newBuilder().setName(name).build();
HelloReply response = blockingStub.sayHello(request);
System.out.println("this is java client, response..." + response.getMessage());
} public static void main(String[] args) throws InterruptedException {
GrpcClient client = new GrpcClient("localhost",50001);
client.greet("this is java client");
} }
6.服务端代码
public class GrpcServer {
private int port = 50001;
private Server server; private void start() throws IOException {
server = ServerBuilder.forPort(port)
.addService(new GreeterImpl())
.build()
.start(); System.out.println("service start..."); Runtime.getRuntime().addShutdownHook(new Thread() { @Override
public void run() { System.err.println("*** shutting down gRPC server since JVM is shutting down");
GrpcServer.this.stop();
System.err.println("*** server shut down");
}
});
} private void stop() {
if (server != null) {
server.shutdown();
}
} // block 一直到退出程序
private void blockUntilShutdown() throws InterruptedException {
if (server != null) {
server.awaitTermination();
}
} public static void main(String[] args) throws IOException, InterruptedException { final GrpcServer server = new GrpcServer();
server.start();
server.blockUntilShutdown();
} // 实现 定义一个实现服务接口的类
private class GreeterImpl extends GreeterGrpc.GreeterImplBase {
public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
System.out.println("this is java service, request..."+req.getName());
HelloReply reply = HelloReply.newBuilder().setMessage(("this is java service")).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}
} }
最新文章
- mybatis里的foreach语句
- C#抽象方法和虚拟方法理解
- Java IO流
- 数据存储--sqlite总结
- ECharts开始
- alt text 与 tooltip区别
- hope is a good thing!
- MyEclipse/Eclipse导入sun.misc.BASE64Encoder jar包步骤
- Form - CHECKBOX全选功能
- 【转】iOS 开发怎么入门?
- .net 爬虫技术
- [JSOI2007]建筑抢修
- 关于取li中的value
- Table组件设置文字超出宽度显示省略号,鼠标悬停以悬浮框显示
- 视频人脸检测——Dlib版(六)
- c++入门之引用
- 转---Python——numpy random类
- Spring data JPA中使用Specifications动态构建查询
- 如何搭建Packetbeat性能监控
- [ucgui] 仪表盘函数
热门文章
- zoom和transform scale
- bzoj3696
- org.springframework.web.struts.ContextLoaderPlugIn 和 org.springframework.web.context.ContextLoaderListener
- Gym 100531J Joy of Flight (几何)
- bzoj 1027: [JSOI2007]合金【凸包+Floyd】
- Luogu P1970 花匠 【线性Dp】 By cellur925
- 《windows核心编程系列》十九谈谈使用远程线程来注入DLL。
- python 学习笔记二 (列表推导式)
- 题解报告:hdu1995汉诺塔V(递推dp)
- Hibernate3的hbm文件错误引用dtd文件导致项目无法启动问题处理