1.安装protocbuf
下载protobuf window版
配置环境变量:
变量名:PROTOCBUF_HOME
变量值:D:\protoc-3.19.1-win64
找到系统变量中的path变量,选中后点击编辑,在新建的内容中输入:%PROTOBUF_HOME%\bin
- idea安装protocbuf插件
2. gRPC项目构建
1. 插件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| <build> <extensions> <extension> <groupId>kr.motd.maven</groupId> <artifactId>os-maven-plugin</artifactId> <version>1.6.2</version> </extension> </extensions> <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.17.3:exe:${os.detected.classifier}</protocArtifact> <pluginId>grpc-java</pluginId> <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.42.0:exe:${os.detected.classifier}</pluginArtifact> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>compile-custom</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-netty-shaded</artifactId> <version>1.42.0</version> </dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-protobuf</artifactId> <version>1.42.0</version> </dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-stub</artifactId> <version>1.42.0</version> </dependency> <dependency> <!-- necessary for Java 9+ --> <groupId>org.apache.tomcat</groupId> <artifactId>annotations-api</artifactId> <version>6.0.53</version> <scope>provided</scope> </dependency>
|
2. 添加.proto文件
一定要在和src/main/java源文件目录同级的proto源文件目录才可以。
helloworld.proto
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| syntax = "proto3"; // 协议版本
// 选项配置 option java_package = "com.cgh.protobuf"; option java_outer_classname = "RPCDateServiceApi"; option java_multiple_files = true;
// 定义包名 package com.cgh.protobuf;
// 服务接口.定义请求参数和相应结果 service RPCDateService { rpc getDate (RPCDateRequest) returns (RPCDateResponse) { } }
// 定义请求体 message RPCDateRequest { string userName = 1; }
// 定义响应内容 message RPCDateResponse { string serverDate = 1; }
|
3. 用插件根据.proto文件生成消息体类文件和XXXGrpc类文件
mvn protobuf:compile 生成消息体类文件 (多次执行可能失败)
mvn protobuf:compile-custom 生成XXXGrpc类文件 (多次执行可能失败)
- 把生成的消息体文件和 XXXGrpc 文件copy到项目
4. 编写实现类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| import com.cgh.protobuf.RPCDateRequest; import com.cgh.protobuf.RPCDateResponse; import com.cgh.protobuf.RPCDateServiceGrpc; import io.grpc.stub.StreamObserver;
import java.time.LocalDate; import java.time.format.DateTimeFormatter;
public class RPCDateServiceImpl extends RPCDateServiceGrpc.RPCDateServiceImplBase {
@Override public void getDate(RPCDateRequest request, StreamObserver<RPCDateResponse> responseObserver) { //请求结果,我们定义的 RPCDateResponse rpcDateResponse = null; // String userName = request.getUserName(); String response = String.format("你好:%s,今天是%s.", userName, LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); try { // 定义响应,是一个builder构造器. rpcDateResponse = RPCDateResponse.newBuilder() .setServerDate(response) .build(); //int i = 10/0; } catch (Exception e) { responseObserver.onError(e); } finally {
responseObserver.onNext(rpcDateResponse); }
responseObserver.onCompleted(); } }
|
5. 定义服务端
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public class GRPCServer { private static final int port = 9999;
public static void main(String[] args) throws IOException, InterruptedException { //设置service端口 Server server = ServerBuilder.forPort(port) .addService(new RPCDateServiceImpl()) .build().start(); System.out.println(String.format("GRpc服务端启动成功, 端口号: %d.", port));
server.awaitTermination();
} }
|
6. 定义客户端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| private static final String host = "localhost"; private static final int serverPort = 9999; public static void main(String[] args) { //1,拿到一个通信channel ManagedChannel channel = ManagedChannelBuilder.forAddress(host, serverPort). usePlaintext()//无需加密或认证 .build(); try { //2.拿到stub对象 RPCDateServiceGrpc.RPCDateServiceBlockingStub rpcDateService = RPCDateServiceGrpc.newBlockingStub(channel); RPCDateRequest rpcDateRequest = RPCDateRequest.newBuilder() .setUserName("JACK") .build(); //3,请求 RPCDateResponse rpcDateResponse = rpcDateService.getDate(rpcDateRequest); //4,输出结果 System.out.println(rpcDateResponse.getServerDate()); } finally { // 5.关闭channel, 释放资源. channel.shutdown(); }
} }
|