折腾-wordpress+grpc+java对接(3)rpc-java服务端配置

本项目基于Spring Boot 2.0.9+yidongnan/grpc-spring-boot-starter 2.1.0.RELEASE,通过grpc-lib,grpc-server,2个模块来模拟grpc的服务调用。

1. 项目结构

根目录java-grpc-wp下有2个子模块:

  • wp-grpc-lib:包含原始proto文件,并负责将其转换为java代码
  • wp-grpc-server:grpc服务端

2. 项目搭建

  1. 新建mavne父项目java-grpc-wp,pom.xml文件 如下

    <?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.datayang</groupId>
        <artifactId>java-grpc-wp</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>pom</packaging>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.9.RELEASE</version>
        </parent>
    
        <!--引入的子模块-->
        <modules>
            <module>wp-grpc-lib</module>
            <module>wp-grpc-server</module>
        </modules>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <spring-boot.version>2.0.9.RELEASE</spring-boot.version>
            <net-devh-grpc.version>2.1.0.RELEASE</net-devh-grpc.version>
        </properties>
    
        <dependencyManagement>
            <dependencies>
                <!--依赖维护平台,在添加第三方依赖的时候,不需要写版本号,它能够自动帮我们挑选一个最优的版本-->
                <dependency>
                    <groupId>io.spring.platform</groupId>
                    <artifactId>platform-bom</artifactId>
                    <!-- <version>Cairo-RELEASE</version> -->
                    <version>Cairo-SR8</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <!--公共grpc模块-->
                <dependency>
                    <groupId>com.datayang</groupId>
                    <artifactId>wp-grpc-lib</artifactId>
                    <version>${project.version}</version>
                </dependency>
                <!--核心grpc-spring-boot依赖-->
                <dependency>
                    <groupId>net.devh</groupId>
                    <artifactId>grpc-client-spring-boot-starter</artifactId>
                    <version>${net-devh-grpc.version}</version>
                </dependency>
                <dependency>
                    <groupId>net.devh</groupId>
                    <artifactId>grpc-server-spring-boot-starter</artifactId>
                    <version>${net-devh-grpc.version}</version>
                </dependency>
            </dependencies>
        </dependencyManagement>
    </project>   
    
  2. 新建子模块wp-grpc-lib,

    proto文件目录是${project.basedir}/src/main/proto

    生成的java代码在${project.basedir}/src/main/java

    使用maven指令compile可完成代码生成,或直接在eclipse里run as -> maven build。 protobuf:compileprotobuf:compile-custom。pom.xml如下

    <?xml version="1.0"?>
     <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
     xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     <modelVersion>4.0.0</modelVersion>
     <parent>
         <groupId>com.datayang</groupId>
         <artifactId>java-grpc-wp</artifactId>
         <version>1.0-SNAPSHOT</version>
     </parent>
     <artifactId>wp-grpc-lib</artifactId>
     <name>wp-grpc-lib</name>
     <packaging>jar</packaging>
    
    
    
     <properties>
             <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
             <os.plugin.version>1.6.2</os.plugin.version>
             <protoc.version>3.11.2</protoc.version>
             <protobuf.plugin.version>0.6.1</protobuf.plugin.version>
             <grpc.version>1.26.0</grpc.version>
         </properties>
    
         <dependencies>
    
             <dependency>
                 <groupId>io.grpc</groupId>
                 <artifactId>grpc-all</artifactId>
                 <version>${grpc.version}</version>
             </dependency>
    
             <!-- 解决protobuf生产UnusedPrivateParameter 错误 -->
             <dependency>
                 <groupId>com.google.protobuf</groupId>
                 <artifactId>protobuf-java</artifactId>
                 <version>3.11.1</version>
             </dependency>
    
         </dependencies>
    
         <build>
             <extensions>
                 <extension>
                     <groupId>kr.motd.maven</groupId>
                     <artifactId>os-maven-plugin</artifactId>
                     <version>${os.plugin.version}</version>
                 </extension>
             </extensions>
             <plugins>
                 <plugin>
                     <groupId>org.xolstice.maven.plugins</groupId>
                     <artifactId>protobuf-maven-plugin</artifactId>
                     <version>${protobuf.plugin.version}</version>
                     <extensions>true</extensions>
                     <configuration>
                         <protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}</protocArtifact>
                         <pluginId>grpc-java</pluginId>
                         <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
                         <!--默认值-->
                         <protoSourceRoot>${project.basedir}/src/main/proto</protoSourceRoot>
                         <!--默认值-->
                         <!--<outputDirectory>${project.build.directory}/generated-sources/protobuf/java</outputDirectory>-->
                         <outputDirectory>${project.basedir}/src/main/java</outputDirectory>
                         <!--设置是否在生成java文件之前清空outputDirectory的文件,默认值为true,设置为false时也会覆盖同名文件-->
                         <clearOutputDirectory>false</clearOutputDirectory>
                         <!--更多配置信息可以查看https://www.xolstice.org/protobuf-maven-plugin/compile-mojo.html-->
                     </configuration>
                     <executions>
                         <execution>
                         <!-- 在执行mvn compile的时候会执行以下操作 -->
                         <phase>compile</phase>
                         <goals>
                            <!--  生成OuterClass类 -->
                             <goal>compile</goal>
                             <!-- 生成Grpc类 -->
                             <goal>compile-custom</goal>
                         </goals>
                     </execution> 
                     </executions>
                 </plugin>
             </plugins>
         </build>
     </project>
    
  3. 新建子模块wp-grpc-server, pom.xml如下

    <?xml version="1.0"?>
     <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     <modelVersion>4.0.0</modelVersion>
    
     <artifactId>wp-grpc-server</artifactId>
     <name>wp-grpc-server</name>
     <packaging>jar</packaging>
    
     <parent>
         <groupId>com.datayang</groupId>
         <artifactId>java-grpc-wp</artifactId>
         <version>1.0-SNAPSHOT</version>
     </parent>
    
     <dependencies>
             <dependency>
                 <groupId>com.datayang</groupId>
                 <artifactId>wp-grpc-lib</artifactId>
             </dependency>
             <dependency>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-starter</artifactId>
             </dependency>
             <dependency>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-starter-test</artifactId>
                 <scope>test</scope>
             </dependency>
             <dependency>
                 <groupId>org.projectlombok</groupId>
                 <artifactId>lombok</artifactId>
             </dependency>
             <dependency>
                 <groupId>net.devh</groupId>
                 <artifactId>grpc-server-spring-boot-starter</artifactId>
             </dependency>
         </dependencies>
    
         <build>
             <plugins>
                 <plugin>
                     <groupId>org.springframework.boot</groupId>
                     <artifactId>spring-boot-maven-plugin</artifactId>
                 </plugin>
             </plugins>
         </build>
    
     </project>
    

3. 实现功能

  1. 在wp-grpc-lib模块利用proto文件生成java代码

    1. 编写proto文件,文件如下
      syntax = "proto3";
      
      option java_package = "com.datayang.wp.grpc.lib";
      
      // The greeter service definition.
      service Greeter {
          // Sends a greeting
          rpc SayHello ( HelloRequest) returns (  HelloReply) {}
      
      }
      // The request message containing the user's name.
      message HelloRequest {
          string name = 1;
      }
      // The response message containing the greetings
      message HelloReply {
          string message = 1;
      }
      
    2. 生成代码 根据上文pom文件的设置,将proto文件放到src/main/proto目录下,在eclipse里run as -> maven build。 protobuf:compileprotobuf:compile-custom
  2. 在wp-grpc-server模块实现接口

    1. 编写application文件
      首先在application.yml文件指定应用名和grpc端口,如下
      spring:
      application:
          name: local-grpc-server
      grpc:
      server:
          port: 9898
      
    2. 编写GreeterService类
      package com.datayang.wp.grpc.server;
      
      import com.datayang.wp.grpc.lib.GreeterGrpc;
      import com.datayang.wp.grpc.lib.GreeterOuterClass;
      
      import io.grpc.stub.StreamObserver;
      import lombok.extern.slf4j.Slf4j;
      import net.devh.springboot.autoconfigure.grpc.server.GrpcService;
      
      @Slf4j
      @GrpcService(GreeterOuterClass.class)
      public class GreeterService extends GreeterGrpc.GreeterImplBase {
          @Override
          public void sayHello(GreeterOuterClass.HelloRequest request, StreamObserver<GreeterOuterClass.HelloReply> responseObserver) {
              String message = "Hello " + request.getName();
              final GreeterOuterClass.HelloReply.Builder replyBuilder = GreeterOuterClass.HelloReply.newBuilder().setMessage(message);
              responseObserver.onNext(replyBuilder.build());
              responseObserver.onCompleted();
              log.info("Returning " +message);
          }
      }
      

4. 启动项目

进入上篇php目录,cmd 执行php .\RpcMain.php

暂无评论
发表新评论