版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/danielzhou888/article/details/80221052
敬请关注博客,后期不断更新优质博文,谢谢
protocol buffer生成java代码:
*******************************************************************************手动生成**************************************************************************************
一、安装maven:
1、配置环境变量
2、验证是否成功:mvn --version
二、安装protocol3
1、下载
protoc-3.5.0-alpha-2-windows-x86_64.exe
protoc-gen-grpc-java-1.0.1-windows-x86_64.exe
2、配置到Path环境变量中:
3、验证是否成功:
DOS执行protoc --version即可
三、根据.proto文件生成java文件
1、生成基本java文件
Linux生成基本文件命令:./bin/protoc --java_out=./javafile/ ./javafile/protofile/person.proto
windows生成基本文件命令:./bin/protoc --java_out=./javafile/ ./javafile/protofile/person.proto (protoc --java_out=./javafile/ ./javafile/protofile/person.proto )
2、生成服务类java文件
在DOS中执行如下命令生成GreeterGrpc服务类文件
protoc --plugin=protoc-gen-grpc-java=D:\Java工具\gRPC\protoc-gen-grpc-java-1.0.1-windows-x86_64.exe --grpc-java_out=java --proto_path=protofile protofile/
helloworld.proto
四、maven项目集成protoc buffer
生成client端和server端代码
使用protocol编译器protoc和指定的grpc的一个java插件。
使用maven的protobuf插件生成相关代码
命令行: protoc --java_out=./java/ ./proto/hello.proto
生成grpc通讯代码
protoc --plugin=protoc-gen-grpc-java=/Users/liuyu9/Documents/personal/golang/src/github.com/grpc-java/compiler/build/exe/java_plugin/protoc-gen-grpc-java --grpc-java_out=./java ./proto/hello.proto
文件列表如下:
# GreeterGrpc包含服务端和客户端要实现的一些基本类-rw-r--r-- 1 root staff 7352 Oct 25 18:48 GreeterGrpc.java
# 包含了protocol buffer发送、序列化、反序列化我们请求和响应代码-rw-r--r-- 1 root staff 16207 Oct 25 18:53 HelloReply.java-rw-r--r-- 1 root staff 501 Oct 25 18:53 HelloReplyOrBuilder.java-rw-r--r-- 1 root staff 16192 Oct 25 18:53 HelloRequest.java-rw-r--r-- 1 root staff 493 Oct 25 18:53 HelloRequestOrBuilder.java-rw-r--r-- 1 root staff 3011 Oct 25 18:53 HelloWorldProto.java
*******************************************************************************手动生成**************************************************************************************
*******************************************************************************IDE--Maven项目生成**************************************************************************************
1,配置好pom依赖,直接clean,install即可在target中找到生成的普通类文件和协议文件
p
om.xml:
<!-- grpc -->
<!--<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<version>1.11.0</version>
</dependency>-->
<dependency>
<groupId>io.grpc </groupId>
<artifactId>grpc-protobuf </artifactId>
<version>1.11.0 </version>
</dependency>
<dependency>
<groupId>io.grpc </groupId>
<artifactId>grpc-stub </artifactId>
<version>1.11.0 </version>
</dependency>
<dependency>
<groupId>com.google.protobuf </groupId>
<artifactId>protobuf-java </artifactId>
<version>3.5.0 </version>
</dependency>
<!-- grpc -->
<!--<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<version>1.11.0</version>
</dependency>-->
<dependency>
<groupId>io.grpc </groupId>
<artifactId>grpc-protobuf </artifactId>
<version>1.11.0 </version>
</dependency>
<dependency>
<groupId>io.grpc </groupId>
<artifactId>grpc-stub </artifactId>
<version>1.11.0 </version>
</dependency>
<dependency>
<groupId>com.google.protobuf </groupId>
<artifactId>protobuf-java </artifactId>
<version>3.5.0 </version>
</dependency>
<!-- grpc -->
<plugin>
<groupId>org.xolstice.maven.plugins </groupId>
<artifactId>protobuf-maven-plugin </artifactId>
<version>0.5.0 </version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.5.0:exe:${os.detected.classifier} </protocArtifact>
<pluginId>grpc-java </pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.0.1:exe:${os.detected.classifier} </pluginArtifact>
</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>
<version>2.3.2 </version>
<configuration>
<source>1.8 </source>
<target>1.8 </target>
</configuration>
</plugin>
<groupId>org.xolstice.maven.plugins </groupId>
<artifactId>protobuf-maven-plugin </artifactId>
<version>0.5.0 </version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.5.0:exe:${os.detected.classifier} </protocArtifact>
<pluginId>grpc-java </pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.0.1:exe:${os.detected.classifier} </pluginArtifact>
</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>
<version>2.3.2 </version>
<configuration>
<source>1.8 </source>
<target>1.8 </target>
</configuration>
</plugin>
*******************************************************************************IDE--Maven项目生成**************************************************************************************