可以看看中文官方文档或者官方文档
grpc是什么,官方文档告诉你,我来告诉你怎么使用Java实现!
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=".0.0"xmlns:xsi=""xsi:schemaLocation=".0.0 .0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId&amole</groupId><artifactId>grpc-study-demo-01</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.21</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.21</version></dependency><dependency><groupId&pc</groupId><artifactId>grpc-all</artifactId><version>1.12.0</version></dependency></dependencies><build><extensions><extension><groupId&d.maven</groupId><artifactId>os-maven-plugin</artifactId><version>1.4.1.Final</version></extension></extensions><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.0</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin><plugin><groupId&lstice.maven.plugins</groupId><artifactId>protobuf-maven-plugin</artifactId><version>0.5.0</version><configuration><pluginId>grpc-java</pluginId><protocArtifact&le.protobuf:protoc:3.0.2:exe:${os.detected.classifier}</protocArtifact><pluginArtifact&pc:protoc-gen-grpc-java:1.2.0:exe:${os.detected.classifier}</pluginArtifact></configuration><executions><execution><goals><goal>compile</goal><goal>compile-custom</goal></goals></execution></executions></plugin></plugins></build>
</project>
文件布局:
命名**.proto文件在proto文件夹内
syntax = "proto3"; // 协议版本// 选项配置
option java_package = pc.api";
option java_outer_classname = "RPCDateServiceApi";
option java_multiple_files = true;// 定义包名
pc.api;// 定义服务类型:服务接口.定义请求参数和相应结果
service RPCDateService {rpc getDate (RPCDateRequest) returns (RPCDateResponse) {}
}// 定义请求体(即我们发送出去的请求数据的数据类型,由1递增)
message RPCDateRequest {int32 id = 1;string userName = 2;string message = 3;int32 age = 4;
}// 定义相应内容(即定义我们接收请求数据的数据类型)
message RPCDateResponse {string serverDate = 1;
}
四种服务类型
other
成功的话,就会出现下面的target文件
(注意:如果下面的server代码里,调用不出来该用的类,那么注意一下生成的target-》generated-sources-》protobuf下的包类型是否和图中一致,不一致的话:鼠标右击该包-》找到Mark Directory as改变成图中的包样式,这样即可调用插件为我们生成的类)
注意:(如果运行失败且出现下面的报错信息,没有则跳过)
出现这个错误,那代表你的maven版本不够(在setting设置下的maven看一下,我的3.6貌似不行,需要3.8才能运行插件)
public class GrpcServer {public static void main(String[] args) {Server server=null;try {server = ServerBuilder.forPort(8888).addService(new MyRPCDateServiceImpl()).build().start();server.awaitTermination();//持续等待信息} catch (IOException e) {System.out.println("server--错误信息===>"+e);} catch (InterruptedException e) {System.out.println("server--错误信息===>"+e);} finally {if (server!=null) {server.shutdown();}}}
}
public class MyRPCDateServiceImpl extends RPCDateServiceGrpc.RPCDateServiceImplBase {@Overridepublic void getDate(RPCDateRequest request, StreamObserver<RPCDateResponse> responseObserver) {try {String message = Id() + "ngetUserName:" UserName() + "ngetMessage:" Message() + "ngetAge:" Age() + "ngetSerializedSize:" AllFields();System.out.println("message" + message);RPCDateResponse build = wBuilder().setServerDate(message).build();System.out.println("bulid:" + build);Next(build);Completed();} catch (Exception e) {System.out.println("impl---错误信息====》");Error(e);}}
}
public class GrpcClient {public static void main(String[] args) {//1.拿到一个通信的channelManagedChannel channel = ManagedChannelBuilder.forAddress("localhost",8888).usePlaintext().build();try {//2.将信息传入request对象RPCDateRequest build = wBuilder().setId(117).setUserName("yyx").setMessage("喜欢吃蔬菜").setAge(22).build();System.out.println("build:" + build+"...");RPCDateServiceGrpc.RPCDateServiceBlockingStub stub = wBlockingStub(channel);Date(build);while (true) {Scanner scanner = new Scanner(System.in);String next = ();if (next .equals("exit")) {break;}}}finally {channel.shutdown();}}
}
服务端启动->客户端启动(发送数据)->服务端接收数据
而这一整个过程的调用由我们的impl类进行承接,至于底层grpc帮我们实现好了
如果有哪里纰漏,还望大佬们指出,我定修改完善
工作中具体用法后面用到了再写
本文发布于:2024-02-02 21:20:13,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170688001046506.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |