Thrift安装与配置及Java编写Thrift实现客户端与服务端通信
- 安装与配置
- 1、首先下载thrift-0.9.3.exe到指定目录,如D:\software\thrift
- 2、向Path中添加变量值,值为thrift.exe的地址,如D:\software\thrift\
- 3、修改thrift-0.9.3.exe名字为thrift.exe
- 4、命令行输入thrift -version,如果输出thrift的版本即表明安装成功。
- Java编写thrift
- 1、首先在D:\software\thrift文件中创建一个HelloService.thrift文件(用记事本就可以创建,然后改一下后缀就可)
- 2、cmd命令下,命令行切换到D:\software\thrift目录,输入thrift -gen java HelloService.thrift,编译会在D:\software\thrift目录下生成HelloService类。
- 3、把以上两个文件拷贝到IDEA中的com.example.demo.service中
- **IDEA中要导入thrift相关的依赖,也就是thrift相关的jar包。原生的maven并不会为我们编译thrift文件,所以需要在build中加入maven-thrift-plugin插件。
<build>
<plugins>
<plugin>
<groupId>org.apache.thrift.tools</groupId>
<artifactId>maven-thrift-plugin</artifactId>
<version>0.1.11</version>
<configuration>
<!--<thriftExecutable>/usr/local/bin/thrift</thriftExecutable>-->
<!--<thriftSourceRoot>src/main/thrift</thriftSourceRoot>-->
<!--<outputDirectory>src/main/java</outputDirectory>-->
</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>
- 4、编写实现类HelloServiceImpl
- 5、编写服务端代码 public class HelloServer
- 6、编写客户端代码 public class HelloClient
- 7、先运行服务端,再运行客户端。