Spring集成Thrift--Server AND Client

来源:互联网 发布:怎么学python 编辑:程序博客网 时间:2024/05/16 08:55

http://hanqunfeng.iteye.com/blog/1936556

Thrift网上有N多教程,不再赘述,这里搭建的是WEB项目,使用了spring,所以尽量使用了基于配置的方式。

一。server端

本着少些代码,配置优先的原则,在server端引入代理类,如下:

ThriftServerProxy:使用了反射

 

Java代码  收藏代码
  1. public class ThriftServerProxy {  
  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 start() {  
  11.     new Thread() {  
  12.     public void run() {  
  13.   
  14.         try {  
  15.         TServerSocket serverTransport = new TServerSocket(getPort());         
  16.         Class Processor = Class.forName(getServiceInterface()+"$Processor");  
  17.         Class Iface = Class.forName(getServiceInterface()+"$Iface");                Constructor con = Processor.getConstructor(Iface);  
  18.         TProcessor processor = (TProcessor)con.newInstance(serviceImplObject);  
  19.         Factory protFactory = new TBinaryProtocol.Factory(true,true);  
  20.         TThreadPoolServer.Args args = new TThreadPoolServer.Args(serverTransport);  
  21.         args.protocolFactory(protFactory);  
  22.                       
  23.         args.processor(processor);  
  24.         TServer server = new TThreadPoolServer(args);  
  25.         logger.info("Starting server on port "+getPort()+" ...");  
  26.         server.serve();  
  27.   
  28.         } catch (TTransportException e) {  
  29.             e.printStackTrace();  
  30.         } catch (Exception e) {  
  31.             e.printStackTrace();  
  32.         }  
  33.         }  
  34.         }.start();  
  35.     }  
  36. }  

 applicationContext-thrift.xml:

 

 

Java代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     ……………………………………………………  
  4.   
  5.     <description>thrift服务</description>  
  6.     <!-- 声明多个server,并将其关联到该list中,以便监听器自动启动 -->  
  7.     <util:list id="thriftServerList">  
  8.         <ref bean="userProxy01" />  
  9.         <ref bean="userProxy02" />  
  10.     </util:list>  
  11.       
  12.     <bean id="userProxy01" class="thrift.proxy.ThriftServerProxy">  
  13.         <property name="port" value="7911"/>  
  14.         <property name="serviceInterface" value="thrift.service.UserService"/>  
  15.         <property name="serviceImplObject" ref="userServiceImpl"/>      
  16.     </bean>  
  17.           
  18.     <bean id="userProxy02" class="thrift.proxy.ThriftServerProxy">  
  19.         <property name="port" value="7910"/>  
  20.         <property name="serviceInterface" value="thrift.service.UserService"/>  
  21.         <property name="serviceImplObject" ref="userServiceImpl"/>      
  22.     </bean>  
  23.           
  24. </beans>  

 使用监听器启动全部服务:

 

ThriftServerStartListener:

 

Java代码  收藏代码
  1. public class ThriftServerStartListener implements ServletContextListener{  
  2.     private static Logger logger = Logger.getLogger(UserServiceImpl.class);  
  3.       
  4.   
  5.     @Override  
  6.     public void contextDestroyed(ServletContextEvent event) {  
  7.           
  8.     }  
  9.   
  10.     @SuppressWarnings("unchecked")  
  11.     @Override  
  12.     public void contextInitialized(ServletContextEvent event) {  
  13.         //启动SETUP SEERVER  
  14.         try {  
  15.             ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());  
  16.               
  17.             List<ThriftServerProxy> list = ((List<ThriftServerProxy>) context.getBean("thriftServerList"));  
  18.             if(list!=null&&list.size()>0){  
  19.                 for(ThriftServerProxy userProxy:list){  
  20.                     userProxy.start();  
  21.                 }  
  22.             }  
  23.   
  24.             logger.info("Thrift Server监听接口启动。。。。。。。。。。。");  
  25.         } catch (Exception e) {  
  26.             logger.error("Thrift Server监听接口启动错误", e);  
  27.             e.printStackTrace();  
  28.         }  
  29.     }  
  30.   
  31. }  

 二。client端

 

client使用链接池管理链接,同时对客户端使用代理,spring配置如下:

applicationContext-thrift.xml:

Java代码  收藏代码
  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.         location="classpath:config/properties/thrift.properties" />  
  9.           
  10.     <!-- 连接池,管理器,客户端代理,3个一组 -->  
  11.     <!-- thrift连接池配置7911 -->  
  12.     <bean id="connectionProvider01" class="thrift.common.ConnectionProviderImpl">  
  13.         <property name="serviceIP" value="${thrift.ens.ip01}" />  
  14.         <property name="servicePort" value="${thrift.ens.port01}" />  
  15.         <property name="maxActive" value="${thrift.ens.maxActive}" />  
  16.         <property name="maxIdle" value="${thrift.ens.maxIdle}" />  
  17.         <property name="testOnBorrow" value="${thrift.ens.testOnBorrow}" />  
  18.         <property name="testOnReturn" value="${thrift.ens.testOnReturn}" />  
  19.         <property name="testWhileIdle" value="${thrift.ens.testWhileIdle}" />  
  20.         <property name="conTimeOut" value="${thrift.ens.conTimeOut}" />  
  21.     </bean>  
  22.     <!-- thrift连接管理配置7911 -->  
  23.     <bean id="connectionManager01" class="thrift.common.ConnectionManager">  
  24.         <property name="connectionProvider" ref="connectionProvider01" />  
  25.     </bean>  
  26.       
  27.     <bean id="userClient01" class="thrift.proxy.ThriftClientProxy">  
  28.         <property name="serviceInterface" value="thrift.service.UserService" />  
  29.         <property name="connectionManager" ref="connectionManager01" />         
  30.     </bean>  
  31.       
  32.     <!-- thrift连接池配置7910 -->  
  33.     <bean id="connectionProvider02" class="thrift.common.ConnectionProviderImpl">  
  34.         <property name="serviceIP" value="${thrift.ens.ip02}" />  
  35.         <property name="servicePort" value="${thrift.ens.port02}" />  
  36.         <property name="maxActive" value="${thrift.ens.maxActive}" />  
  37.         <property name="maxIdle" value="${thrift.ens.maxIdle}" />  
  38.         <property name="testOnBorrow" value="${thrift.ens.testOnBorrow}" />  
  39.         <property name="testOnReturn" value="${thrift.ens.testOnReturn}" />  
  40.         <property name="testWhileIdle" value="${thrift.ens.testWhileIdle}" />  
  41.         <property name="conTimeOut" value="${thrift.ens.conTimeOut}" />  
  42.     </bean>  
  43.     <!-- thrift连接管理配置 7910-->  
  44.     <bean id="connectionManager02" class="thrift.common.ConnectionManager">  
  45.         <property name="connectionProvider" ref="connectionProvider02" />  
  46.     </bean>  
  47.       
  48.       
  49.       
  50.     <bean id="userClient02" class="thrift.proxy.ThriftClientProxy">  
  51.         <property name="serviceInterface" value="thrift.service.UserService" />  
  52.         <property name="connectionManager" ref="connectionManager02" />         
  53.     </bean>  
  54.       
  55. </beans>  

 

 

 

ThriftClientProxy:

 

Java代码  收藏代码
  1. public class ThriftClientProxy {  
  2.   
  3.     private String serviceInterface;  
  4.     private ConnectionManager connectionManager;  
  5.         set and get……………………  
  6.     @SuppressWarnings({ "rawtypes""unchecked" })  
  7.     public Object getClient() {  
  8.         Object object = null;  
  9.         try {  
  10.             TTransport transport = connectionManager.getSocket();  
  11.   
  12.             TProtocol protocol = new TBinaryProtocol(transport);  
  13.             Class client = Class.forName(getServiceInterface() + "$Client");  
  14.   
  15.             Constructor con = client.getConstructor(TProtocol.class);  
  16.             object = con.newInstance(protocol);  
  17.   
  18.         } catch (ClassNotFoundException e) {  
  19.             // TODO Auto-generated catch block  
  20.             e.printStackTrace();  
  21.         } catch (SecurityException e) {  
  22.             // TODO Auto-generated catch block  
  23.             e.printStackTrace();  
  24.         } catch (NoSuchMethodException e) {  
  25.             // TODO Auto-generated catch block  
  26.             e.printStackTrace();  
  27.         } catch (IllegalArgumentException e) {  
  28.             // TODO Auto-generated catch block  
  29.             e.printStackTrace();  
  30.         } catch (InstantiationException e) {  
  31.             // TODO Auto-generated catch block  
  32.             e.printStackTrace();  
  33.         } catch (IllegalAccessException e) {  
  34.             // TODO Auto-generated catch block  
  35.             e.printStackTrace();  
  36.         } catch (InvocationTargetException e) {  
  37.             // TODO Auto-generated catch block  
  38.             e.printStackTrace();  
  39.         }  
  40.         return object;  
  41.     }  
  42. }  

 客户端调用,这里使用一个controller:

 

 

Java代码  收藏代码
  1. @Controller  
  2. public class IndexController {  
  3.       
  4.     @Resource(name="userClient01")  
  5.     private ThriftClientProxy client01;  
  6.       
  7.     @Resource(name="userClient02")  
  8.     private ThriftClientProxy client02;  
  9.   
  10.     @RequestMapping("/index.do")  
  11.     public String handleIndex(Model model) {  
  12.         UserService.Client client_01 = (UserService.Client)(client01.getClient());  
  13.         UserService.Client client_02 = (UserService.Client)(client02.getClient());  
  14.         String name;  
  15.         try {  
  16.             client_01.getUser("zhangsan");  
  17.             name = client_02.getUserName("zhaosi"100);  
  18.             model.addAttribute("userName", name);  
  19.         } catch (TException e) {  
  20.             // TODO Auto-generated catch block  
  21.             e.printStackTrace();  
  22.         }  
  23.           
  24.         return "index";  
  25.     }  
  26.       
  27. }  

连接池部分参考了如下内容:http://wenku.baidu.com/view/d0e91021aaea998fcc220e3d.html 

代码详见附件。


http://www.open-open.com/lib/view/open1357804231418.html

0 0
原创粉丝点击