作者:手机用户2602885631_316 | 来源:互联网 | 2023-07-12 11:35
为了展示gRPC的工作原理,我们来构建一个客户端和相应的服务器,以公开一个简单的Hello World gRPC服务。
pom文件
<?xml version&#61;"1.0" encoding&#61;"UTF-8"?>
<project xmlns&#61;"http://maven.apache.org/POM/4.0.0" xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation&#61;"http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.7</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.espringboot</groupId><artifactId>grpc_demo</artifactId><version>0.0.1-SNAPSHOT</version><name>grpc_demo</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version><os-maven-plugin.version>1.6.1</os-maven-plugin.version><protobuf-maven-plugin.version>0.6.1</protobuf-maven-plugin.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>net.devh</groupId><artifactId>grpc-spring-boot-starter</artifactId><version>2.4.0.RELEASE</version></dependency></dependencies><build><extensions><extension><groupId>kr.motd.maven</groupId><artifactId>os-maven-plugin</artifactId><version>${os-maven-plugin.version}</version></extension></extensions><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin><!--grpc proto文件编译插件--><plugin><groupId>org.xolstice.maven.plugins</groupId><artifactId>protobuf-maven-plugin</artifactId><version>${protobuf-maven-plugin.version}</version><configuration><protocArtifact>com.google.protobuf:protoc:3.5.1-1:exe:${os.detected.classifier}</protocArtifact><pluginId>grpc-java</pluginId><pluginArtifact>io.grpc:protoc-gen-grpc-java:1.16.1:exe:${os.detected.classifier}</pluginArtifact></configuration><executions><execution><goals><goal>compile</goal><goal>compile-custom</goal></goals></execution></executions></plugin></plugins></build></project>
目录格式
注意&#xff1a;一定要把 proto文件夹 设置 资源文件&#xff0c;否则后续会报错
yaml文件
server:port: 8080
grpc:server:in-process-name: grpc# 修改服务端默认入参最大大小&#xff0c;默认值为4M &#xff0c;这里修改为20M 20*1024*1024max-inbound-message-size: 20971520# grpc 端口号port: 8989client:GLOBAL:negotiation-type: plaintext# 修改客户端端默认入参最大大小&#xff0c;默认值为4M &#xff0c;这里修改为20M 20*1024*1024max-inbound-message-size: 20971520# 客户端指定连接服务端地址address: &#39;static://127.0.0.1:8989&#39;
编写 proto文件
syntax &#61; "proto3";package net.devh.boot.grpc.example;option java_multiple_files &#61; true;
option java_package &#61; "net.devh.boot.grpc.examples.lib";
option java_outer_classname &#61; "HelloWorldProto";
service MyService {rpc SayHello (HelloRequest) returns (HelloReply) {}
}
message HelloRequest {string name &#61; 1;
}
message HelloReply {string message &#61; 1;
}
Maven编译
这个时候 maven会把 proto文件编译生成java文件
编写 服务端
import io.grpc.stub.StreamObserver;
import net.devh.boot.grpc.examples.lib.HelloReply;
import net.devh.boot.grpc.examples.lib.HelloRequest;
import net.devh.boot.grpc.examples.lib.MyServiceGrpc;
import net.devh.boot.grpc.server.service.GrpcService;&#64;GrpcService
public class MyServiceImpl extends MyServiceGrpc.MyServiceImplBase{&#64;Overridepublic void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {HelloReply reply &#61; HelloReply.newBuilder().setMessage("Hello &#61;&#61;> " &#43; request.getName()).build();responseObserver.onNext(reply);responseObserver.onCompleted();}}
编写客户端
&#64;Component
public class GrpcClientExample {&#64;GrpcClient("myService")private MyServiceGrpc.MyServiceBlockingStub myServiceStub;public String receiveGreeting(String name) {HelloRequest request &#61; HelloRequest.newBuilder().setName(name).build();return myServiceStub.sayHello(request).getMessage();}}
编写 controller
&#64;RequestMapping("/api/v1/grpc")
&#64;RestController
public class GrpcController {private final GrpcClientExample grpcClientExample;public GrpcController(GrpcClientExample grpcClientExample) {this.grpcClientExample &#61; grpcClientExample;}&#64;GetMapping("/get/{name}")public Object grpc(&#64;PathVariable("name") String name) {String names &#61; grpcClientExample.receiveGreeting(name);System.out.println("response.getMessage() &#61; " &#43; names);return names;}}
运行项目 测试 --controller - > 客户端 ->服务端
参考&#xff1a;
https://people.blog.csdn.net/article/details/121575516