日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring集成Thrift--Server AND Client

發(fā)布時間:2024/4/17 javascript 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring集成Thrift--Server AND Client 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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

Thrift網(wǎng)上有N多教程,不再贅述,這里搭建的是WEB項(xiàng)目,使用了spring,所以盡量使用了基于配置的方式。

一。server端

本著少些代碼,配置優(yōu)先的原則,在server端引入代理類,如下:

ThriftServerProxy:使用了反射

?

Java代碼??
  • public?class?ThriftServerProxy?{??
  • ??????????
  • ????private?static?Logger?logger?=?Logger.getLogger(ThriftServerStartListener.class);??
  • ????private?int?port;//?端口??
  • ????private?String?serviceInterface;//?實(shí)現(xiàn)類接口??????
  • ????private?Object?serviceImplObject;//實(shí)現(xiàn)類??
  • ??
  • ????set?and?get?……………………??
  • ????@SuppressWarnings({?"rawtypes",?"unchecked"?})??
  • ????public?void?start()?{??
  • ????new?Thread()?{??
  • ????public?void?run()?{??
  • ??
  • ????????try?{??
  • ????????TServerSocket?serverTransport?=?new?TServerSocket(getPort());?????????
  • ????????Class?Processor?=?Class.forName(getServiceInterface()+"$Processor");??
  • ????????Class?Iface?=?Class.forName(getServiceInterface()+"$Iface");????????????????Constructor?con?=?Processor.getConstructor(Iface);??
  • ????????TProcessor?processor?=?(TProcessor)con.newInstance(serviceImplObject);??
  • ????????Factory?protFactory?=?new?TBinaryProtocol.Factory(true,true);??
  • ????????TThreadPoolServer.Args?args?=?new?TThreadPoolServer.Args(serverTransport);??
  • ????????args.protocolFactory(protFactory);??
  • ??????????????????????
  • ????????args.processor(processor);??
  • ????????TServer?server?=?new?TThreadPoolServer(args);??
  • ????????logger.info("Starting?server?on?port?"+getPort()+"?...");??
  • ????????server.serve();??
  • ??
  • ????????}?catch?(TTransportException?e)?{??
  • ????????????e.printStackTrace();??
  • ????????}?catch?(Exception?e)?{??
  • ????????????e.printStackTrace();??
  • ????????}??
  • ????????}??
  • ????????}.start();??
  • ????}??
  • }??
  • ?applicationContext-thrift.xml:

    ?

    ?

    Java代碼??
  • <?xml?version="1.0"?encoding="UTF-8"?>??
  • <beans?xmlns="http://www.springframework.org/schema/beans"??
  • ????……………………………………………………??
  • ??
  • ????<description>thrift服務(wù)</description>??
  • ????<!--?聲明多個server,并將其關(guān)聯(lián)到該list中,以便監(jiān)聽器自動啟動?-->??
  • ????<util:list?id="thriftServerList">??
  • ????????<ref?bean="userProxy01"?/>??
  • ????????<ref?bean="userProxy02"?/>??
  • ????</util:list>??
  • ??????
  • ????<bean?id="userProxy01"?class="thrift.proxy.ThriftServerProxy">??
  • ????????<property?name="port"?value="7911"/>??
  • ????????<property?name="serviceInterface"?value="thrift.service.UserService"/>??
  • ????????<property?name="serviceImplObject"?ref="userServiceImpl"/>??????
  • ????</bean>??
  • ??????????
  • ????<bean?id="userProxy02"?class="thrift.proxy.ThriftServerProxy">??
  • ????????<property?name="port"?value="7910"/>??
  • ????????<property?name="serviceInterface"?value="thrift.service.UserService"/>??
  • ????????<property?name="serviceImplObject"?ref="userServiceImpl"/>??????
  • ????</bean>??
  • ??????????
  • </beans>??
  • ?使用監(jiān)聽器啟動全部服務(wù):

    ?

    ThriftServerStartListener:

    ?

    Java代碼??
  • public?class?ThriftServerStartListener?implements?ServletContextListener{??
  • ????private?static?Logger?logger?=?Logger.getLogger(UserServiceImpl.class);??
  • ??????
  • ??
  • ????@Override??
  • ????public?void?contextDestroyed(ServletContextEvent?event)?{??
  • ??????????
  • ????}??
  • ??
  • ????@SuppressWarnings("unchecked")??
  • ????@Override??
  • ????public?void?contextInitialized(ServletContextEvent?event)?{??
  • ????????//啟動SETUP?SEERVER??
  • ????????try?{??
  • ????????????ApplicationContext?context?=?WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());??
  • ??????????????
  • ????????????List<ThriftServerProxy>?list?=?((List<ThriftServerProxy>)?context.getBean("thriftServerList"));??
  • ????????????if(list!=null&&list.size()>0){??
  • ????????????????for(ThriftServerProxy?userProxy:list){??
  • ????????????????????userProxy.start();??
  • ????????????????}??
  • ????????????}??
  • ??
  • ????????????logger.info("Thrift?Server監(jiān)聽接口啟動。。。。。。。。。。。");??
  • ????????}?catch?(Exception?e)?{??
  • ????????????logger.error("Thrift?Server監(jiān)聽接口啟動錯誤",?e);??
  • ????????????e.printStackTrace();??
  • ????????}??
  • ????}??
  • ??
  • }??
  • ?二。client端

    ?

    client使用鏈接池管理鏈接,同時對客戶端使用代理,spring配置如下:

    applicationContext-thrift.xml:

    Java代碼??
  • <?xml?version="1.0"?encoding="UTF-8"?>??
  • <beans?xmlns="http://www.springframework.org/schema/beans"??
  • ??
  • ????……………………………………………………??
  • ????<description>thrift客戶端</description>??
  • ??????
  • ????<context:property-placeholder??
  • ????????location="classpath:config/properties/thrift.properties"?/>??
  • ??????????
  • ????<!--?連接池,管理器,客戶端代理,3個一組?-->??
  • ????<!--?thrift連接池配置7911?-->??
  • ????<bean?id="connectionProvider01"?class="thrift.common.ConnectionProviderImpl">??
  • ????????<property?name="serviceIP"?value="${thrift.ens.ip01}"?/>??
  • ????????<property?name="servicePort"?value="${thrift.ens.port01}"?/>??
  • ????????<property?name="maxActive"?value="${thrift.ens.maxActive}"?/>??
  • ????????<property?name="maxIdle"?value="${thrift.ens.maxIdle}"?/>??
  • ????????<property?name="testOnBorrow"?value="${thrift.ens.testOnBorrow}"?/>??
  • ????????<property?name="testOnReturn"?value="${thrift.ens.testOnReturn}"?/>??
  • ????????<property?name="testWhileIdle"?value="${thrift.ens.testWhileIdle}"?/>??
  • ????????<property?name="conTimeOut"?value="${thrift.ens.conTimeOut}"?/>??
  • ????</bean>??
  • ????<!--?thrift連接管理配置7911?-->??
  • ????<bean?id="connectionManager01"?class="thrift.common.ConnectionManager">??
  • ????????<property?name="connectionProvider"?ref="connectionProvider01"?/>??
  • ????</bean>??
  • ??????
  • ????<bean?id="userClient01"?class="thrift.proxy.ThriftClientProxy">??
  • ????????<property?name="serviceInterface"?value="thrift.service.UserService"?/>??
  • ????????<property?name="connectionManager"?ref="connectionManager01"?/>?????????
  • ????</bean>??
  • ??????
  • ????<!--?thrift連接池配置7910?-->??
  • ????<bean?id="connectionProvider02"?class="thrift.common.ConnectionProviderImpl">??
  • ????????<property?name="serviceIP"?value="${thrift.ens.ip02}"?/>??
  • ????????<property?name="servicePort"?value="${thrift.ens.port02}"?/>??
  • ????????<property?name="maxActive"?value="${thrift.ens.maxActive}"?/>??
  • ????????<property?name="maxIdle"?value="${thrift.ens.maxIdle}"?/>??
  • ????????<property?name="testOnBorrow"?value="${thrift.ens.testOnBorrow}"?/>??
  • ????????<property?name="testOnReturn"?value="${thrift.ens.testOnReturn}"?/>??
  • ????????<property?name="testWhileIdle"?value="${thrift.ens.testWhileIdle}"?/>??
  • ????????<property?name="conTimeOut"?value="${thrift.ens.conTimeOut}"?/>??
  • ????</bean>??
  • ????<!--?thrift連接管理配置?7910-->??
  • ????<bean?id="connectionManager02"?class="thrift.common.ConnectionManager">??
  • ????????<property?name="connectionProvider"?ref="connectionProvider02"?/>??
  • ????</bean>??
  • ??????
  • ??????
  • ??????
  • ????<bean?id="userClient02"?class="thrift.proxy.ThriftClientProxy">??
  • ????????<property?name="serviceInterface"?value="thrift.service.UserService"?/>??
  • ????????<property?name="connectionManager"?ref="connectionManager02"?/>?????????
  • ????</bean>??
  • ??????
  • </beans>??
  • ?

    ?

    ?

    ThriftClientProxy:

    ?

    Java代碼??
  • public?class?ThriftClientProxy?{??
  • ??
  • ????private?String?serviceInterface;??
  • ????private?ConnectionManager?connectionManager;??
  • ????????set?and?get……………………??
  • ????@SuppressWarnings({?"rawtypes",?"unchecked"?})??
  • ????public?Object?getClient()?{??
  • ????????Object?object?=?null;??
  • ????????try?{??
  • ????????????TTransport?transport?=?connectionManager.getSocket();??
  • ??
  • ????????????TProtocol?protocol?=?new?TBinaryProtocol(transport);??
  • ????????????Class?client?=?Class.forName(getServiceInterface()?+?"$Client");??
  • ??
  • ????????????Constructor?con?=?client.getConstructor(TProtocol.class);??
  • ????????????object?=?con.newInstance(protocol);??
  • ??
  • ????????}?catch?(ClassNotFoundException?e)?{??
  • ????????????//?TODO?Auto-generated?catch?block??
  • ????????????e.printStackTrace();??
  • ????????}?catch?(SecurityException?e)?{??
  • ????????????//?TODO?Auto-generated?catch?block??
  • ????????????e.printStackTrace();??
  • ????????}?catch?(NoSuchMethodException?e)?{??
  • ????????????//?TODO?Auto-generated?catch?block??
  • ????????????e.printStackTrace();??
  • ????????}?catch?(IllegalArgumentException?e)?{??
  • ????????????//?TODO?Auto-generated?catch?block??
  • ????????????e.printStackTrace();??
  • ????????}?catch?(InstantiationException?e)?{??
  • ????????????//?TODO?Auto-generated?catch?block??
  • ????????????e.printStackTrace();??
  • ????????}?catch?(IllegalAccessException?e)?{??
  • ????????????//?TODO?Auto-generated?catch?block??
  • ????????????e.printStackTrace();??
  • ????????}?catch?(InvocationTargetException?e)?{??
  • ????????????//?TODO?Auto-generated?catch?block??
  • ????????????e.printStackTrace();??
  • ????????}??
  • ????????return?object;??
  • ????}??
  • }??
  • ?客戶端調(diào)用,這里使用一個controller:

    ?

    ?

    Java代碼??
  • @Controller??
  • public?class?IndexController?{??
  • ??????
  • ????@Resource(name="userClient01")??
  • ????private?ThriftClientProxy?client01;??
  • ??????
  • ????@Resource(name="userClient02")??
  • ????private?ThriftClientProxy?client02;??
  • ??
  • ????@RequestMapping("/index.do")??
  • ????public?String?handleIndex(Model?model)?{??
  • ????????UserService.Client?client_01?=?(UserService.Client)(client01.getClient());??
  • ????????UserService.Client?client_02?=?(UserService.Client)(client02.getClient());??
  • ????????String?name;??
  • ????????try?{??
  • ????????????client_01.getUser("zhangsan");??
  • ????????????name?=?client_02.getUserName("zhaosi",?100);??
  • ????????????model.addAttribute("userName",?name);??
  • ????????}?catch?(TException?e)?{??
  • ????????????//?TODO?Auto-generated?catch?block??
  • ????????????e.printStackTrace();??
  • ????????}??
  • ??????????
  • ????????return?"index";??
  • ????}??
  • ??????
  • }??
  • 連接池部分參考了如下內(nèi)容:http://wenku.baidu.com/view/d0e91021aaea998fcc220e3d.html?

    代碼詳見附件。


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

    總結(jié)

    以上是生活随笔為你收集整理的Spring集成Thrift--Server AND Client的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。