美文网首页
1.IDEA 下protobuf 使用(Java)

1.IDEA 下protobuf 使用(Java)

作者: chachacha88 | 来源:发表于2020-06-07 17:38 被阅读0次

1.环境

版本: spring boot 2.2.5.RELEASE
语言:Java

2.安装IDEA插件

Protobuf Support | GoogleProtobufTool

Protobuf Support
  • Full Proto3 support.
  • Custom include path for proto files.
  • Usage search for messages, enums and fields (for standard and custom options).
  • Syntax validation for proto2/proto3. Checks for reserved/duplicated field tags and names. Highlight unresolved reference errors.
  • Fonts & Colors configuration.
  • Structure View.
  • Code formatting.
  • Navigation to message, enum or service by name (Ctrl+N)
    Rename refactoring (files, messages, enums and fields).
    Spell checking.
GoogleProtobufTool
  • generate java protobuf plugin for idea. generate one or more file.

3.配置pom文件

protobuf-java

<dependency>
  <groupId>com.google.protobuf</groupId>
  <artifactId>protobuf-java</artifactId>
  <version>3.12.2</version>
 </dependency>

grpc

<dependency>
  <groupId>io.grpc</groupId>
  <artifactId>grpc-netty</artifactId>
  <version>1.29.0</version>
</dependency>
<dependency>
  <groupId>io.grpc</groupId>
  <artifactId>grpc-protobuf</artifactId>
   <version>1.29.0</version>
 </dependency>
 <dependency>
   <groupId>io.grpc</groupId>
   <artifactId>grpc-stub</artifactId>
   <version>1.29.0</version>
</dependency>

4.生成Java

插件配置

pom设置

<build>
  <extensions>
    <extension>
        <groupId>kr.motd.maven</groupId>
        <artifactId>os-maven-plugin</artifactId>
        <version>1.4.1.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.1.0: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>

proto代码

  • src main 目录下新建的proto文件夹下,创建UserModel.proto文件
syntax = "proto3";
option java_outer_classname = "UserModel";

message User{
     int32 id = 1;
     string name = 2;
     string sex= 3;
}
  • 执行生成(protobuf:compile-javano)
  • 生成Java文件(/target/generated-sources/protobuf/java/*)

相关文章

网友评论

      本文标题:1.IDEA 下protobuf 使用(Java)

      本文链接:https://www.haomeiwen.com/subject/ofpgzhtx.html