SpringBoot整合gRPC - proto3 -- 简单明了

  1. 项目结构
    SpringBoot整合gRPC - proto3 -- 简单明了_第1张图片

  2. pom引入(parent中引入即可)

    <properties>
        <net-devh-grpc.version>2.14.0.RELEASEnet-devh-grpc.version>
        <os-maven-plugin.version>1.6.0os-maven-plugin.version>
        <protobuf-maven-plugin.version>0.5.1protobuf-maven-plugin.version>
    properties>
    
    <dependency>
        <groupId>net.devhgroupId>
        <artifactId>grpc-spring-boot-starterartifactId>
        <version>2.14.0.RELEASEversion>
    dependency>
    
  3. 创建lib、service、client服务
    a. lib中, 创建proto文件
    SpringBoot整合gRPC - proto3 -- 简单明了_第2张图片

    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>
         
         <extension>
             <groupId>kr.motd.mavengroupId>
             <artifactId>os-maven-pluginartifactId>
             <version>${os-maven-plugin.version}version>
         extension>
     extensions>
     <plugins>
         <plugin>
             <groupId>org.springframework.bootgroupId>
             <artifactId>spring-boot-maven-pluginartifactId>
             <executions>
                 <execution>
                     <goals>
                         <goal>repackagegoal>
                     goals>
                 execution>
             executions>
         plugin>
         <plugin>
             <groupId>org.apache.maven.pluginsgroupId>
             <artifactId>maven-surefire-pluginartifactId>
             <configuration>
                 <skip>trueskip>
             configuration>
         plugin>
         
         <plugin>
             <groupId>org.xolstice.maven.pluginsgroupId>
             <artifactId>protobuf-maven-pluginartifactId>
             <version>${protobuf-maven-plugin.version}version>
             <configuration>
                 <protocArtifact>com.google.protobuf:protoc:3.5.1-1:exe:${os.detected.classifier}protocArtifact>
                 <pluginId>grpc-javapluginId>
                 <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.11.0:exe:${os.detected.classifier}pluginArtifact>
                 <outputDirectory>${project.build.sourceDirectory}outputDirectory>
                 <clearOutputDirectory>falseclearOutputDirectory>
             configuration>
             <executions>
                 <execution>
                     <goals>
                         <goal>compilegoal>
                         <goal>compile-customgoal>
                     goals>
                 execution>
             executions>
         plugin>
     plugins>
 build>

c. 在lib中执行 mvn clean install , 即可生成proto文件对应的java文件,如下图
SpringBoot整合gRPC - proto3 -- 简单明了_第3张图片

  1. 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();
        }
    }
    
  2. 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();
        }
    }
    
    
  3. 调用 以及结果
    SpringBoot整合gRPC - proto3 -- 简单明了_第4张图片

你可能感兴趣的:(java,spring,boot,spring,boot,后端,java,rpc)