作为构建业务服务的发布者,一般情况下我们发布thrift的服务,会是这样
public class Server {
@SuppressWarnings({ "unchecked", "rawtypes" })
private void start(int port) {
try {
TServerSocket serverTransport = new TServerSocket(port);
ArithmeticService.Processor processor = new ArithmeticService.Processor(new ArithmeticServiceImpl());
TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));
server.serve();
} catch (TTransportException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Server().start(8082);
}
}
其中,ArithmeticService是我们在接口中定义的业务A接口,在SERVER这边进行发布。不过,当我们又有一个业务B,想跟这个A在一起部署。这样,就得用上TMultiplexedProcessor,相当于可以在一个端口下面发布多个业务服务。具体代码如下
public class Main {
public static void main(String[] args) throws Exception {
ApplicationContext app = new GenericXmlApplicationContext("classpath:/applicationContext-server.xml");
Main.start(app);
}
@SuppressWarnings({"unchecked", "rawtypes"})
private static void start(ApplicationContext app) {
Properties pro = (Properties) app.getBean("config");
int thriftPort = NumberUtils.toInt(pro.getProperty("thrift.port"));
try {
TServerSocket serverTransport = new TServerSocket(thriftPort);
//sms
SmsService.Iface smsIface = app.getBean( SmsService.Iface.class);
SmsService.Processor smsProcessor = new SmsService.Processor(smsIface);
//email
EmailService.Iface emailIface = app.getBean( EmailService.Iface.class);
EmailService.Processor emailProcessor = new EmailService.Processor(emailIface);
//注册多个TMultiplexedProcessor
TMultiplexedProcessor tMultiplexedProcessor = new TMultiplexedProcessor();
tMultiplexedProcessor.registerProcessor(SmsService.class.getName()+"$Client",smsProcessor);
tMultiplexedProcessor.registerProcessor(EmailService.class.getName()+"$Client", emailProcessor);
TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(tMultiplexedProcessor));
server.serve();
} catch (TTransportException e) {
e.printStackTrace();
}
}
}
注意看TMultiplexedProcessor ,利用它可以注册多个业务服务。上面这段代码块,主要注册了短信发送服务,邮件发送服务。服务启动成功后,可以利用我之前改写的thrift-pool-client
调用示例如下
public class ThriftMain {
public static void main(String[] args) throws TException {
// customize pool config
GenericKeyedObjectPoolConfig poolConfig = new GenericKeyedObjectPoolConfig();
// ... customize pool config here
// customize transport, while if you expect pooling the connection, you should use TFrameTransport.
Function<ThriftServerInfo, TTransport> transportProvider = new Function<ThriftServerInfo, TTransport>() {
public TTransport apply(ThriftServerInfo info) {
// TODO Auto-generated method stub
TSocket socket = new TSocket(info.getHost(), info.getPort());
return socket;
}
};
Function<List<ThriftServerInfo>, List<ThriftServerInfo>> addElementFunction =
new Function<List<ThriftServerInfo>, List<ThriftServerInfo>>() {
public List<ThriftServerInfo> apply(List<ThriftServerInfo> list) {
return Arrays.asList(//
ThriftServerInfo.of("localhost", 8082)//
// or you can return a dynamic result.
);
}
};
Supplier<List<ThriftServerInfo>> list = Suppliers.compose(addElementFunction, new Supplier<List<ThriftServerInfo>>(){
public List<ThriftServerInfo> get() {
// TODO Auto-generated method stub
return Arrays.asList(//
ThriftServerInfo.of("localhost", 8082)//
// or you can return a dynamic result.
);
}});
ThriftClient thriftClient = new ThriftClientImpl( list,new DefaultThriftConnectionPoolImpl(poolConfig, transportProvider));
thriftClient.iface(SmsService.Client.class).sendSms("*******","尊敬的用户:欢迎使用****。您的登录名是:" + "**********" + ",密码为02145,如非本人操作,请不要理会.");
}
}
本篇没有涉及到thrift的接口定义等相关技术点,再启篇幅吧。
网友评论