1.最近由于目前公司微服务使用到grpc可是grpc一般使用在go语言上,因此国内基本没有什么关于grpc在java的完整教程,因此我也对grpc进行了一些资料查找和学习,只能说可以使用,原理就不深入了。
2.grpc官网
3.创建父工程,作为如果你学习到了grpc还没使用父工程进行项目创建,建议可以现去学习一下关于父工程统一定义版本等相关学习
4.创建工程引入依赖
父工程pom依赖
```xml
```go
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.minjiang</groupId>
<artifactId>SecondGrpc</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>grpcDemo</module>
</modules>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<grpc.version>1.34.1</grpc.version>
<protobuf.version>3.12.0</protobuf.version>
<protoc.version>3.12.0</protoc.version>
<maven.cpmplier.source>1.8</maven.cpmplier.source>
<maven.cpmplier.target>1.8</maven.cpmplier.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-bom</artifactId>
<version>${grpc.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
<version>${protobuf.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.6.2</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>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
<protoSourceRoot>src/main/resources/proto</protoSourceRoot>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
子工程pom.xml
```xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>SecondGrpc</artifactId>
<groupId>com.minjiang</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>grpcDemo</artifactId>
<dependencies>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
</dependency>
</dependencies>
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.6.2</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>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
<protoSourceRoot>src/main/resources/proto</protoSourceRoot>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
5.首先在resouce下创建你的proto文件夹(系统默认设置),并且创建.proto文件
6.add.proto文件
syntax = "proto3";
package grpcDemo;
option java_package = "a.b.c";
option java_outer_classname = "AddServiceProto";
option java_multiple_files = true;
service AddService{
rpc add(AddRequest) returns(AddReply){}
}
message AddRequest{
int32 a = 1;
int32 b = 2;
}
message AddReply{
int32 res = 1;
}
7.install生成文件
8.intsall生成会在target中出现
下图为我们主要生成的service
9.生成之后我们进行配置服务端和客户端的测试,如下图新建两个java文件
10.服务端文件
package a.b.c;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;
import java.io.IOException;
import static a.b.c.AddServiceGrpc.getAddMethod;
import static io.grpc.stub.ServerCalls.asyncUnimplementedUnaryCall;
/**
* @auther guannw
* @create 2021/6/28 21:05
*/
public class AddServer extends AddServiceGrpc.AddServiceImplBase {
public static void main(String[] args) throws IOException {
ServerBuilder.forPort(9999)//设置端口
.addService(new AddServer())// 添加服务
.build() //创建服务
.start();//开始服务
System.out.println("server start at 9999");
while (true){
}
}
public void add(AddRequest request, StreamObserver<AddReply> responseObserver) {
// asyncUnimplementedUnaryCall(getAddMethod(), responseObserver);
int res = myAdd(request.getA(),request.getB());
//返回值
responseObserver.onNext(AddReply.newBuilder().setRes(res).build());
//结束
responseObserver.onCompleted();
}
private int myAdd(int a , int b){
return a + b ;
}
}
代码解析如下图
11.客户端代码
package a.b.c;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
/**
* @auther guannw
* @create 2021/6/28 21:15
*/
public class AddClient {
ManagedChannel channel;
AddServiceGrpc.AddServiceBlockingStub stub;
public static void main(String[] args) {
int a = 12;
int b = 13;
AddClient addClient = new AddClient();
AddReply reply = addClient.stub.add(AddRequest.newBuilder().setA(a).setB(b).build());
System.out.println(reply.getRes());
}
public AddClient(){
channel = ManagedChannelBuilder
.forAddress("127.0.0.1",9999)
.usePlaintext() //使用纯文本类型
.build();
stub =
AddServiceGrpc.newBlockingStub(channel);
}
}
代码解析
12.测试先启动服务端在启动客户端即可,这里就不演示了