說明:
GRPC是什么?
首先我們知道 RPC是遠程過程調用。
而GRPC是RPC的一種實現。
那么為什么要用GRPC呢?
因為它支持跨語言的開發,換句話說,大家都用過FeignRPC,尤其在spring cloud中。
然而它只支持java語言,而作為微服務,可能有很多其他的服務不是java開發的。因此需要滿足這個需求,就需要一個跨語言的RPC,所以就會考慮使用GRPC
好了,下面進入正題
直接上代碼。
我們做一個Service和一個Client 進行交互。
一,Service端
1,POM
注意:
grpc-spring-boot-starter
os-maven-plugin
protobuf-maven-plugin
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="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.5.6</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.leadtrans</groupId> <artifactId>report</artifactId> <version>1.6.0</version> <name>report</name> <description>Demo project for Spring Boot</description> <properties> <java.version>11</java.version> <spring-cloud.version>2020.0.4</spring-cloud.version> <!-- GRPC --> <grpc-spring-boot-starter.version>2.3.2</grpc-spring-boot-starter.version> <os-maven-plugin.version>1.6.0</os-maven-plugin.version> <protobuf-maven-plugin.version>0.5.1</protobuf-maven-plugin.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.78</version> </dependency> <!-- GRPC --> <dependency> <groupId>org.lognet</groupId> <artifactId>grpc-spring-boot-starter</artifactId> <version>${grpc-spring-boot-starter.version}</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <extensions> <!-- os-maven-plugin --> <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> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> <!-- protobuf-maven-plugin --> <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.11.0:exe:${os.detected.classifier}</pluginArtifact> <outputDirectory>${project.build.sourceDirectory}</outputDirectory> <clearOutputDirectory>false</clearOutputDirectory> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>compile-custom</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
2,創建Proto
PS:Proto文件夾,必須和JAVA同級!
HelloWorld.protp
PS:請使用 option java_package="xxxx",
而不是 package xxxx
package xxxx 會導致你引用其他包時,會報錯
syntax = "proto3";
option java_multiple_files = true;
option java_package="com.example.test.helloworld";
//請求
message Request {
double num1 = 1;
double num2 = 2;
OperateType opType = 3;
}
//操作類型
enum OperateType {
Addition = 0;
Division = 1;
Multiplication = 2;
Subtraction = 3;
}
message Response {
double result = 1;
}
//定義服務
service Operate {
rpc Calculate (Request) returns (Response);
}
3,執行命令,生成文件
mvn clean install
或者點擊
可以看到生成了文件:
4,實現接口
為了簡單演示,這里就直接設置Response.Result=2
OperateImpl
@GRpcService public class OperateImpl extends OperateGrpc.OperateImplBase { @Override public void calculate(Request request, StreamObserver<Response> responseObserver) { Response response=Response.newBuilder().setResult(2).build(); System.out.println(response); responseObserver.onNext(response); responseObserver.onCompleted(); } }
二,Client端
1,POM(同Service端)
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="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.5.6</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>test1</artifactId> <version>1.0</version> <name>test1</name> <description>Demo project for Spring Boot</description> <properties> <java.version>11</java.version> <spring-cloud.version>2021.0.0</spring-cloud.version> <grpc-spring-boot-starter.version>2.3.2</grpc-spring-boot-starter.version> <os-maven-plugin.version>1.6.0</os-maven-plugin.version> <protobuf-maven-plugin.version>0.5.1</protobuf-maven-plugin.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- grpc --> <dependency> <groupId>org.lognet</groupId> <artifactId>grpc-spring-boot-starter</artifactId> <version>${grpc-spring-boot-starter.version}</version> </dependency> </dependencies> <build> <extensions> <!-- os-maven-plugin --> <extension> <groupId>kr.motd.maven</groupId> <artifactId>os-maven-plugin</artifactId> <version>${os-maven-plugin.version}</version> </extension> </extensions> <plugins> <!-- spring-boot-maven-plugin --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!-- protobuf-maven-plugin --> <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.11.0:exe:${os.detected.classifier}</pluginArtifact> <outputDirectory>${project.build.sourceDirectory}</outputDirectory> <clearOutputDirectory>false</clearOutputDirectory> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>compile-custom</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
2,拷貝Proto
和java同級目錄
3,執行命令
mvn clean install
4,調用方法
CalculateService
其中 main方法,是測試方法。
GRPC啟動的默認端口是6565,在main中設置了。
public class CalculateService { private final ManagedChannel channel; private final OperateGrpc.OperateBlockingStub blockingStub; private CalculateService(ManagedChannel channel) { this.channel = channel; blockingStub = OperateGrpc.newBlockingStub(channel); } public CalculateService(String host, int port) { this(ManagedChannelBuilder.forAddress(host, port) .usePlaintext() .build()); } public void shutdown() throws InterruptedException { channel.shutdown().awaitTermination(5, TimeUnit.SECONDS); } public float operate(float num1, float num2, OperateType operateType) { Request request = Request.newBuilder().setNum1(num1).setNum2(num2).setOpType(operateType).build(); Response response = blockingStub.calculate(request); return (float) response.getResult(); } public static void main(String[] args) { try { CalculateService service = new CalculateService("localhost", 6565); System.out.println(service.operate(100, 0, OperateType.Division)); service.shutdown(); } catch (Exception e) { System.out.println(e); } } }
三,結果