2015-08-11 51 views
19

Bất cứ ai có bất kỳ ví dụ hoặc suy nghĩ nào sử dụng gRPC cùng với Spring Boot?Sử dụng Spring Boot cùng với gRPC và Protobuf

+3

https://spring.io/blog/2015/03/22/sử dụng-google-protocol-buffer-với-spring-mvc-based-rest-services có lẽ là một bài đọc hay. –

+0

Đúng, đã phát hiện ra điều đó. Nhưng tôi muốn biết nếu có ai đã gắn kết điều này với các định nghĩa dịch vụ protobuf không? – Markus

+0

Tìm kiếm một ví dụ quá –

Trả lời

16

Nếu nó vẫn phù hợp với bạn, tôi đã tạo gRPC spring-boot-starter here.

grpc xuân-boot-khởi động tự động cấu hình, và chạy máy chủ gRPC nhúng với @ GRpcService-enabled đậu.

Ví dụ đơn giản nhất:

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

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

} 

Ngoài ra còn có một ví dụ về làm thế nào để tích hợp khởi động với Eureka trong tập tin README của dự án.

2

Bắt đầu từ https://spring.io/blog/2015/03/22/using-google-protocol-buffers-with-spring-mvc-based-rest-services, sau đó
hãy nhìn vào SPR-13589 ProtobufHttpMessageConverter support for protobuf 3.0.0-beta4 và liên quan SPR-13203 HttpMessageConverter based on Protostuff library

Đó là một số hỗ trợ cho proto3 đang đến trong mùa xuân 5. Vì nó đang được phát triển là một trong những khuyến khích để bỏ phiếu và nâng cao những gì là quan trọng cho dự án của họ.

+0

https://github.com/WThamira/gRpc-spring-boot-example là ví dụ đúng cho trường hợp đó tôi nghĩ – wthamira

0

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

Trong máy chủ

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

Trong client

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

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

Tại đây tôi sử dụng gRpc và eureka để giao tiếp. Dự án này dựa trên mùa xuân-boot

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

thêm bạn canuse đăng ký làm lãnh sự cũng có. ví dụ đầy đủ trong repo này

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

maven này phụ thuộc vào sự giúp đỡ 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> 

và cần chương trình plugin trong dưới

 <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> 
Các vấn đề liên quan