摘要:本文简单写一个Thrift支持多接口的代码
Thrift通过TMultiplexedProcessor来设置多个服务接口
下面是代码:
IDL接口
HelloService.thrift
1 | namespace java com.thrift.demo.service |
TestService.thrift
1 | struct Test { |
接口实现类
HelloServiceImpl.java
1 | public class HelloServiceImpl implements HelloService.Iface { |
TestServiceImpl.java
1 | public class TestServiceImpl implements TestService.Iface { |
Server端
1 | public class ThriftServer { |
Client端
1 | public class ThriftClient { |
运行结果:
Server端:
开启thrift服务器,监听端口:9090
TestServiceImpl.getBool() + true
TestServiceImpl.getDouble() + 5.0
say string :哈哈哈
Client端:
Received 1
Client getBool()true
Received 2
Client getDouble()5.0
Received 1
HelloService sayString :哈哈哈
通过这个方法,就可以在启动一个服务中同时提供多个接口给客户端调用,这种方式在项目开发中可能会用的更多。
参考文章:
Thrift对多接口服务的支持