zoukankan      html  css  js  c++  java
  • Intellij IDEA中使用Protobuf的正确姿势

    一、.proto文件语法高亮显示

        需要安装Protobuf Support插件

       依次点击Intellij中的“File”-->"Settings"-->"Plugins"-->"Browse repositories",如下所示:

    输入Protobuf,如下所示

    安装完后,重启Intellij IDEA,查看.proto文件,会发现已经支持语法高亮显示。

    二、将.proto文件转成Java类

        一般的做法,是执行protoc命令,依次将.proto文件转成Java类:

    protoc.exe -I=d:/tmp --java_out=d:/tmp d:/tmp/monitor_data.proto

      不过gRPC官方推荐了一种更优雅的使用姿势,可以通过maven轻松搞定

     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类

      使用maven的编译命令,即可在target中看到根据.proto文件生成的Java类,如下所示:

    三、遇到的坑

    1.打开.proto文件后,显示“File not found”提示,如下所示:

     这种情况,一般是未设置.proto文件所在文件夹为源文件,可以进行如下设置:

     在.proto文件所在的文件夹上右键,设置目录为源文件根目录,如下所示:

    参考文档:

    https://github.com/google/protobuf

    https://github.com/protostuff/protobuf-jetbrains-plugin

    https://github.com/grpc/grpc-java

  • 相关阅读:
    浅析跨域请求
    python虚拟环境--virtualenv
    centos7下使用yum安装pip
    centos下python安装与虚拟环境配置
    ES6基础语法
    CCI_chapter 19 Moderate
    CCI_chapter 16 Low level
    CCI_chapter 13C++
    CCI_chapter 8 Recurision
    LeetCode_Generate Parentheses
  • 原文地址:https://www.cnblogs.com/liugh/p/7505533.html
Copyright © 2011-2022 走看看