将Spring Boot与gRPC和Protobuf一起使用

时间:2015-08-11 09:36:11

标签: spring spring-boot protocol-buffers grpc

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

6 个答案:

答案 0 :(得分:24)

如果它仍然适合您,我已经创建了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集成的示例。

答案 1 :(得分:3)

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

这是对proto3的一些支持即将在春季5开始。由于它正在开发中,我们鼓励人们投票并提出对他们的项目重要的事情。

答案 2 :(得分:2)

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());

答案 3 :(得分:1)

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

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

@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 服务器库,我还建议使用LogNet/grpc-spring-boot-starter

答案 4 :(得分:0)

在这里,我使用gRpc和eureka进行交流。这个项目基于Spring-boot

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

另外,您也可以注册为领事。这个回购中的完整示例

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 :(得分:0)

在此Github Repo [1]中,您将找到一个使用gRPC将用户插入并查看到ouchbase数据库中的示例。请参考原始文件[2]以找到rpc方法。

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

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