rpc框架: thrift/avro/protobuf 之maven插件生成java类
thrift、avro、probobuf 这几个rpc框架的基本思想都差不多,先定义IDL文件,然后由各自的编译器(或maven插件)生成目标语言的源代码,但是,根据idl生成源代码这件事,如果每次都要手动敲命令,未免太无聊了,幸好这三种框架都提供了对应的maven插件来完成代码的自动生成,本文演示了这三种框架的maven插件用法。
一、maven-thrift-plugin
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>yjmyzz</groupId> 8 <artifactId>thrift-contract</artifactId> 9 <version>1.0</version> 10 11 <properties> 12 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 13 <compiler-plugin.version>2.3.2</compiler-plugin.version> 14 <thrift.version>0.9.2</thrift.version> 15 </properties> 16 17 <dependencies> 18 <dependency> 19 <groupId>org.apache.thrift</groupId> 20 <artifactId>libthrift</artifactId> 21 <version>${thrift.version}</version> 22 </dependency> 23 </dependencies> 24 25 <build> 26 <plugins> 27 <plugin> 28 <groupId>org.apache.maven.plugins</groupId> 29 <artifactId>maven-compiler-plugin</artifactId> 30 <version>${compiler-plugin.version}</version> 31 <configuration> 32 <encoding>${project.build.sourceEncoding}</encoding> 33 </configuration> 34 </plugin> 35 <plugin> 36 <groupId>org.apache.thrift.tools</groupId> 37 <artifactId>maven-thrift-plugin</artifactId> 38 <version>0.1.11</version> 39 <configuration> 40 <thriftExecutable>/usr/local/bin/thrift</thriftExecutable> 41 </configuration> 42 <executions> 43 <execution> 44 <id>thrift-sources</id> 45 <phase>generate-sources</phase> 46 <goals> 47 <goal>compile</goal> 48 </goals> 49 </execution> 50 <execution> 51 <id>thrift-test-sources</id> 52 <phase>generate-test-sources</phase> 53 <goals> 54 <goal>testCompile</goal> 55 </goals> 56 </execution> 57 </executions> 58 </plugin> 59 </plugins> 60 </build> 61 62 </project>
.thrift文件约定放在src/main/thrift目录即可,运行mvn package后,会自动在target目录下生成java源码及编译后的class,参考下图:
二、avro-maven-plugin
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>yjmyzz.avro</groupId> 8 <artifactId>avro-contract</artifactId> 9 <version>1.0</version> 10 11 <properties> 12 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 13 <compiler-plugin.version>2.3.2</compiler-plugin.version> 14 <avro.version>1.7.5</avro.version> 15 </properties> 16 <dependencies> 17 <dependency> 18 <groupId>junit</groupId> 19 <artifactId>junit</artifactId> 20 <version>4.10</version> 21 <scope>test</scope> 22 </dependency> 23 <dependency> 24 <groupId>org.slf4j</groupId> 25 <artifactId>slf4j-simple</artifactId> 26 <version>1.6.4</version> 27 <scope>compile</scope> 28 </dependency> 29 <dependency> 30 <groupId>org.apache.avro</groupId> 31 <artifactId>avro</artifactId> 32 <version>${avro.version}</version> 33 </dependency> 34 <dependency> 35 <groupId>org.apache.avro</groupId> 36 <artifactId>avro-ipc</artifactId> 37 <version>${avro.version}</version> 38 </dependency> 39 </dependencies> 40 <build> 41 <plugins> 42 <plugin> 43 <groupId>org.apache.maven.plugins</groupId> 44 <artifactId>maven-compiler-plugin</artifactId> 45 <version>${compiler-plugin.version}</version> 46 </plugin> 47 <plugin> 48 <groupId>org.apache.avro</groupId> 49 <artifactId>avro-maven-plugin</artifactId> 50 <version>${avro.version}</version> 51 <executions> 52 <execution> 53 <id>schemas</id> 54 <phase>generate-sources</phase> 55 <goals> 56 <goal>schema</goal> 57 <goal>protocol</goal> 58 <goal>idl-protocol</goal> 59 </goals> 60 </execution> 61 </executions> 62 </plugin> 63 </plugins> 64 </build> 65 </project>
各种avro的定义文件放在src/main/avro下,其它跟thrift类似,参考下图:
三、protobuf-maven-plugin
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>yjmyzz.protobuf</groupId> 8 <artifactId>protobuf-contract</artifactId> 9 <version>1.0</version> 10 11 12 <dependencies> 13 <dependency> 14 <groupId>com.google.protobuf</groupId> 15 <artifactId>protobuf-java</artifactId> 16 <version>3.0.0-beta-1</version> 17 </dependency> 18 </dependencies> 19 <build> 20 <plugins> 21 <plugin> 22 <groupId>com.github.igor-petruk.protobuf</groupId> 23 <artifactId>protobuf-maven-plugin</artifactId> 24 <version>0.6.3</version> 25 <executions> 26 <execution> 27 <goals> 28 <goal>run</goal> 29 </goals> 30 </execution> 31 </executions> 32 <configuration> 33 <protocCommand>/usr/local/bin/protoc</protocCommand> 34 </configuration> 35 </plugin> 36 </plugins> 37 </build> 38 39 40 </project>
定义文件放在/src/main/protobuf下,其它跟前二个插件类似,参考下图:
注:<protocCommand>/usr/local/bin/protoc</protocCommand> 这里的protoc编译器的版本,必须与
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.0.0-beta-1</version>
</dependency>
中的版本号兼容,否则生成java时会提示版本号不一致
作者:菩提树下的杨过
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。