Solon v2.7.5

grpc-solon-cloud-plugin

</> markdown
<dependency>
    <groupId>org.noear</groupId>
    <artifactId>grpc-solon-cloud-plugin</artifactId>
</dependency>

1、描述

分布式扩展插件。基于 grpc 适配的 rpc 插件。此项目可使用 Solon Cloud Discovery 插件包,作注册与发现用。

2、配置示例

  • pom.xml 增加注册与发现服务包:

Solon Cloud Discovery 选一个插件包。

  • app.yml 增加 grpc 配置:
# 服务端申明
server.grpc:
  port: 9081
  name: demo-grpc

# 客户端本地临时申明(或者使用 solon cloud discovery 服务包的配置)
solon.cloud.local:
  discovery:
    service:
      demo-grpc:
        - "grpc://localhost:9081"

3、代码应用

  • 服务端(实现接口服务)
@GrpcService
public class HelloImpl extends HelloHttpGrpc.HelloHttpImplBase{
    @Override
    public void sayHello(HelloHttpRequest request, StreamObserver<HelloHttpResponse> responseObserver) {
        String requestMsg = request.getMsg();
        String responseMsg = "hello " + requestMsg;
        HelloHttpResponse helloHttpResponse = HelloHttpResponse.newBuilder().setMsg(responseMsg).build();
        responseObserver.onNext(helloHttpResponse);
        responseObserver.onCompleted();
    }
}
  • 客户端应用
@Controller
public class TestController {

    @GrpcClient("demo-grpc")
    HelloHttpGrpc.HelloHttpBlockingStub helloHttp;

    @Mapping("/grpc/")
    public String test() {
        HelloHttpRequest request = HelloHttpRequest.newBuilder().setMsg("test").build();

        return helloHttp.sayHello(request).getMsg();
    }
}

4、代码演示

https://gitee.com/noear/solon-examples/tree/main/7.Solon-Remoting-Rpc/demo7013-rpc_grpc