【问题标题】:Using Spring Boot together with gRPC and Protobuf将 Spring Boot 与 gRPC 和 Protobuf 一起使用
【发布时间】:2015-11-03 11:17:22
【问题描述】:

任何人有任何使用 gRPC 和 Spring Boot 的示例或想法吗?

【问题讨论】:

  • spring.io/blog/2015/03/22/… 可能是一本不错的读物。
  • 不错,已经找到了。但我想知道是否有人也将它与 protobuf 服务定义联系在一起?
  • 也找个例子
  • 我推荐使用yidongnan/grpc-spring-boot-starter。它同时支持服务器和客户端以及 Spring-Security、Metrics 和更多特性和示例。 披露:我是该库的核心维护者之一。如果您对该库有任何疑问,请随时在此处打开问题。

标签: spring spring-boot protocol-buffers grpc


【解决方案1】:

如果它仍然对你有用,我已经创建了 gRPC spring-boot-starter here

grpc-spring-boot-starter 使用 @GRpcService-enabled bean 自动配置和运行嵌入式 gRPC 服务器。

最简单的例子:

@GRpcService(grpcServiceOuterClass = GreeterGrpc.class)
public static class GreeterService implements GreeterGrpc.Greeter {

    @Override 
    public void sayHello(GreeterOuterClass.HelloRequest request, StreamObserver<GreeterOuterClass.HelloReply> responseObserver) {
      // omitted 
    }

}

在项目的 README 文件中还有一个如何将启动器与 Eureka 集成的示例。

【讨论】:

  • 如果你想为客户端和服务器使用“相同”的库,我推荐使用yidongnan/grpc-spring-boot-starter。它还具有 Spring-Security 支持。 披露:我是该库的核心维护者之一。
  • Spring Boot 是否没有为 gRPC 和 protobuf 提供任何支持作为启动器依赖项,例如 data-jpa、日志记录、测试等的支持?
  • github.com/LogNet/grpc-spring-boot-starter 是非官方的。目前 Spring 团队不提供 grpc 的启动器。
  • 通过@GrpcService 实例化是否会发生属性自动装配?
  • 服务将通过 spring 的 bean factory 实例化,自动装配按原样工作。
【解决方案2】:

https://spring.io/blog/2015/03/22/using-google-protocol-buffers-with-spring-mvc-based-rest-services开始,然后
看一眼 SPR-13589 ProtobufHttpMessageConverter support for protobuf 3.0.0-beta4 及相关SPR-13203 HttpMessageConverter based on Protostuff library

这是在 Spring 5 中对 proto3 的一些支持。由于它正在开发中,因此鼓励人们投票并提出对他们的项目重要的内容。

【讨论】:

【解决方案3】:

https://github.com/yidongnan/grpc-spring-boot-starter

在服务器中

@GrpcService(GreeterGrpc.class)
public class GrpcServerService extends GreeterGrpc.GreeterImplBase {

    @Override
    public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
        HelloReply reply = HelloReply.newBuilder().setMessage("Hello =============> " + req.getName()).build();
        responseObserver.onNext(reply);
        responseObserver.onCompleted();
    }
}

在客户端

@GrpcClient("gRPC server name")
private Channel serverChannel;

GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(serverChannel);
HelloReply response = stub.sayHello(HelloRequest.newBuilder().setName(name).build());

【讨论】:

    【解决方案4】:

    在这里我使用 gRpc 和 eureka 进行通信。本项目基于 Spring-boot

    https://github.com/WThamira/grpc-spring-boot

    此外,您还可以使用注册为领事。此 repo 中的完整示例

    https://github.com/WThamira/gRpc-spring-boot-example

    这个 maven 依赖对 gRpc 的帮助

            <dependency>
                <groupId>io.grpc</groupId>
                <artifactId>grpc-stub</artifactId>
                <version>1.0.1</version>
            </dependency>
            <dependency>
                <groupId>io.grpc</groupId>
                <artifactId>grpc-protobuf</artifactId>
                <version>1.0.1</version>
            </dependency>
            <dependency>
                <groupId>io.grpc</groupId>
                <artifactId>grpc-netty</artifactId>
                <version>1.0.1</version>
            </dependency>
    

    需要插件显示在下面

           <plugin>
                    <groupId>org.xolstice.maven.plugins</groupId>
                    <artifactId>protobuf-maven-plugin</artifactId>
                    <version>0.5.0</version>
                    <configuration>
                        <!-- The version of protoc must match protobuf-java. If you don't depend 
                            on protobuf-java directly, you will be transitively depending on the protobuf-java 
                            version that grpc depends on. -->
                        <protocArtifact>com.google.protobuf:protoc:3.0.2:exe:${os.detected.classifier}</protocArtifact>
                        <pluginId>grpc-java</pluginId>
                        <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.0.1:exe:${os.detected.classifier}</pluginArtifact>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>compile</goal>
                                <goal>compile-custom</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
    

    【讨论】:

      【解决方案5】:

      如果您需要 gRPC 客户端 库,即使用存根,请查看我的库 https://github.com/sfcodes/grpc-client-spring-boot

      这个库会自动扫描你的类路径,找到所有的 gRPC 存根类,实例化它们,并将它们注册为 bean 到 ApplicationContext;让您可以轻松地@Autowire 并像注入任何其他 Spring bean 一样注入它们。例如:

      @RestController
      public class GreeterController {
      
          @Autowired  // <===== gRPC stub is autowired!
          private GreeterGrpc.GreeterBlockingStub greeterStub;
      
          @RequestMapping(value = "/sayhello")
          public String sayHello(@RequestParam String name) {
              HelloRequest request = HelloRequest.newBuilder().setName(name).build();
              HelloReply reply = greeterStub.sayHello(request);
              return reply.getMessage();
          }
      }
      

      对于 gRPC server 库,我也推荐LogNet/grpc-spring-boot-starter

      【讨论】:

        【解决方案6】:

        在这个 Github Repo[1] 中,您将找到一个使用 gRPC 将用户插入和查看到 couchbase 数据库的示例。请参考 proto 文件[2] 找到 rpc 方法。

        通常使用像bloomRPC 这样的gRPC 客户端来访问服务。使用 envoy 代理,可以使用 HTTP/1.1 转码和访问服务。在自述文件中显示了创建配置文件和使用 docker 文件运行特使代理的步骤。

        [1]https://github.com/Senthuran100/grpc-User
        [2]https://github.com/Senthuran100/grpc-User/blob/master/src/main/proto/user.proto

        【讨论】:

          【解决方案7】:

          使用 GRPC 创建了一个简单的 Springboot 应用程序。此 GitHub 存储库同时包含服务器和客户端示例。 Repo 有单独的 Maven 模块(grpc-interface),我们在其中声明 Proto 文件并生成 Java 源代码,然后可以在服务器和客户端应用程序中用作 lib。

          https://github.com/vali7394/grpc-springboot-repo

          【讨论】:

            猜你喜欢
            • 2019-08-03
            • 1970-01-01
            • 2018-12-06
            • 2017-03-21
            • 2021-01-02
            • 2020-03-09
            • 2015-05-15
            • 2018-06-28
            • 2017-08-04
            相关资源
            最近更新 更多