须要安装Protobuf Support插件java
依次点击Intellij中的“File”-->"Settings"-->"Plugins"-->"Browse repositories",以下所示:maven
输入Protobuf,以下所示ide
安装完后,重启Intellij IDEA,查看.proto文件,会发现已经支持语法高亮显示。ui
通常的作法,是执行protoc命令,依次将.proto文件转成Java类:google
protoc.exe -I=d:/tmp --java_out=d:/tmp d:/tmp/monitor_data.proto
不过gRPC官方推荐了一种更优雅的使用姿式,能够经过maven轻松搞定spa
2.1 pom.xml文件配置插件
<properties> <grpc.version>1.6.1</grpc.version> <protobuf.version>3.3.0</protobuf.version> </properties>
<dependencies> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-netty</artifactId> <version>${grpc.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-protobuf</artifactId> <version>${grpc.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-stub</artifactId> <version>${grpc.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>${protobuf.version}</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:${protobuf.version}:exe:${os.detected.classifier}</protocArtifact> <pluginId>grpc-java</pluginId> <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>compile-custom</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
2.2 编译生成Java类3d
使用maven的编译命令,便可在target中看到根据.proto文件生成的Java类,以下所示:netty
3、遇到的坑code
1.打开.proto文件后,显示“File not found”提示,以下所示:
这种状况,通常是未设置.proto文件所在文件夹为源文件,能够进行以下设置:
在.proto文件所在的文件夹上右键,设置目录为源文件根目录,以下所示: