rpc框架: thrift/avro/protobuf 之maven插件生成java类

简介: thrift、avro、probobuf 这几个rpc框架的基本思想都差不多,先定义IDL文件,然后由各自的编译器(或maven插件)生成目标语言的源代码,但是,根据idl生成源代码这件事,如果每次都要手动敲命令,未免太无聊了,幸好这三种框架都提供了对应的maven插件来完成代码的自动生成,本文演示了这三种框架的maven插件用法。

thriftavroprobobuf 这几个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>
View Code

.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>
View Code

各种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>
View Code

定义文件放在/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时会提示版本号不一致

目录
相关文章
|
9天前
|
Java 测试技术 Maven
Maven 插件
Maven包含clean、default/build和site三大生命周期,各周期由一系列阶段组成,如mvn clean执行Clean生命周期的clean阶段。实际工作由插件如maven-clean-plugin完成。Maven是插件驱动的框架,用于生成jar/war、编译、测试、文档和报告。插件目标通过`mvn plugin-name:goal-name`调用。
|
1天前
|
Java 测试技术 Maven
Maven 插件
Maven 实际上是一个依赖插件执行的框架,每个任务实际上是由插件完成。
|
2天前
|
Java 测试技术 Maven
Maven 插件
Maven manages projects through three standard lifecycles: clean (for clearing project outputs), default (for building projects), and site (for creating project documentation). Each lifecycle consists of phases that act as unified interfaces, implemented by plugins.
|
4天前
|
Java 测试技术 Maven
Maven 插件
**Maven Lifecycles & Plugins Summary** Maven orchestrates project builds via 3 core lifecycles: `clean`, `default(build)`, and `site`. Each lifecycle comprises phases serving as standardized interfaces for tasks like cleaning, building, and creating site docs.
|
7天前
|
Java 测试技术 Maven
Maven 插件
创建 jar 文件 创建 war 文件 编译代码文件 代码单元测试 创建工程文档 创建工程报告
|
9天前
|
Java 数据库连接 Apache
java编程语言常用框架有哪些?
Java作为一种广泛使用的编程语言,拥有众多常用框架,这些框架帮助开发者提高开发效率和代码质量。
19 3
|
10天前
|
测试技术 API Android开发
《手把手教你》系列基础篇(九十七)-java+ selenium自动化测试-框架设计篇-Selenium方法的二次封装和页面基类(详解教程)
【7月更文挑战第15天】这是关于自动化测试框架中Selenium API二次封装的教程总结。教程中介绍了如何设计一个支持不同浏览器测试的页面基类(BasePage),该基类包含了对Selenium方法的二次封装,如元素的输入、点击、清除等常用操作,以减少重复代码。此外,页面基类还提供了获取页面标题和URL的方法。
27 2
|
11天前
|
Web App开发 XML Java
《手把手教你》系列基础篇(九十六)-java+ selenium自动化测试-框架之设计篇-跨浏览器(详解教程)
【7月更文挑战第14天】这篇教程介绍了如何使用Java和Selenium构建一个支持跨浏览器测试的自动化测试框架。设计的核心是通过读取配置文件来切换不同浏览器执行测试用例。配置文件中定义了浏览器类型(如Firefox、Chrome)和测试服务器的URL。代码包括一个`BrowserEngine`类,它初始化配置数据,根据配置启动指定的浏览器,并提供关闭浏览器的方法。测试脚本`TestLaunchBrowser`使用`BrowserEngine`来启动浏览器并执行测试。整个框架允许在不同浏览器上运行相同的测试,以确保兼容性和一致性。
25 3
|
13天前
|
存储 Web App开发 Java
《手把手教你》系列基础篇(九十五)-java+ selenium自动化测试-框架之设计篇-java实现自定义日志输出(详解教程)
【7月更文挑战第13天】这篇文章介绍了如何在Java中创建一个简单的自定义日志系统,以替代Log4j或logback。
47 5
|
13天前
|
设计模式 Java 测试技术
《手把手教你》系列基础篇(九十四)-java+ selenium自动化测试-框架设计基础-POM设计模式实现-下篇(详解教程)
【7月更文挑战第12天】在本文中,作者宏哥介绍了如何在不使用PageFactory的情况下,用Java和Selenium实现Page Object Model (POM)。文章通过一个百度首页登录的实战例子来说明。首先,创建了一个名为`BaiduHomePage1`的页面对象类,其中包含了页面元素的定位和相关操作方法。接着,创建了测试类`TestWithPOM1`,在测试类中初始化WebDriver,设置驱动路径,最大化窗口,并调用页面对象类的方法进行登录操作。这样,测试脚本保持简洁,遵循了POM模式的高可读性和可维护性原则。
17 2

推荐镜像

更多