-
项目结构
-
pom引入(parent中引入即可)
<properties> <net-devh-grpc.version>2.14.0.RELEASE</net-devh-grpc.version> <os-maven-plugin.version>1.6.0</os-maven-plugin.version> <protobuf-maven-plugin.version>0.5.1</protobuf-maven-plugin.version> </properties> <dependency> <groupId>net.devh</groupId> <artifactId>grpc-spring-boot-starter</artifactId> <version>2.14.0.RELEASE</version> </dependency>
-
创建lib、service、client服务
a. lib中, 创建proto文件
syntax = "proto3"; option java_package = "com.mose.proto.msg"; // 定义service -- 即 class service MsgService { // 定义 方法名 入参对象 返回 回参对象 rpc SendMsg (MsgRequest) returns (MsgResponse); } // 定义 入参对象 // 其中的 1、2、3 只是为了排序 message MsgRequest { int32 a = 1; int32 b = 2; Type type = 3; } // 定义 回参对象 message MsgResponse { int32 c = 1; } // 定义 入参/回参 枚举类型 // 默认值为第一个,即 = 0 的为默认值 enum Type { add = 0; cut = 1; }
b. pom中增加build信息
<build>
<extensions>
<!-- os-maven-plugin -->
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>${os-maven-plugin.version}</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<!-- protobuf-maven-plugin -->
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>${protobuf-maven-plugin.version}</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.11.0:exe:${os.detected.classifier}</pluginArtifact>
<outputDirectory>${project.build.sourceDirectory}</outputDirectory>
<clearOutputDirectory>false</clearOutputDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
c. 在lib中执行 mvn clean install , 即可生成proto文件对应的java文件,如下图
-
service 实现接口
server: port: 8081 spring: application: name: grpc-service grpc: server: port: 9091
import com.mose.proto.msg.Msg; import com.mose.proto.msg.MsgServiceGrpc; import io.grpc.stub.StreamObserver; import net.devh.boot.grpc.server.service.GrpcService; @GrpcService public class MsgService extends MsgServiceGrpc.MsgServiceImplBase { @Override public void sendMsg(Msg.MsgRequest request, StreamObserver<Msg.MsgResponse> responseObserver) { // 获取参数 int a = request.getA(); int b = request.getB(); Msg.Type type = request.getType(); // 处理逻辑 int c = 0; switch (type) { case add: c = a + b; break; case cut: c = a - b; break; } // 构造回参 Msg.MsgResponse response = Msg.MsgResponse.newBuilder().setC(c).build(); // 返回 responseObserver.onNext(response); // 处理完成 responseObserver.onCompleted(); } }
-
client 调用接口
server: port: 8082 spring: application: name: grpc-client grpc: server: port: 9092 client: grpc-service: address: localhost:9091 negotiation-type: plaintext
import com.mose.proto.msg.Msg; import com.mose.proto.msg.MsgServiceGrpc; import net.devh.boot.grpc.client.inject.GrpcClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MsgController { /** * 获取调用存根 */ @GrpcClient("grpc-service") private MsgServiceGrpc.MsgServiceBlockingStub stub; @GetMapping("msg") public int sendMsg(int a, int b) { return stub.sendMsg( // 构造入参 Msg.MsgRequest.newBuilder().setA(a).setB(b).setType(Msg.Type.cut).build() ) // 获取回参 .getC(); } }
-
调用 以及结果