文章目录
安装插件引入依赖编写Student.proto文件proto类型生成文件新建对象
安装插件
引入依赖
<dependencies>
<dependency>
<groupId>io.netty
</groupId>
<artifactId>netty-all
</artifactId>
<version>4.1.20.Final
</version>
</dependency>
<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>
</dependencies>
<build>
<extensions>
<extension>
<groupId>kr.motd.maven
</groupId>
<artifactId>os-maven-plugin
</artifactId>
<version>1.5.0.Final
</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins
</groupId>
<artifactId>protobuf-maven-plugin
</artifactId>
<version>0.5.0
</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.23.0:exe:${os.detected.classifier}
</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile
</goal>
<goal>compile-custom
</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
编写Student.proto文件
需要在main包下新建一个proto包,然后把proto文件放包此包里
syntax
= "proto3";
option java_outer_classname
= "StudentPOJO";
message Student
{
int32 id
= 1;
string name
= 2;
}
proto类型
生成文件
新建对象
把生成的java文件放到项目的pojo包里,然后就可以新建对象了。
StudentPOJO
.Student stu1
= StudentPOJO
.Student
.newBuilder().setId(1).setName("张三").build();