Thrift--Spring集成ThriftServlet
Thrift除了可以通过TCP协议访问,还可以通过HTTP/HTTPS协议访问,在java中,thrift提供了一个servlet:org.apache.thrift.server.TServlet,我们只需继承这个TServlet就可以很方便的将TCP服务转换成HTTP/HTTPS服务,参考http://hanqunfeng.iteye.com/blog/1936556,为其中的ContractManage服务提供servlet接口,如下:
一。基本实现方法
1)服务端
?只需要改写构造函数即可,将实现类通过参数的形式传递给父类。
通过spring对servlet的管理(参考:http://hanqunfeng.iteye.com/blog/605174)来注入参数:
?
?这样,每增加一个服务只需要在spring配置文件中增加配置即可,不需要再单独创建一个servlet:
?
2)客户端
客户端同样采用代理的方式来实现
?
每调用一个接口,在spring中增加一个配置即可:
<bean id="contractManage"class="thrift.proxy.ThriftServletClientProxy"><property name="servletUrl"><value>http://localhost:8080/ThriftServer/contractManageServletProxy.do</value></property><property name="serviceInterface"value="thrift.service.ContactManager"></property></bean><bean id="userService"class="thrift.proxy.ThriftServletClientProxy"><property name="servletUrl"><value>http://localhost:8080/ThriftServer/userServiceServletProxy.do</value></property><property name="serviceInterface"value="thrift.service.UserService"></property></bean>?调用方式参考代码中的IndexController。
?