http://hanqunfeng.iteye.com/blog/1936556
Thrift网上有N多教程,不再赘述,这里搭建的是WEB项目,使用了spring,所以尽量使用了基于配置的方式。
一。server端
本着少些代码,配置优先的原则,在server端引入代理类,如下:
ThriftServerProxy:使用了反射
1. public class
2.
3. private static Logger logger = Logger.getLogger(ThriftServerStartListener.class);
4. private int port;// 端口
5. private String serviceInterface;// 实现类接口
6. private Object serviceImplObject;//实现类
7.
8. set and get ……………………
9. @SuppressWarnings({ "rawtypes", "unchecked"
10. public void
11. new
12. public void
13.
14. try
15. new
16. "$Processor");
17. "$Iface"); Constructor con = Processor.getConstructor(Iface);
18. TProcessor processor = (TProcessor)con.newInstance(serviceImplObject);
19. new TBinaryProtocol.Factory(true,true);
20. new
21. args.protocolFactory(protFactory);
22.
23. args.processor(processor);
24. new
25. "Starting server on port "+getPort()+" ...");
26. server.serve();
27.
28. catch
29. e.printStackTrace();
30. catch
31. e.printStackTrace();
32. }
33. }
34. }.start();
35. }
36. }
applicationContext-thrift.xml:
1. <?xml versinotallow="1.0" encoding="UTF-8"?>
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. ……………………………………………………
4.
5. <description>thrift服务</description>
6. <!-- 声明多个server,并将其关联到该list中,以便监听器自动启动 -->
7. "thriftServerList">
8. "userProxy01"
9. "userProxy02"
10. </util:list>
11.
12. "userProxy01" class="thrift.proxy.ThriftServerProxy">
13. "port" value="7911"/>
14. "serviceInterface" value="thrift.service.UserService"/>
15. "serviceImplObject" ref="userServiceImpl"/>
16. </bean>
17.
18. "userProxy02" class="thrift.proxy.ThriftServerProxy">
19. "port" value="7910"/>
20. "serviceInterface" value="thrift.service.UserService"/>
21. "serviceImplObject" ref="userServiceImpl"/>
22. </bean>
23.
24. </beans>
使用监听器启动全部服务:
ThriftServerStartListener:
1. public class ThriftServerStartListener implements
2. private static Logger logger = Logger.getLogger(UserServiceImpl.class);
3.
4.
5. @Override
6. public void
7.
8. }
9.
10. @SuppressWarnings("unchecked")
11. @Override
12. public void
13. //启动SETUP SEERVER
14. try
15. ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
16.
17. "thriftServerList"));
18. if(list!=null&&list.size()>0){
19. for(ThriftServerProxy userProxy:list){
20. userProxy.start();
21. }
22. }
23.
24. "Thrift Server监听接口启动。。。。。。。。。。。");
25. catch
26. "Thrift Server监听接口启动错误", e);
27. e.printStackTrace();
28. }
29. }
30.
31. }
二。client端
client使用链接池管理链接,同时对客户端使用代理,spring配置如下:
applicationContext-thrift.xml:
1. <?xml version="1.0" encoding="UTF-8"?>
2. <beans xmlns="http://www.springframework.org/schema/beans"
3.
4. ……………………………………………………
5. <description>thrift客户端</description>
6.
7. <context:property-placeholder
8. "classpath:config/properties/thrift.properties"
9.
10. 3个一组 -->
11. 7911
12. "connectionProvider01" class="thrift.common.ConnectionProviderImpl">
13. "serviceIP" value="${thrift.ens.ip01}"
14. "servicePort" value="${thrift.ens.port01}"
15. "maxActive" value="${thrift.ens.maxActive}"
16. "maxIdle" value="${thrift.ens.maxIdle}"
17. "testOnBorrow" value="${thrift.ens.testOnBorrow}"
18. "testOnReturn" value="${thrift.ens.testOnReturn}"
19. "testWhileIdle" value="${thrift.ens.testWhileIdle}"
20. "conTimeOut" value="${thrift.ens.conTimeOut}"
21. </bean>
22. 7911
23. "connectionManager01" class="thrift.common.ConnectionManager">
24. "connectionProvider" ref="connectionProvider01"
25. </bean>
26.
27. "userClient01" class="thrift.proxy.ThriftClientProxy">
28. "serviceInterface" value="thrift.service.UserService"
29. "connectionManager" ref="connectionManager01"
30. </bean>
31.
32. 7910
33. "connectionProvider02" class="thrift.common.ConnectionProviderImpl">
34. "serviceIP" value="${thrift.ens.ip02}"
35. "servicePort" value="${thrift.ens.port02}"
36. "maxActive" value="${thrift.ens.maxActive}"
37. "maxIdle" value="${thrift.ens.maxIdle}"
38. "testOnBorrow" value="${thrift.ens.testOnBorrow}"
39. "testOnReturn" value="${thrift.ens.testOnReturn}"
40. "testWhileIdle" value="${thrift.ens.testWhileIdle}"
41. "conTimeOut" value="${thrift.ens.conTimeOut}"
42. </bean>
43. 7910-->
44. "connectionManager02" class="thrift.common.ConnectionManager">
45. "connectionProvider" ref="connectionProvider02"
46. </bean>
47.
48.
49.
50. "userClient02" class="thrift.proxy.ThriftClientProxy">
51. "serviceInterface" value="thrift.service.UserService"
52. "connectionManager" ref="connectionManager02"
53. </bean>
54.
55. </beans>
ThriftClientProxy:
1. public class
2.
3. private
4. private
5. set and get……………………
6. @SuppressWarnings({ "rawtypes", "unchecked"
7. public
8. null;
9. try
10. TTransport transport = connectionManager.getSocket();
11.
12. new
13. "$Client");
14.
15. class);
16. object = con.newInstance(protocol);
17.
18. catch
19. // TODO Auto-generated catch block
20. e.printStackTrace();
21. catch
22. // TODO Auto-generated catch block
23. e.printStackTrace();
24. catch
25. // TODO Auto-generated catch block
26. e.printStackTrace();
27. catch
28. // TODO Auto-generated catch block
29. e.printStackTrace();
30. catch
31. // TODO Auto-generated catch block
32. e.printStackTrace();
33. catch
34. // TODO Auto-generated catch block
35. e.printStackTrace();
36. catch
37. // TODO Auto-generated catch block
38. e.printStackTrace();
39. }
40. return
41. }
42. }
客户端调用,这里使用一个controller:
1. @Controller
2. public class
3.
4. @Resource(name="userClient01")
5. private
6.
7. @Resource(name="userClient02")
8. private
9.
10. @RequestMapping("/index.do")
11. public
12. UserService.Client client_01 = (UserService.Client)(client01.getClient());
13. UserService.Client client_02 = (UserService.Client)(client02.getClient());
14. String name;
15. try
16. "zhangsan");
17. "zhaosi", 100);
18. "userName", name);
19. catch
20. // TODO Auto-generated catch block
21. e.printStackTrace();
22. }
23.
24. return "index";
25. }
26.
27. }
连接池部分参考了如下内容:http://wenku.baidu.com/view/d0e91021aaea998fcc220e3d.html
代码详见附件。