Spring Boot+Nacos+gRPC,一个区别于 OpenFeign 的微服务通信方案!( 二 )

很简单的一个 HelloService 服务 。
3.2 grpc-servergrpc-server 则是我们的服务端,这是一个 Spring Boot 工程,项目依赖如下:
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.7</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>org.javaboy</groupId><artifactId>grpc-server</artifactId><version>0.0.1-SNAPSHOT</version><name>grpc-server</name><description>grpc-server</description><properties><java.version>17</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.javaboy</groupId><artifactId>grpc-api</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>.NET.devh</groupId><artifactId>grpc-server-spring-boot-starter</artifactId><version>2.14.0.RELEASE</version></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>2021.0.5.0</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>由于第三方库 grpc-server-spring-boot-starter 在支持 Spring Boot3 上还有一些瑕疵,因此我这里使用了 Spring Boot2.7.7 这个版本 。
这里需要注意的是就是添加了 gRPC 的依赖 grpc-server-spring-boot-starter 和 nacos 的依赖 。其他都是常规配置 。
接下来我们来在服务端提供 gRPC 方法的实现:
@GrpcServicepublic class HelloServiceImpl extends HelloServiceGrpc.HelloServiceImplBase {@Overridepublic void sayHello(StringValue request, StreamObserver<StringValue> responseobserver) {String value = https://www.isolves.com/it/cxkf/kj/2023-04-04/request.getValue();responseObserver.onNext(StringValue.newBuilder().setValue("hello " + value).build());responseObserver.onCompleted();}}小伙伴们看到,通过 @GrpcService 注解去标记我们的一个服务即可 。
最后,在 application.yaml 中进行配置,将当前服务注册到 nacos 容器中:
grpc:server:port: 9099spring:cloud:nacos:discovery:server-addr: hc.javaboy.org:8848username: nacospassword: nacosenabled: trueapplication:name: grpc_serverOK,服务端搞定 。
3.3 grpc-client最后再来看看客户端 。
先来看依赖:
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.7</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>org.javaboy</groupId><artifactId>grpc-client</artifactId><version>0.0.1-SNAPSHOT</version><name>grpc-client</name><description>grpc-client</description><properties><java.version>17</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.javaboy</groupId><artifactId>grpc-api</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>net.devh</groupId><artifactId>grpc-client-spring-boot-starter</artifactId><version>2.14.0.RELEASE</version></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>2021.0.5.0</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>


推荐阅读