欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

thrift servlet

程序员文章站 2022-05-28 14:14:46
...

 

Thrift通常以独立服务存在,也支持HTTP协议,以servlet方式在Tomcat中运行。

 

一. 服务端Servlet

实现比较简单,只需实现TExtensibleServlet,并在getProcessor()中填充实现类。

/**
 * Thrift servlet
 */
@WebServlet(name="thrifttest",value="/thrifttest")
public class ThriftService extends TExtensibleServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected TProtocolFactory getInProtocolFactory() {
		TProtocolFactory factory = new TCompactProtocol.Factory();
		return factory;
	}

	@Override
	protected TProtocolFactory getOutProtocolFactory() {
		TProtocolFactory factory = new TCompactProtocol.Factory();
		return factory;
	}

	@Override
	protected TProcessor getProcessor() {
		// 接口实现类
		ThriftServiceImpl impl = new ThriftServiceImpl();

		// 返回处理器
		TProcessor tProcesser = new SimpleThriftService.Processor<SimpleThriftService.Iface>(impl); 
		return tProcesser;
	}
}

 

二. 客户端

/**
 * Http thrift client
 */
public static void httpClient() {  
	String url = "http://host:8080/servlet3/thrifttest";  
	try {
		TTransport transport = new THttpClient(url); 	         
		TProtocol protocol = new TCompactProtocol(transport); // HTTP通信协议     
		SimpleThriftService.Client client = new SimpleThriftService.Client(protocol);

		transport.open();  
		int val = client.getInt(10); 	// 调用方法
		System.out.println( "Val: " + val );

		transport.close();  
	} catch (TException e) {
		e.printStackTrace();
	}  
} 

 

三. 协议分析

thrift servlet 
            
    
    博客分类: java thriftservlet  

 

 

 

 

 

 

 

 

 

 

  • thrift servlet 
            
    
    博客分类: java thriftservlet 
  • 大小: 72.8 KB
相关标签: thrift servlet