现在,我正在使用Java语言使用Apache Thrift。
使用 THsHaServer 时,我在跟踪服务器上的IP客户端时遇到问题。当我使用 TThreadPoolServer 时,可以在服务器处理器中获取 TSocket 的客户端,但是它不适用于非阻塞服务器,特别是 THsHaServer 。
我认为 TFramedTransport 包装了 TSocket ,所以我无法检索 TSocket 。
如何在Thrift中的非阻塞服务器上获取IP客户端?
这是我的代码
节俭文件
service AuthenticationService {
bool isAuthenticated(1: string username, 2: string password)
}
简体-服务器
public class AuthTrackingHsHaServer {
private AuthenticationServiceHandler handler;
private AuthProcessor processor;
private TNonblockingServerTransport serverTransport;
private TServer server;
public AuthTrackingHsHaServer() {
try {
initProcesser();
initTransport();
initServer();
} catch (TTransportException ex) {
Logger.getLogger(AuthTrackingHsHaServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void initProcesser() {
this.handler = new AuthenticationServiceHandler();
this.processor = new AuthProcessor(handler);
}
private void initTransport() throws TTransportException {
serverTransport = new TNonblockingServerSocket(Config.PORT_AUTHENTICATION_SERVICE);
}
private void initServer() {
THsHaServer.Args args
= new THsHaServer.Args(serverTransport)
.transportFactory(new TFramedTransport.Factory(Integer.MAX_VALUE))
.protocolFactory(new TBinaryProtocol.Factory())
.processor(processor)
.executorService(new ThreadPoolExecutor(3, 10, 5, TimeUnit.DAYS, new LinkedBlockingDeque<>()));
args.maxReadBufferBytes = Long.MAX_VALUE;
server = new THsHaServer(args);
}
public void start() throws TTransportException {
System.out.println("Authentication service server is listening on port " + Config.PORT_AUTHENTICATION_SERVICE + " ...");
server.serve();
}
public static void main(String[] args) {
AuthTrackingHsHaServer server = new AuthTrackingHsHaServer();
try {
server.start();
} catch (TTransportException ex) {
Logger.getLogger(AuthTrackingHsHaServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
已简化-AuthProcessor,是从Processor扩展的
public class AuthProcessor extends AuthenticationService.Processor {
public AuthProcessor(AuthenticationService.Iface iface) {
super(iface);
}
@Override
public boolean process(TProtocol in, TProtocol out) throws TException {
TTransport t = in.getTransport();
TFramedTransport framedTransport = (TFramedTransport) t;
// TODO: How to get TSocket in TFramedTransport
return super.process(in, out); //To change body of generated methods, choose Tools | Templates.
}
}
已简化-处理器的处理程序
public class AuthenticationServiceHandler implements AuthenticationService.Iface {
@Override
public boolean isAuthenticated(String username, String password) throws TException {
return username.equals("abc") && password.equals("12345");
}
}