Spring集成Thrift--Server AND Client
http://hanqunfeng.iteye.com/blog/1936556
Thrift网上有N多教程,不再赘述,这里搭建的是WEB项目,使用了spring,所以尽量使用了基于配置的方式。
一。server端
本着少些代码,配置优先的原则,在server端引入代理类,如下:
ThriftServerProxy:使用了反射
- public class ThriftServerProxy {
- private static Logger logger = Logger.getLogger(ThriftServerStartListener.class);
- private int port;// 端口
- private String serviceInterface;// 实现类接口
- private Object serviceImplObject;//实现类
- 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:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- ……………………………………………………
- <description>thrift服务</description>
- <!-- 声明多个server,并将其关联到该list中,以便监听器自动启动 -->
- <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>
使用监听器启动全部服务:
ThriftServerStartListener:
- 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监听接口启动。。。。。。。。。。。");
- } catch (Exception e) {
- logger.error("Thrift Server监听接口启动错误", e);
- e.printStackTrace();
- }
- }
- }
二。client端
client使用链接池管理链接,同时对客户端使用代理,spring配置如下:
applicationContext-thrift.xml:
- <?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:
- 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;
- }
- }
客户端调用,这里使用一个controller:
- @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";
- }
- }
连接池部分参考了如下内容:http://wenku.baidu.com/view/d0e91021aaea998fcc220e3d.html
代码详见附件。
http://www.open-open.com/lib/view/open1357804231418.html
Spring集成Thrift--Server AND Client相关推荐
- 玩转Spring Cloud之配置中心(config server config client)
玩转Spring Cloud之配置中心(config server &config client) 本文内容导航: 一.搭建配置服务中心(config server) 1.1.git方式 1 ...
- java thrift client_使用thrift的java client调用python server
上面这篇文章的例子是使用java client调用python server中的helloString方法来打印client传输过去的字符串 thrift文件,hello.thrift service ...
- Spring Cloud Feign启动Load balancer does not have available server for client分析
项目场景: Spring Cloud Feign 学习过程中遇到Feign访问的时候报错Load balancer does not have available server for client ...
- 从零开始学 Java - Spring 集成 Memcached 缓存配置(二)
Memcached 客户端选择 上一篇文章 从零开始学 Java - Spring 集成 Memcached 缓存配置(一)中我们讲到这篇要谈客户端的选择,在 Java 中一般常用的有三个: Memc ...
- memcached与spring集成
一.背景 销售CRM(项目A)将负责管理项目信息系统(项目B)的支付与权限 上级要求为避免频繁调用CRM接口,中间放一级缓存,但要做到缓存中保证最新数据 因项目B已使用memcache作缓存,所以决定 ...
- mysql数据实时同步:Canal安装部署、kafka安装、zk安装、mysql安装、Canal Server+Canal Client HA,Canal+mysql+Kafka,相关验证(学习笔记)
目录 Canal安装部署 1.1. 服务器准备 1.2. 设置主机名并配置hosts 1.3. 免密设置 1.4. 设置ntp时间 1.5. 关闭防火墙 1.6. 关闭selinux 1.7. 安装J ...
- resteasy_RESTEasy教程第2部分:Spring集成
resteasy RESTEasy提供了对Spring集成的支持,这使我们能够将Spring bean作为RESTful WebServices公开. 步骤#1:使用Maven配置RESTEasy + ...
- RESTEasy教程第2部分:Spring集成
RESTEasy提供了对Spring集成的支持,这使我们能够将Spring bean作为RESTful WebServices公开. 步骤#1:使用Maven配置RESTEasy + Spring依赖 ...
- RabbitMQ安装和使用(和Spring集成)
一.安装Rabbit MQ Rabbit MQ 是建立在强大的Erlang OTP平台上,因此安装Rabbit MQ的前提是安装Erlang.通过下面两个连接下载安装3.2.3 版本: 下载并安装 E ...
最新文章
- VC++ 6.0下OpengGL配置以及glut配置
- winsock setsockopt 详解
- [Linux]centOS7-1-1503-x86_64下安装VM-TOOLS
- 硬件编程:硬件开发中常用的几种通信接口总结
- 前端学习(2783):封装myrequest并绑定到全局
- pythoning——11、正则匹配
- LR录制脚本后,中文显示的是乱码,怎么解决?
- RabbitMQ ACK 机制的意义是什么?
- EF6学习笔记十:原始查询,在EF中使用SQL语句
- mysql mapinfo_MapInfo常见数据格式
- 买的鱼丸怎么做好吃 鱼丸的家常做法介绍
- 终端溯源图构建工具SPADE专题-1 SPADE工具安装
- Be yourself——《按自己的意愿过一生》读后感
- iView级联选择器Cascader回显慢的问题
- 构建docker镜像时ERROR: http://dl-cdn.alpinelinux.org/alpine/v3.12/main: network error
- 分享一个python生成ico图片程序
- powershell 启动时报错 profile.ps1 cannot be loaded because running scri
- 几家线上少儿英语(vipkid、哒哒英语等)试听感想
- c语言程序教师节祝福,2015年教师节祝福语(大学生适用)
- Taro(React)实现具有滚动效果的倒数计时器
热门文章
- C# 系统应用之调用SDelete程序粉碎文件及基础原理知识
- 【数据结构与算法】之深入解析运用链表结构计算“两数相加”的算法实现
- 树莓派AI视觉云台——3、树莓派系统的启动
- JAVA数组及数组的应用
- ROS(12):双足机器人开发调研
- 【C++】 C++标准模板库(四) Set
- [Qt教程] 第38篇 网络(八)TCP(二)
- linux xps 13 wifi,XPS13-9350安装linux以及网卡连接失败修复的临时方案
- Sql注入工具php,phpcms框架SQL注入--基于Havij工具
- canoco5冗余分析步骤_Python怎么学习才能效果最好?网友:学习步骤在这了