有关grpc的前置知识:gRPC
相关依赖
-
io.grpc:grpc-netty-shaded:1.61.0
- 这是gRPC框架的一个库,提供Netty实现以支持gRPC的网络通信。 -
io.grpc:grpc-protobuf:1.61.0
- 这是gRPC框架的一个库,提供对Protocol Buffers的支持,用于定义消息格式和服务接口。 -
io.grpc:grpc-stub:x
- 这是gRPC框架的一个库,提供自动生成的客户端和服务器存根,用于在gRPC服务之间进行通信。 -
org.apache.tomcat:annotations-api:6.0.53
- 这是Apache Tomcat的一个库,提供了Java注解API的实现。在Java 9+中,您需要将此依赖项添加到您的项目中,以便在构建和运行时正确处理注解。
<dependency><groupId>io.grpc</groupId><artifactId>grpc-netty-shaded</artifactId><version>1.61.0</version><scope>runtime</scope>
</dependency>
<dependency><groupId>io.grpc</groupId><artifactId>grpc-protobuf</artifactId><version>1.61.0</version>
</dependency>
<dependency><groupId>io.grpc</groupId><artifactId>grpc-stub</artifactId><version>1.61.0</version>
</dependency>
<!-- Java 9+ 需要添加 -->
<dependency> <groupId>org.apache.tomcat</groupId><artifactId>annotations-api</artifactId><version>6.0.53</version><scope>provided</scope>
</dependency>
代码生成配置
关于build中的os-maven-plugin插件,作用是生成对应系统的相关版本参数${os.detected.classifier}
<extensions><extension><groupId>kr.motd.maven</groupId><artifactId>os-maven-plugin</artifactId><version>1.7.1</version></extension></extensions>
- 生成对应class类
- compile:用于生成protobuf类
- compile-custom:生成rpc调用类,其中会用到compile过程中生成的protobuf类
<build><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.25.1:exe:${os.detected.classifier}</protocArtifact><pluginId>grpc-java</pluginId><pluginArtifact>io.grpc:protoc-gen-grpc-java:1.61.0:exe:${os.detected.classifier}</pluginArtifact></configuration><executions><execution><goals><goal>compile</goal><goal>compile-custom</goal></goals></execution></executions></plugin></plugins>
</build>