# thrift-client-support **Repository Path**: arvin/thrift-client-support ## Basic Information - **Project Name**: thrift-client-support - **Description**: Thrift客户端支持 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2017-08-23 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # thrift-client-support Thrift客户端支持, 降低使用Thrift的复杂度,利用连接池技术、JDK动态代理技术实现快速接入Thrift服务 # 发布jar到Maven中央仓库 经个人整理,撰写了两篇博文详细描述了将自己的组件发布到Maven中央仓库,可参考: # 使用方式 可参考测试类: `net.oschina.thrift.test.ClientTest` ## 测试准备 ### 编写thrift协议文件 本质上就是接口定义,定义方法调用的协议, 参考test目录下的 HiService.thrift, 该文件仅仅定义了一个方法: ```thrift namespace java net.oschina.thrift.test.gen service HiService { string sayHi(1:string name) } ``` ### 根据thrift文件生成代码 首先你得下载thrift工具: 0.5 及其以下版本的: http://archive.apache.org/dist/incubator/thrift/ 0.6 及其以上版本的: http://archive.apache.org/dist/thrift/ 选择合适的版本下载,然后在命令行输入命令生成文件 thrift-0.10.0.exe -gen java HiService.thrift 上面的命令将生成文件到 当前目录的gen-java下面,拷贝到正确的目录下即可 ### 编写客户端Spring文件 配置文件可以参考: `applicationContext-thrift-client-test.xml`, 其内容如下: ```xml ``` ### 编写测试类 请查看测试类: `net.oschina.thrift.test.ClientTest` 该类的源码如下: ```java public class ClientTest { /** * 发布Thrift 服务 */ public void publishThriftService() throws Exception { HiService.Iface hiService = new HiServiceImpl(); TSConfig config = new TSConfig(); config.setHost("127.0.0.1"); config.setPort(9090); config.setTimeout(2000); ThriftServiceServerPublisher publisher = new ThriftServiceServerPublisher(); publisher.setConfig(config); publisher.setService(hiService); // 发布 System.out.println("准备发布....."); publisher.afterPropertiesSet(); System.out.println("发布成功....."); } @Before public void ready() throws Exception { publishThriftService(); } @Test public void testClient() throws TException { ApplicationContext acx = new ClassPathXmlApplicationContext("classpath:applicationContext-thrift-client-test.xml"); HiService.Iface hiService = acx.getBean(HiService.Iface.class); Assert.assertEquals("Hi, Arvin", hiService.sayHi("Arvin")); } } ``` ### 运行测试 直接运行单元测试即可