Idea+protobuf-maven-plugin插件编译proto文件生成客户端和服务端代码

首先,定义test.proto文件,官网样例如下

syntax = "proto3";//请在非空非注释的第一行指定当前文件使用的是proto3的语法,默认proto2
package grpc;//package与java_package有些不同,java_package是定义编译生成的java文件所在的目录,而package是对应的java类的命名空间
option java_package = "com.diy.vcp.grpc.core";
option java_outer_classname = "HelloWorldServiceProto";//要生成Java类的名称
option java_multiple_files = true;//编译后会生成多个Message类,并没有被包含在HelloWorldServiceProto.java文件中,反之,生成单一HelloWorldServiceProto.java文件
//服务端接口类
service Greeter {
  rpc SayHello (HelloRequest) returns (HelloReply) {} //服务端接口方法
}
//请求参数 基于序号的协议字段映射,所以字段可以乱序,可缺段
message HelloRequest {
  string name = 1;
  string sex = 2;
}
//响应参数
message HelloReply {
  string message = 1;
}

然后,准备好maven工程mock-grpc,pom.xml添加编译proto文件的插件protobuf-maven-plugin

<build>
        <extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId><!--引入操作系统os设置的属性插件,否则${os.detected.classifier} 操作系统版本会找不到 -->
                <version>1.5.0.Final</version>
            </extension>
        </extensions>
        <plugins>
            <!--添加编译proto文件的编译程序和对应的编译插件-->
            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>0.5.1</version>
                <configuration>
                    <protocArtifact>com.google.protobuf:protoc:3.6.1:exe:${os.detected.classifier}</protocArtifact>
                    <pluginId>grpc-java</pluginId>
                    <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.14.0:exe:${os.detected.classifier}</pluginArtifact>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>compile-custom</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
 <dependency>
      <groupId>com.google.protobuf</groupId>
      <artifactId>protobuf-java</artifactId>
      <version>3.6.1</version>
    </dependency>
    <dependency>
      <groupId>io.grpc</groupId>
      <artifactId>grpc-protobuf</artifactId>
      <version>1.23.0</version>
    </dependency>
    <dependency>
      <groupId>io.grpc</groupId>
      <artifactId>grpc-stub</artifactId>
      <version>1.23.0</version>
    </dependency>
    <dependency>
      <groupId>io.grpc</groupId>
      <artifactId>grpc-netty</artifactId>
      <version>1.23.0</version>
    </dependency>
    <dependency>
      <groupId>io.grpc</groupId>
      <artifactId>grpc-netty-shaded</artifactId>
      <version>1.23.0</version>
    </dependency>
    <dependency>
      <groupId>io.grpc</groupId>
      <artifactId>grpc-okhttp</artifactId>
      <version>1.23.0</version>
    </dependency>

最后,可以在maven projects视图中选择 protobuf:compile命令执行

protobuf:compile //编译消息对象
protobuf:compile-custom //依赖消息对象,生成接口服务

也可以使用通过终端Terminal执行mvn命令,但是必须提前安装好protoc.exe

mvn install:install-file -DgroupId=com.google.protobuf -DartifactId=protoc -Dversion=3.0.0 -Dclassifier=windows-x86_64} -Dpackaging=exe -Dfile=D:\protobuf\protoc.exe
mvn protobuf:compile
mvn protobuf:compile-custom

在这里插入图片描述

import io.grpc.ClientInterceptor;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;

public class GrpcClient {
	private final ManagedChannel channel;//客户端与服务器的通信channel
	private SurfaceDataGrpc.SurfaceDataBlockingStub blockStub;//阻塞式客户端存根节点

	public RHTest(String host, int port) {
		ManagedChannelBuilder builder = ManagedChannelBuilder.forAddress(host, port).maxInboundMessageSize(2147483647).usePlaintext();
		this.channel = builder.intercept(new ClientInterceptor[]{}).build();//指定grpc服务器地址和端口初始化通信channel
		blockStub = SurfaceDataGrpc.newBlockingStub(channel);//根据通信channel初始化客户端存根节点
	}

	public RetArr2D getWinterolympicByTimeAndStaID(GetWOByTimeAndStaIDRequest request){
		GetSurfEleByTimeResponse reply = blockStub.getWinterolympicByTimeAndStaID(request);
		return reply.getData();
	}
}

参考:https://www.jianshu.com/p/eab66d996e19
https://developers.google.cn/protocol-buffers/docs/javatutorial
https://www.cnblogs.com/coding400/p/9397186.html

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值