在Mac下IDEA安裝并使用protobuf方式(Java)
安裝插件

引入依賴
<dependencies>
<!--這個(gè)是netty的依賴包,可以不引用-->
<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包下新建一個(gè)proto包,然后把proto文件放包此包里

syntax = "proto3"; //版本
option java_outer_classname = "StudentPOJO"; //生成的外部類名,同時(shí)也是文件名
//protobuf 使用massage管理數(shù)據(jù)
//會(huì)在 StudentPOJO 外部類生成一個(gè)內(nèi)部類 Student,這個(gè)才是真正發(fā)送的POJO對(duì)象
message Student {
int32 id = 1; //Student類中有一個(gè)屬性名為id,類型為int32(protobuf類型),1表示屬性序號(hào),不是值
string name = 2;
}
proto類型


生成文件



新建對(duì)象
把生成的java文件放到項(xiàng)目的pojo包里,然后就可以新建對(duì)象了。
StudentPOJO.Student stu1 = StudentPOJO.Student.newBuilder().setId(1).setName("張三").build();
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Myeclipse鏈接Oracle等數(shù)據(jù)庫(kù)時(shí)lo exception: The Network Adapter coul
今天小編就為大家分享一篇關(guān)于Myeclipse鏈接Oracle等數(shù)據(jù)庫(kù)時(shí)lo exception: The Network Adapter could not establish the connection,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03
Hibernate Validation自定義注解校驗(yàn)的實(shí)現(xiàn)
這篇文章主要介紹了Hibernate Validation自定義注解校驗(yàn)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Java設(shè)計(jì)模式之靜態(tài)工廠模式詳解
這篇文章主要介紹了Java設(shè)計(jì)模式之靜態(tài)工廠模式,簡(jiǎn)單說明了靜態(tài)工廠模式的概念、原理、實(shí)現(xiàn)與使用方法,需要的朋友可以參考下2017-09-09
Mybatis之a(chǎn)ssociation和collection用法
這篇文章主要介紹了Mybatis之a(chǎn)ssociation和collection用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
java并發(fā)學(xué)習(xí)-CountDownLatch實(shí)現(xiàn)原理全面講解
這篇文章主要介紹了java并發(fā)學(xué)習(xí)-CountDownLatch實(shí)現(xiàn)原理全面講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2021-02-02
如何實(shí)現(xiàn)java Iterator迭代器功能
這篇文章主要介紹了如何實(shí)現(xiàn)java Iterator迭代器功能,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
Spring Cloud微服務(wù)之間調(diào)用Dubbo的詳細(xì)過程
SpringCloudAlibabaDubbo是阿里巴巴開源的Dubbo與SpringCloud的深度集成方案,本文介紹Spring Cloud微服務(wù)之間調(diào)用Dubbo的詳細(xì)過程,感興趣的朋友跟隨小編一起看看吧2026-02-02

