thrift rpc开发

1、安装thriftjava

http://thrift.apache.org/download 下载thriftlinux

linux安装apache

$ ./configuremaven

$ make测试

$ sudo make installui

window安装编码

新建d:/thrift文件夹,将下载的thrift-0.11.0.exe从新命名为thrift.exe后放到d:/thrift文件夹下url

2.配置环境变量spa

 在path添加 d:/thrift插件

 

验证

执行  thrift -version,出现版本信息即成功

Thrift version 0.11.0

 

2、java maven使用thrift

pom.xml文件以下

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.study</groupId>
	<artifactId>thrift</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>thrift</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
	</properties>

	<dependencies>
		<!-- https://mvnrepository.com/artifact/org.apache.thrift/libthrift -->
		<dependency>
			<groupId>org.apache.thrift</groupId>
			<artifactId>libthrift</artifactId>
			<version>0.11.0</version>
		</dependency>

		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.14</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<!-- 指定maven编译的jdk版本,若是不指定,maven3默认用jdk 1.5 maven2默认用jdk1.3 -->
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<!-- 通常而言,target与source是保持一致的,可是,有时候为了让程序能在其余版本的jdk中运行(对于低版本目标jdk,源代码中不能使用低版本jdk中不支持的语法),会存在target不一样于source的状况 -->
					<source>1.8</source> <!-- 源代码使用的JDK版本 -->
					<target>1.8</target> <!-- 须要生成的目标class文件的编译版本 -->
					<encoding>UTF-8</encoding><!-- 字符集编码 -->
					<skipTests>true</skipTests><!-- 跳过测试 -->
					<verbose>true</verbose>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.thrift.tools</groupId>
				<artifactId>maven-thrift-plugin</artifactId>
				<version>0.1.11</version>
				<configuration>
					<thriftExecutable>d:\\thrift\\thrift.exe</thriftExecutable>
					<!-- <thriftSourceRoot>src/main/thrift</thriftSourceRoot> -->
					<!-- <outputDirectory>src/main/java</outputDirectory> -->
					<outputDirectory>${project.build.sourceDirectory}</outputDirectory>
					<generator>java</generator>
				</configuration>
				<executions>
					<execution>
						<id>thrift-sources</id>
						<phase>generate-sources</phase>
						<goals>
							<goal>compile</goal>
						</goals>
					</execution>
					<execution>
						<id>thrift-test-sources</id>
						<phase>generate-test-sources</phase>
						<goals>
							<goal>testCompile</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

咱们能够为插件作一些配置:

thriftExecutable,指的是thrift编译器的位置,若是咱们配置了环境变量,能够不指定。验证环境变量能够使用thrift --version命令。

thriftSourceRoot,thrift源文件的目录,默认会从src/main/thrift下读取。

outputDirectory,生成java文件的目录。其实这个通常不须要配置,由于java文件的包名是在.thrift文件以namespace的形式定义的

 

执行mvn package 生成java代码

相关文章
相关标签/搜索