grpc(java实现)
可以看看中文官方文档或者官方文档 grpc是什么,官方文档告诉你,我来告诉你怎么使用Java实现!
maven依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.examole</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>io.grpc</groupId>
<artifactId>grpc-all</artifactId>
<version>1.12.0</version>
</dependency>
</dependencies>
<build>
<extensions>
<extension>
<groupId>kr.motd.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>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.0</version>
<configuration>
<pluginId>grpc-java</pluginId>
<protocArtifact>com.google.protobuf:protoc:3.0.2:exe:${os.detected.classifier}</protocArtifact>
<pluginArtifact>io.grpc: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文件
文件布局:
syntax = "proto3";
option java_package = "com.example.grpc.api";
option java_outer_classname = "RPCDateServiceApi";
option java_multiple_files = true;
package com.example.grpc.api;
service RPCDateService {
rpc getDate (RPCDateRequest) returns (RPCDateResponse) {
}
}
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才能运行插件)
服务端server
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 {
@Override
public void getDate(RPCDateRequest request, StreamObserver<RPCDateResponse> responseObserver) {
try {
String message = request.getId() + "\ngetUserName:" +
request.getUserName() + "\ngetMessage:" +
request.getMessage() + "\ngetAge:" +
request.getAge() + "\ngetSerializedSize:" +
request.getAllFields();
System.out.println("message" + message);
RPCDateResponse build = RPCDateResponse.newBuilder().setServerDate(message).build();
System.out.println("bulid:" + build);
responseObserver.onNext(build);
responseObserver.onCompleted();
} catch (Exception e) {
System.out.println("impl---错误信息====》");
responseObserver.onError(e);
}
}
}
客户端client
public class GrpcClient {
public static void main(String[] args) {
ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost",8888)
.usePlaintext().build();
try {
RPCDateRequest build = RPCDateRequest.newBuilder()
.setId(117)
.setUserName("yyx")
.setMessage("喜欢吃蔬菜")
.setAge(22).build();
System.out.println("build:" + build+"...");
RPCDateServiceGrpc.RPCDateServiceBlockingStub stub = RPCDateServiceGrpc.newBlockingStub(channel);
stub.getDate(build);
while (true) {
Scanner scanner = new Scanner(System.in);
String next = scanner.next();
if (next .equals("exit")) {
break;
}
}
}finally {
channel.shutdown();
}
}
}
服务端启动->客户端启动(发送数据)->服务端接收数据 而这一整个过程的调用由我们的impl类进行承接,至于底层grpc帮我们实现好了
如果有哪里纰漏,还望大佬们指出,我定修改完善 工作中具体用法后面用到了再写
|