有两种方式:**http:网络传输协议**, **rpc(Remote Produce Call)远程过程调用**
两者区别: RPC 是根据语言API来定义,不是根据网络的应用定义
主要有以下几个过程
(1) 通过.proto文件定义传输的接口和消息体。
(2) 通过protocol编译器生成server端和client端的stub程序。
(3) 将请求封装成HTTP2的Stream。
(4) 通过Channel作为数据通信通道使用Socket进行数据传输。
springboot : 2.1.4.RELEASE
JDK1.8
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
com.lrbj
grpc-springboot-demo
0.0.1-SNAPSHOT
grpc-springboot-demo
Demo project for Spring Boot
pom
grpc-lib
grpc-server
grpc-client
UTF-8
UTF-8
1.8
2.1.4.RELEASE
2.0.1.RELEASE
com.lrbj
grpc-lib
${project.version}
net.devh
grpc-client-spring-boot-starter
${net-devh-grpc.version}
net.devh
grpc-server-spring-boot-starter
${net-devh-grpc.version}
syntax = "proto3";
option java_package = "com.lrbj.grpc.lib";
// The greeting service definition.
service Greeter{
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {
}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
com.lrbj
grpc-springboot-demo
0.0.1-SNAPSHOT
grpc-springboot-demo
Demo project for Spring Boot
pom
grpc-lib
grpc-server
grpc-client
UTF-8
UTF-8
1.8
2.1.4.RELEASE
2.0.1.RELEASE
com.lrbj
grpc-lib
${project.version}
net.devh
grpc-client-spring-boot-starter
${net-devh-grpc.version}
net.devh
grpc-server-spring-boot-starter
${net-devh-grpc.version}
根据pom文件配置在src/main/java目录下生成指定文件
4.0.0
com.lrbj
grpc-springboot-demo
0.0.1-SNAPSHOT
grpc-server
grpc-server
jar
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.projectlombok
lombok
com.lrbj
grpc-lib
net.devh
grpc-server-spring-boot-starter
org.springframework.boot
spring-boot-maven-plugin
grpc.sever.address 默认值为"0.0.0.0"
spring:
application:
name: local-grpc-server
grpc:
server:
port: 8002
@Slf4j
@GrpcService(GreeterOuterClass.class)
public class GreeterService extends GreeterGrpc.GreeterImplBase {
@Override
public void sayHello(GreeterOuterClass.HelloRequest request, StreamObserver responseObserver) {
String message = "Hello " + request.getName();
final GreeterOuterClass.HelloReply.Builder replyBuilder = GreeterOuterClass.HelloReply.newBuilder().setMessage(message);
responseObserver.onNext(replyBuilder.build());
responseObserver.onCompleted();
log.info("Returning " + message);
}
}
4.0.0
com.lrbj
grpc-springboot-demo
0.0.1-SNAPSHOT
grpc-client
grpc-client
jar
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-web
com.lrbj
grpc-lib
net.devh
grpc-client-spring-boot-starter
org.springframework.boot
spring-boot-starter-web
org.projectlombok
lombok
org.springframework.boot
spring-boot-maven-plugin
server:
port: 8003
spring:
application:
name: local-grpc-client
grpc:
client:
local-grpc-server:
port: 8002
enableKeepAlive: true
keepAliveWithoutCalls: true
@Service
public class GrpcClientService {
//两种获取Channel方式
//方法一
@GrpcClient("local-grpc-server")
private Channel serverChannel;
public String sendMessage(String name) {
GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(serverChannel);
GreeterOuterClass.HelloReply response = stub.sayHello(GreeterOuterClass.HelloRequest.newBuilder().setName(name).build());
return response.getMessage();
}
@RestController
public class GrpcClientController {
@Autowired
private GrpcClientService grpcClientService;
@RequestMapping("/")
public String printMessage(@RequestParam(defaultValue = "Susan") String name) {
return grpcClientService.sendMessage(name);
}
例子源码地址:gitdemo
参考链接:
https://grpc.io/docs/quickstart/java/
https://www.cnblogs.com/tianzhiyi/p/6120909.html
https://blog.csdn.net/alinyua/article/details/83030149
http://jia-shun.cn/2018/08/12/gRPC/