工程项目说明
- rpc-grpc-api 存放的是protobuf生成的通用代码
- rpc-grpc-boot-server 是grpc服务端,也是服务的提供者
- rpc-grpc-boot-client 是grpc客户端,rpc服务的调用者
rpc-grpc-api
本项目的模块,与此下文章中的一致,可以参考此文章:Java Maven工程 gRPC demo - 掘金 (juejin.cn)
rpc-grpc-boot-server
使用的是springboot2.7.18版本,还需引入对应的第三方grpc-spring-boot-starter依赖。此工程的依赖如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.7.18</version>
</dependency>
<dependency>
<groupId>io.github.lognet</groupId>
<artifactId>grpc-spring-boot-starter</artifactId>
<version>4.9.1</version>
</dependency>
<dependency>
<groupId>com.rpc</groupId>
<artifactId>rpc-grpc-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
注意:此grpc-spring-boot-starter依赖中的grpc相关的版本与rpc-grpc-api中的版本一致,否则可能会出现问题。此外,此项目是由maven项目改造成springboot项目还需要自己编写springboot启动类。 springboot启动类如下:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BootGrpcServerApplication {
public static void main(String[] args) {
SpringApplication.run(BootGrpcServerApplication.class,args);
}
}
编写类,实现rpc-grpc-api中的接口,并增加@GRpcService注册,代表者此类已经成了一个grpc的服务,只需在application.yml中进行相应的配置
编写服务代码如下:
import org.lognet.springboot.grpc.GRpcService;
// 通过注解的方式将服务提供者注入到Spring的IOC容器中
@GRpcService
public class ProviderService extends HelloGrpcServiceGrpc.HelloGrpcServiceImplBase {
@Override
public void helloGrpc(HelloProto.HelloRequest request, StreamObserver<HelloProto.HelloResponse> responseObserver) {
// 从客户端获取到的参数信息
String name = request.getName();
System.out.println("springboot版本,从客户端中获取到的参数为:" + name);
// 构建返回
responseObserver.onNext(HelloProto.HelloResponse.newBuilder().setResult("springboot版本,从服务器端返回的数据").build());
responseObserver.onCompleted();
}
}
在resources目录下,新建application.yml配置文件,并进行如下的配置:
# 配置工程的名称
spring:
application:
name: grpc-boot-server
# grpc 启动的服务地址
grpc:
port: 9000
rpc-grpc-boot-client
和创建rpc-grpc-boot-server工程类似,创建项目、添加依赖、编写代码、编写配置文件4步
创建项目就省略了,直接进入到添加依赖步骤,依赖如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.7.18</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.18</version>
</dependency>
<!--grpc-client 客户端-->
<dependency>
<groupId>net.devh</groupId>
<artifactId>grpc-client-spring-boot-starter</artifactId>
<version>2.14.0.RELEASE</version>
</dependency>
<!--依赖共通代码-->
<dependency>
<groupId>com.rpc</groupId>
<artifactId>rpc-grpc-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
同样创建springboot启动类,代码如下:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BootGrpcClientApplication {
public static void main(String[] args) {
SpringApplication.run(BootGrpcClientApplication.class,args);
}
}
客户端使用spring-web 请求来客户端,编写的controller代码如下:
import com.grpc.HelloGrpcServiceGrpc;
import com.grpc.HelloProto;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class IndexController {
// 使用grpc-client注解,括号的内容为服务端配置的项目名称
@GrpcClient("grpc-boot-server")
private HelloGrpcServiceGrpc.HelloGrpcServiceBlockingStub helloGrpcServiceBlockingStub;
@GetMapping("/index")
public String grpcIndex(String name){
System.out.println("开始grpc调用-----------");
HelloProto.HelloResponse response = helloGrpcServiceBlockingStub.helloGrpc(HelloProto.HelloRequest.newBuilder().setName(name).build());
System.out.println("获取到grpc方法调用的结果:" + response.getResult());
return response.getResult();
}
}
编写的配置文件如下:
# client 名
spring:
application:
name: boot-grpc-client
# tomcat 端口号
server:
port: 8000
# grpc 客户端配置
grpc:
client:
GLOBAL:
#grpc服务端的提供者
address: "static://127.0.0.1:9000"
#设置客户端的访问模式,默认为TSL连接,而服务端默认的是PLAINTEXT连接
negotiation-type: plaintext
启动rpc-grpc-boot-server和rpc-grpc-client两项目。如果启动成功之后,没有出现任何报错,则从浏览器或者其它发送http请求的工具发送 http://localhost:8000/index?name=张三 get请求,查看返回和后台日志。 调用成功之后的截图如下:
请求结果
rpc-grpc-boot-client 客户端日志
rpc-grpc-boot-server 服务端日志