THRIFT服务器缺省无法获取客户端连接的地址。经过研究,发现通过重载transport factory+Thread local,能够简单解决。不需要修改thrift生成的代码。
>>>>>
arg.protocolFactory(protocolFactory);
arg.outputTransportFactory(new TFramedTransport.Factory());
arg.inputTransportFactory(new TFramedTransport.Factory() {
@Override
public TTransport getTransport(TTransport base) {
if(base instanceof TSocket) {
TSocket _base = (TSocket) base;
Socket socket = _base.getSocket();
if(socket != null) {
String remoteAddr = String.valueOf(socket.getRemoteSocketAddress());
ThreadLocalIpUtils.setIp(remoteAddr);
}
}
return new TFramedTransport(base) {
@Override
public void close() {
ThreadLocalIpUtils.remove();
super.close();
}
};
}
});
>>>>>
arg.protocolFactory(protocolFactory);
arg.outputTransportFactory(new TFramedTransport.Factory());
arg.inputTransportFactory(new TFramedTransport.Factory() {
@Override
public TTransport getTransport(TTransport base) {
if(base instanceof TSocket) {
TSocket _base = (TSocket) base;
Socket socket = _base.getSocket();
if(socket != null) {
String remoteAddr = String.valueOf(socket.getRemoteSocketAddress());
ThreadLocalIpUtils.setIp(remoteAddr);
}
}
return new TFramedTransport(base) {
@Override
public void close() {
ThreadLocalIpUtils.remove();
super.close();
}
};
}
});