Wer hat Beispiele oder Gedanken zur Verwendung von gRPC zusammen mit Spring Boot?
Wenn es für Sie immer noch relevant ist, habe ich gRPC Spring-Boot-Starter hier erstellt.
grpc-spring-boot-starter konfiguriert und führt den eingebetteten gRPC-Server mit @ GRpcService-fähigen Beans automatisch aus.
Das einfachste Beispiel:
@GRpcService(grpcServiceOuterClass = GreeterGrpc.class)
public static class GreeterService implements GreeterGrpc.Greeter {
@Override
public void sayHello(GreeterOuterClass.HelloRequest request, StreamObserver<GreeterOuterClass.HelloReply> responseObserver) {
// omitted
}
}
Es gibt auch ein Beispiel, wie Sie den Starter mit Eureka in die Projektdatei README integrieren können.
Ab https://spring.io/blog/2015/03/22/using-google-protocol-buffers-with-spring-mvc-based-rest-services , dann
werfen Sie einen Blick auf SPR-13589 ProtobufHttpMessageConverter-Unterstützung für Protobuf 3.0.0-beta4 und verwandte SPR-13203 HttpMessageConverter basierend auf Protostuff-Bibliothek
Proto3 wird im Frühling 5 unterstützt. Da sich das Programm gerade in der Entwicklung befindet, wird empfohlen, zu stimmen und das für ihn wichtige Thema zu erheben.
https://github.com/yidongnan/grpc-spring-boot-starter
Im 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();
}
}
Im Kunden
@GrpcClient("gRPC server name")
private Channel serverChannel;
GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(serverChannel);
HelloReply response = stub.sayHello(HelloRequest.newBuilder().setName(name).build());
Hier verwende ich gRpc und Eureka zur Kommunikation. Dieses Projekt basiert auf Spring-Boot
https://github.com/WThamira/grpc-spring-boot
zusätzlich können Sie sich auch als Konsul registrieren. volles Beispiel in diesem Repo
https://github.com/WThamira/gRpc-spring-boot-example
diese Maven-Abhängigkeit hilft bei 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>
und brauchen Plugin unten
<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>
Wenn Sie eine gRPC client -Bibliothek benötigen, d. H. Konsumstubs, besuchen Sie meine Bibliothek https://github.com/sfcodes/grpc-client-spring-boot .
Diese Bibliothek durchsucht Ihren Klassenpfad automatisch, findet alle gRPC-Stub-Klassen, instanziiert sie und registriert sie als Beans mit dem ApplicationContext. So können Sie ganz einfach @Autowire
en und injizieren, wie Sie es auch bei anderen Springbohnen tun würden. Zum Beispiel:
@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();
}
}
Für die gRPC-Bibliothek server würde ich auch LogNet/grpc-spring-boot-starter
empfehlen.