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代碼

相關文章
相關標籤/搜索