将Google的gRPC协议proto文件自动生成java服务

本文介绍了如何创建一个简单的HelloWorld.proto文件,定义了服务接口和消息类型,并指导如何使用Maven和protobuf-maven-plugin生成Java服务代码。还提到在Linux环境下如何解决JVM兼容性问题。
摘要由CSDN通过智能技术生成

创建 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
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值