创建 proto 文件
例如,创建helloworld.proto文件
//使用的 proto 协议版本
syntax = "proto3";
//包路径,可以和我们项目中设置的包一致,也可以自定义
package helloworld;
// Greeter 服务名,在后期生成java服务的时候,使用不到
service Greeter {
// 请求的方法名以及参数和返回值,可以写多个方法
rpc SayHello (HelloRequest) returns (HelloResponse) {}
rpc SayHi (HiRequest) returns (HiResponse) {}
}
// 参数定义,和在java中定义实体类一致
// 当然其定义的参数和返回值也可以是基本类型
message HelloRequest {
string name = 1;
}
// 定义参数的属性值的基本类型可以自行查询百度
message HiRequest {
string name = 1;
string sex = 2;
int32 calculate=3;
}
message HelloResponse {
string message = 1;
}
message HiResponse {
string message = 1;
string code = 2;
}
以下是使用maven方式生成java服务文件
为什么使用maven方式,因为使用linux开发的时候,好多exe文件不支持,顶多使用maven复杂一点而已
pom.xml,导入依赖,版本可以自己定义
<!-- grpc-netty -->
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>1.61.1</version>
</dependency>
<!-- grpc-protobuf -->
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.61.1</version>
</dependency>
<!-- grpc-stub -->
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.61.1</version>
</dependency>
<!-- apache.tomcat annotations-api -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>annotations-api</artifactId>
<version>6.0.53</version>
<scope>provided</scope>
</dependency>
<!-- protobuf-java -->
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>4.26.1</version>
</dependency>
添加 plugin
<plugin>
<groupId>io.github.ascopes</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<!-- 此处写入protobuf-maven-plugin的版本 -->
<version>1.1.3</version>
<!-- protobuf-maven-plugin 的配置信息 -->
<configuration>
<!-- 此处的版本为上面protobuf-java的版本 -->
<protocVersion>4.26.1</protocVersion>
<!-- proto文件默认位置: src/main/protobuf/helloworld.proto;生成位置:target/generated-sources/protobuf -->
<sourceDirectories>
<sourceDirectory>src/main/protobuf</sourceDirectory>
</sourceDirectories>
</configuration>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
若需要plugin的详细使用方式,请参考
protobuf-maven-plugin的 gitHub地址:https://github.com/ascopes/protobuf-maven-plugin
protobuf-maven-plugin的 使用介绍:https://ascopes.github.io/protobuf-maven-plugin
会生成两个文件 GreeterGrpc 和 Helloworld ,里面的代码无需手动修改
将target/generated-sources/protobuf中的文件转移到src下的包名下进行使用,需修改package的指向,若包名一致,则无需修改
使用方式:在使用maven编译好之后,生成了代码之后,则可以把plugin注释掉了
调用方式,创建GrpcClient类
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import protobuf.GreeterGrpc.GreeterBlockingStub;
public class GrpcClient {
public static void main(String[] args) {
//连接rpc服务器
ManagedChannel managedChannel = ManagedChannelBuilder.forAddress("localhost", 50054)
.usePlaintext().build();
//使用 BlockingStub 创建连接对象,当然,你也可以选择其他的连接方式,如:FutureStub/Stub
GreeterBlockingStub blockingStub = GreeterGrpc.newBlockingStub(managedChannel);
Helloworld.HelloResponse myResponse = blockingStub.sayHello(Helloworld.HelloRequest.newBuilder().setName("World").build());
System.out.println(myResponse.getMessage());
Helloworld.HiResponse response = blockingStub.sayHi(Helloworld.HiRequest.newBuilder().setName("张三").setSex("1").setCalculate(30).build());
System.out.println(response.getMessage());
}
}
但这样直接运行会报错,class io.grpc.netty.shaded.io.netty.util.internal.PlatformDependent0$7
cannot access class jdk.internal.misc.Unsafe (in module java.base) because module java.base does not export jdk.internal.misc to unnamed
需要在JVM参数中添加如下:
-Dio.grpc.netty.shaded.io.netty.tryReflectionSetAccessible=true
--add-opens java.base/java.nio=ALL-UNNAMED
--add-opens java.base/jdk.internal.misc=ALL-UNNAMED