54

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

4

7 回答 7

37

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

grpc-spring-boot-starter使用支持@GRpcService 的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 集成的示例。

于 2016-02-01T09:53:30.770 回答
5

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());
于 2017-02-10T01:47:01.437 回答
4

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

该库将自动扫描您的类路径,查找所有 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();
    }
}

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

于 2018-10-28T23:58:45.197 回答
3

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

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

于 2016-11-11T06:51:03.023 回答
0

在这里我使用 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>
于 2017-03-10T15:07:50.067 回答
0

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

于 2020-12-18T15:54:54.157 回答
0

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

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

于 2021-09-13T21:08:09.830 回答