一、前言
上一篇文章介绍了spring boot集成thrift分别在jar和war的情况下启动thrift服务。从文章中可以发现,客户端获取的信息是在服务端固定的参数。如何处理我们的业务呢?这将是本篇文章的重点。话不多说直接开始... ...
二、项目整改
在springboot中要处理业务中的信息,需要获取项目中的上下文信息即可,具体操作如下:
- 2.1 修改thrift服务(ThriftServer)
package cn.bintools.daios.example.thrift.server;
import cn.bintools.daios.example.thrift.HelloService;
import cn.bintools.daios.example.thrift.impl.HelloServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportException;
import org.apache.thrift.transport.TTransportFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* 初始化thrift服务
*
* @author <a href="jian.huang@bintools.cn">yunzhe</a>
* @version 1.0.0 2019-07-02-下午7:10
*/
@Component
@Slf4j
public class ThriftServer implements ApplicationContextAware,InitializingBean {
@Value("${thrift.port}")
private int port;
private TBinaryProtocol.Factory protocolFactory;
private TTransportFactory transportFactory;
//通过实现ApplicationContextAware,InitializingBean 获取上下文信息
private static ApplicationContext context;
public void init(){
protocolFactory = new TBinaryProtocol.Factory();
transportFactory = new TTransportFactory();
}
public void start(){
new Thread(){
@Override
public void run(){
TProcessor processor = new HelloService.Processor<HelloService.Iface>(new HelloServiceImpl());
init();
try{
TServerSocket serverSocket = new TServerSocket(port);
TThreadPoolServer.Args args = new TThreadPoolServer.Args(serverSocket);
args.protocolFactory(protocolFactory);
args.processor(processor);
args.transportFactory(transportFactory);
TServer server = new TThreadPoolServer(args);
log.info("thrift server start success, port={}",port);
server.serve();
}catch (TTransportException e){
log.error("thrift server start fail",e);
}
}
}.start();
}
// 获取上下文
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context=applicationContext;
}
@Override
public void afterPropertiesSet() throws Exception {
}
}
- 2.2 服务端逻辑处理 (HelloServiceImpl)
需要对该类实现ApplicationListener<ApplicationContextEvent>实现其中的onApplicationEvent方法。具体代码如下
package cn.bintools.daios.example.thrift.impl;
import cn.bintools.daios.example.thrift.ConnectionInfo;
import cn.bintools.daios.example.thrift.HelloService;
import cn.bintools.daios.example.thrift.server.BusinessServer;
import org.apache.thrift.TException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ApplicationContextEvent;
import org.springframework.stereotype.Service;
/**
* 服务端实现
*
* @author <a href="jian.huang@bintools.cn">yunzhe</a>
* @version 1.0.0 2019-07-02-下午7:14
*/
@Service
public class HelloServiceImpl implements HelloService.Iface,ApplicationListener<ApplicationContextEvent> {
private static ApplicationContext applicationContext;
@Override
public int add(int num1, int num2) throws TException {
//通过上下文实例化具体的bean
BusinessServer server = applicationContext.getBean(BusinessServer.class);
return server.business();
}
@Override
public ConnectionInfo getConnInfoById(int cpId) throws TException {
ConnectionInfo connectionInfo = new ConnectionInfo();
connectionInfo.setConnId(12);
connectionInfo.setConnectionName("ThrfaceiftConnName");
connectionInfo.setUrl("192.168.1.162");
connectionInfo.setPort(3306);
connectionInfo.setUserName("mysql_conn");
connectionInfo.setPassword("abc*&ABC123");
return connectionInfo;
}
@Override
public void onApplicationEvent(ApplicationContextEvent event) {
//从event中获取上下文信息
applicationContext = event.getApplicationContext();
}
}
通过对这两个文件进行相应的处理,就可以实现通过thrift实现具体的业务处理。
码云地址:
https://gitee.com/huangjian163/thriftBusiness.git
三、总结
通过以上测试,可以让thrift进行相应的处理。感觉还有更优的解决方案,希望大家多多指点