Using Spring Boot together with gRPC and Protobuf

后端 未结 6 1925
傲寒
傲寒 2021-01-31 02:30

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

6条回答
  •  情深已故
    2021-01-31 02:57

    If you need a gRPC client library, i.e. consume stubs, check out my library 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 @Autowire and inject them just like you would any other Spring bean. For example:

    @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 server library, I'd also recommend LogNet/grpc-spring-boot-starter.

提交回复
热议问题