Thrift 半同步半异步的服务模型-THsHaServer(异步调用客户端)

本文详细介绍了如何使用Apache Thrift库实现服务端半同步半异步模型,并结合TFramedTransport进行数据传输。同时,展示了如何在客户端实现异步调用,包括使用TAsyncClientManager、TNonblockingSocket和TBinaryProtocol等组件。文章还探讨了如何通过SSL协议增加安全性,并提供了示例代码。

半同步半异步的服务端模型,需要指定为: TFramedTransport 数据传输的方式。

服务端HelloTHsHaServer.java:

package cn.slimsmart.thrift.demo.helloworld;

import org.apache.thrift.TException;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.THsHaServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;

/**
 * 注册服务端 半同步半异步的服务端模型,需要指定为: TFramedTransport 数据传输的方式。 THsHaServer
 * 非阻塞
 */
public class HelloTHsHaServer {
	// 注册端口
	public static final int SERVER_PORT = 8080;

	public static void main(String[] args) throws TException {
		TProcessor tprocessor = new HelloWorld.Processor<HelloWorld.Iface>(new HelloWorldImpl());
		// 传输通道 - 非阻塞方式  
		TNonblockingServerSocket serverTransport = new TNonblockingServerSocket(SERVER_PORT);
		//半同步半异步
		THsHaServer.Args tArgs = new THsHaServer.Args(serverTransport);
		tArgs.processor(tprocessor);
		tArgs.transportFactory(new TFramedTransport.Factory());
		//二进制协议
		tArgs.protocolFactory(new TBinaryProtocol.Factory());
		// 半同步半异步的服务模型
		TServer server = new THsHaServer(tArgs);
		System.out.println("HelloTHsHaServer start....");
		server.serve(); // 启动服务
	}
}
客户端HelloAsyncClient.java

package cn.slimsmart.thrift.demo.helloworld;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.apache.thrift.TException;
import org.apache.thrift.async.AsyncMethodCallback;
import org.apache.thrift.async.TAsyncClientManager;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocolFactory;
import org.apache.thrift.transport.TNonblockingSocket;
import org.apache.thrift.transport.TNonblockingTransport;

import cn.slimsmart.thrift.demo.helloworld.HelloWorld.AsyncClient.sayHello_call;

/**
 * 客户端异步调用,服务端需使用TNonblockingServer ,THsHaServer
 */
public class HelloAsyncClient {
	public static final String SERVER_IP = "127.0.0.1";
	public static final int SERVER_PORT = 8080;
	public static final int TIMEOUT = 30000;

	public static void main(String[] args) throws TException, IOException, InterruptedException {
		//异步调用管理器
		TAsyncClientManager clientManager = new TAsyncClientManager();
		//设置传输通道,调用非阻塞IO
		TNonblockingTransport transport = new TNonblockingSocket(SERVER_IP, SERVER_PORT, TIMEOUT);
		// 协议要和服务端一致
		//TProtocolFactory tprotocol = new TCompactProtocol.Factory();
		TProtocolFactory tprotocol = new TBinaryProtocol.Factory();
		
		HelloWorld.AsyncClient asyncClient = new HelloWorld.AsyncClient(tprotocol, clientManager, transport);
		CountDownLatch latch = new CountDownLatch(1);
		AsynCallback callBack = new AsynCallback(latch);
		System.out.println("call method sayHello start ...");
		// 调用服务
		asyncClient.sayHello("jack", callBack);
		System.out.println("call method sayHello .... end");
		//等待完成异步调用
		boolean wait = latch.await(30, TimeUnit.SECONDS);
		System.out.println("latch.await =:" + wait);
	}
}
class AsynCallback implements AsyncMethodCallback<sayHello_call> {
	private CountDownLatch latch;

	public AsynCallback(CountDownLatch latch) {
		this.latch = latch;
	}

	@Override
	public void onComplete(sayHello_call response) {
		System.out.println("onComplete");
		try {
			System.out.println("AsynCall result :" + response.getResult().toString());
		} catch (TException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			latch.countDown();
		}
	}
	@Override
	public void onError(Exception exception) {
		System.out.println("onError :" + exception.getMessage());
		latch.countDown();
	}
}

使用SSL加密协议:

//SSL服务端
		TSSLTransportParameters parameters = new TSSLTransportParameters();
		//keystore文件  密码
		parameters.setKeyStore("../../.keystore", "thrift");
		TServerTransport serverTransport = TSSLTransportFactory.getServerSocket(8080, 3000, null, parameters);
		
		//SSL客户端
		TSSLTransportParameters parameters = new TSSLTransportParameters();
		parameters.setTrustStore("../../.trustore", "thrift", "SunX509", "JKS");
		TTransport tTransport = TSSLTransportFactory.getClientSocket("127.0.0.1", 8080, 3000, parameters);
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值