2021-03-29

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、先运行服务端,再运行客户端。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值