Mar*_*kus 35 spring protocol-buffers spring-boot grpc
有没有任何使用gRPC和Spring Boot的例子或想法的人?
Ale*_*rer 31
如果仍然相关,为了你,我已经创建GRPC弹簧引导首发位置.
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
}
}
Run Code Online (Sandbox Code Playgroud)
还有一个如何在项目的README文件中将启动器与Eureka集成的示例.
小智 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();
}
}
Run Code Online (Sandbox Code Playgroud)
在客户端
@GrpcClient("gRPC server name")
private Channel serverChannel;
GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(serverChannel);
HelloReply response = stub.sayHello(HelloRequest.newBuilder().setName(name).build());
Run Code Online (Sandbox Code Playgroud)
小智 5
如果您需要 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();
}
}
Run Code Online (Sandbox Code Playgroud)
对于 gRPC服务器库,我还推荐LogNet/grpc-spring-boot-starter.
| 归档时间: |
|
| 查看次数: |
26422 次 |
| 最近记录: |