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
代码详见附件。
相关推荐
spring-cloud-starter-thrift包括客户端spring-cloud-starter-thrift-client和服务端spring-cloud-starter-thrift-server两个模块。服务端:支持Apache Thrift的各种原生服务线程模型,包括单线程阻塞模型(simple)、...
- 创建Spring配置文件,如`thrift-server.xml`,配置Thrift服务器实例(通常是`TNonBlockingServer`或`TSimpleServer`)和Thrift服务处理器。 - 使用Spring的`<bean>`标签定义Thrift服务接口的实现,并通过`@...
【 Maven-Thrift-Server:构建Thrift服务的Maven实践】 在软件开发中,Thrift是一种高效的跨语言服务开发框架,由Facebook开发并开源。它允许定义数据类型和服务接口,然后自动生成各种编程语言的代码,使得不同...
maven-thrift-plugin-0.1.11.jar
如何连接项目非常简单: repositories { jcenter()}compile ' info.developerblog.spring.thrift:spring-thrift-starter:+ '如何使用服务器端注释@ThriftController(“ servlet_path”)帮助您构建服务器控制器以...
标题中的"thrift-0.9.1.exe"和"thrift-0.9.2.exe"是Thrift框架的不同版本。这些文件是Windows平台上的可执行程序,用于生成与Thrift相关的代码。0.9.1和0.9.2分别代表了Thrift的两个发行版本,每个版本可能包含了新...
【 Maven 和 Thrift 的结合:maven-thrift-client】 在软件开发中,Thrift 是一个强大的跨语言服务开发框架,由 Facebook 开发并开源。它允许开发者定义服务接口和服务数据类型,然后自动生成多种编程语言的客户端...
maven-thrift-plugin-0.1.11-sources.jar
maven插件 maven-thrift-plugin-0.1.10
"thrift-0.13.0.zip"这个压缩包包含了Thrift 0.13.0版本的源码和工具,用于生成Go语言的Thrift接口。Thrift IDL文件描述了HBase服务的接口,包括批量读写操作。批量操作在HBase中是非常重要的,因为它们可以显著提高...
spark-hive-thriftserver_2.11-2.1.spark-hive-thrift
1. **集成Thrift**:Thrift-Laravel项目提供了集成Thrift到Laravel的工具和示例,使得开发者可以在Laravel应用中轻松地创建Thrift服务提供商和消费者。 2. **服务提供者**:在Laravel中,服务提供者负责注册和绑定...
"thrift-0.9.3.exe"是Thrift框架的一个特定版本(0.9.3)的可执行文件,主要用于Windows操作系统。这个文件在Thrift开发过程中扮演着至关重要的角色,它能帮助开发者将定义好的Thrift接口文件(.thrift)转换为不同...
在“thrift-0.13.0.tar.gz”这个压缩包中,包含了Thrift 0.13.0版本的相关源码、库文件和文档。 Thrift IDL是Thrift的关键组成部分,它允许开发者用类似C++或Java的语法定义数据类型和服务接口。例如,你可以定义一...
thrift开发时,将thrift文件自动生成java文件需要用到thrift-0.9.0.exe
标题中的“Thrift--JSClient”指的是Apache Thrift在JavaScript客户端的使用。Apache Thrift是一种软件框架,用于构建跨语言的服务。它通过定义一个中间表示(IDL,接口定义语言)来构建服务,然后自动生成各种编程...
spark-hive_2.11-2.3.0...spark-hive-thriftserver_2.11-2.3.0.jar log4j-2.15.0.jar slf4j-api-1.7.7.jar slf4j-log4j12-1.7.25.jar curator-client-2.4.0.jar curator-framework-2.4.0.jar curator-recipes-2.4.0.jar
thrift开发时,将thrift文件自动生成java文件需要用到thrift-0.9.0.exe