将 Spring Boot 与 gRPC 和 Protobuf 一起使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31938242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:42:47  来源:igfitidea点击:

Using Spring Boot together with gRPC and Protobuf

springspring-bootprotocol-buffersgrpc

提问by Markus

Anyone having any examples or thoughts using gRPC together with Spring Boot?

有人有将 gRPC 与 Spring Boot 结合使用的任何示例或想法吗?

回答by Alexander.Furer

If it's still relevant for you, I've created gRPC spring-boot-starter here.

如果它仍然与您相关,我已经在这里创建了 gRPC spring-boot-starter 。

grpc-spring-boot-starterauto-configures and runs the embedded gRPC server with @GRpcService-enabledbeans.

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

The simplest example :

最简单的例子:

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

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

}

There is also an example of how to integrate the starter with Eureka in project's README file.

在项目的 README 文件中还有一个关于如何将 starter 与 Eureka 集成的示例。

回答by Paul Verest

Starting from https://spring.io/blog/2015/03/22/using-google-protocol-buffers-with-spring-mvc-based-rest-services, then
take a look at SPR-13589 ProtobufHttpMessageConverter support for protobuf 3.0.0-beta4and related SPR-13203 HttpMessageConverter based on Protostuff library

https://spring.io/blog/2015/03/22/using-google-protocol-buffers-with-spring-mvc-based-rest-services开始,然后
查看 SPR-13589 ProtobufHttpMessageConverter 对 protobuf 的支持3.0.0- beta4和相关的SPR-13203 HttpMessageConverter 基于 Protostuff 库

That is some support for proto3 is coming in Spring 5. As it is under development one is encouraged to vote and raise what is important for their project.

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

回答by Michael Chen

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

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

In server

在服务器

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

In client

在客户端

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

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

回答by Semyon Fishman

If you need a gRPC clientlibrary, i.e. consume stubs, check out my library https://github.com/sfcodes/grpc-client-spring-boot

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

This library will automatically scan your classpath, find all gRPC stub classes, instantiate them, and register them as beans with the ApplicationContext; allowing you to easily @Autowireand inject them just like you would any other Spring bean. For example:

这个库会自动扫描你的类路径,找到所有 gRPC 存根类,实例化它们,并将它们注册为 ApplicationContext 的 bean;允许您@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();
    }
}

For gRPC serverlibrary, I'd also recommend LogNet/grpc-spring-boot-starter.

对于 gRPC服务器库,我还推荐LogNet/grpc-spring-boot-starter.

回答by wthamira

In here I use gRpc and eureka to communication. This project based on Spring-boot

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

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

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

additionally you canuse register as consul also. full example in this repo

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

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

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

this maven dependency help to gRpc

这个 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>

and need plugin show in below

并需要插件显示在下面

       <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>