Spring和Thrift集成,提供服务

As you all know,thrift可以通过servlet提供服务。如果能与Spring集成起来就更方便了,因为我们的大部分类是通过Spring注入的。

所以,下面介绍Spring和Thrift集成来提供服务。

 

原理是Spring的HttpRequestHandler。详见http://www.codeproject.com/Tips/251636/How-to-inject-Spring-beans-into-Servlets

 

第一步:定义基本HttpRequestHandler

package com.test.service.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.annotation.PostConstruct;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.thrift.TException;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.protocol.TProtocolFactory;
import org.apache.thrift.transport.TIOStreamTransport;
import org.apache.thrift.transport.TTransport;
import org.springframework.web.HttpRequestHandler;


public abstract class TestThriftServlet implements HttpRequestHandler{
    
    protected TProcessor processor ;

    protected static TProtocolFactory inProtocolFactory = new TCompactProtocol.Factory();

    protected static TProtocolFactory outProtocolFactory = new TCompactProtocol.Factory();
    
    @PostConstruct
    protected void invokeInit(){
        init();
    }
    
    abstract void init();
    
    @Override
    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if(processor == null){
            throw new RuntimeException("You have to override init method and init processor firstly");
        }

        TTransport inTransport = null;
        TTransport outTransport = null;

        try {
            response.setContentType("application/x-thrift");
            InputStream in = request.getInputStream();
            OutputStream out = response.getOutputStream();

            TTransport transport = new TIOStreamTransport(in, out);
            inTransport = transport;
            outTransport = transport;

            TProtocol inProtocol = inProtocolFactory.getProtocol(inTransport);
            TProtocol outProtocol = outProtocolFactory.getProtocol(outTransport);

            processor.process(inProtocol, outProtocol);
            out.flush();
        } catch (TException te) {
            throw new ServletException(te);
        }
    }

}

 第二步:提供我们的接口

其中service类就是我们需要注入的bean

package com.test.service.servlet;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.test.core.common.Logger;
import com.test.core.service.PasswordService;


@Component
public class FooThriftHandler extends TestThriftServlet{
    
    private static final Logger logger = Logger.getLogger(FooThriftHandler.class);
    
    @Autowired
    private ThriftService.Iface service;

    @Override
    void init() {
        processor = new ThriftService.Processor<ThriftService.Iface>(service);
        logger.info("FooThriftHandler init complete");
    }

}

 第三步,增加web.xml配置

<servlet>
          <servlet-name>fooThriftHandler</servlet-name>
          <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
     </servlet>
     <servlet-mapping>
          <servlet-name>fooThriftHandler</servlet-name>
          <url-pattern>/fooThriftHandler</url-pattern>
     </servlet-mapping>

 第四步,将FooThriftHandler增加到Spring的Bean中

------------------------------------------------------- 

这样子,我们使用http://127.0.0.1:8080/fooThriftHandler 就可以调用这个Thrift服务了

你可能感兴趣的:(spring,thrift)