1、添加依賴 jar
<dependency> <groupId>org.apache.thrift</groupId> <artifactId>libthrift</artifactId> <version>0.8.0</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.1</version> </dependency>
2、編寫IDL文件 Hello.thrift
namespace java service.demo
service Hello {
string helloString(1:string para)
i32 helloInt(1:i32 para)
bool helloBoolean(1:bool para)
void helloVoid()
string helloNull()
}
3、生成代碼
thrift -o <output directory> -gen java Hello.thrift
生成代碼縮略圖:4、編寫實現類、實現Hello.Iface:
縮略圖:
5、編寫服務端,發布(阻塞式IO + 多線程處理)服務。
- /**
- * 阻塞式、多線程處理
- *
- * @param args
- */
- @SuppressWarnings({ "unchecked", "rawtypes" })
- public static void main(String[] args) {
- try {
- //設置傳輸通道,普通通道
- TServerTransport serverTransport = new TServerSocket(7911);
- //使用高密度二進制協議
- TProtocolFactory proFactory = new TCompactProtocol.Factory();
- //設置處理器HelloImpl
- TProcessor processor = new Hello.Processor(new HelloImpl());
- //創建服務器
- TServer server = new TThreadPoolServer(
- new Args(serverTransport)
- .protocolFactory(proFactory)
- .processor(processor)
- );
- System.out.println("Start server on port 7911...");
- server.serve();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
6、編寫客戶端,調用(阻塞式IO + 多線程處理)服務:
- public static void main(String[] args) throws Exception {
- // 設置傳輸通道 - 普通IO流通道
- TTransport transport = new TSocket("localhost", 7911);
- transport.open();
- //使用高密度二進制協議
- TProtocol protocol = new TCompactProtocol(transport);
- //創建Client
- Hello.Client client = new Hello.Client(protocol);
- long start = System.currentTimeMillis();
- for(int i=0; i<10000; i++){
- client.helloBoolean(false);
- client.helloInt(111);
- client.helloNull();
- client.helloString("dongjian");
- client.helloVoid();
- }
- System.out.println("耗時:" + (System.currentTimeMillis() - start));
- //關閉資源
- transport.close();
- }
現在已完成整個開發過程,超級無敵簡單。
其中服務端使用的協議需要與客戶端保持一致。
-------------------------------------------------------------------------------------------------------------------
上面展示了普通且常用的服務端和客戶端,下面請看非阻塞IO,即java中的NIO:
基于非阻塞IO(NIO)的服務端:
- public static void main(String[] args) {
- try {
- //傳輸通道 - 非阻塞方式
- TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(7911);
- //異步IO,需要使用TFramedTransport,它將分塊緩存讀取。
- TTransportFactory transportFactory = new TFramedTransport.Factory();
- //使用高密度二進制協議
- TProtocolFactory proFactory = new TCompactProtocol.Factory();
- //設置處理器 HelloImpl
- TProcessor processor = new Hello.Processor(new HelloImpl());
- //創建服務器
- TServer server = new TThreadedSelectorServer(
- new Args(serverTransport)
- .protocolFactory(proFactory)
- .transportFactory(transportFactory)
- .processor(processor)
- );
- System.out.println("Start server on port 7911...");
- server.serve();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
調用非阻塞IO(NIO)服務的客戶端:
- public static void main(String[] args) throws Exception {
- //設置傳輸通道,對于非阻塞服務,需要使用TFramedTransport,它將數據分塊發送
- TTransport transport = new TFramedTransport(new TSocket("localhost", 7911));
- transport.open();
- //使用高密度二進制協議
- TProtocol protocol = new TCompactProtocol(transport);
- //創建Client
- Hello.Client client = new Hello.Client(protocol);
- long start = System.currentTimeMillis();
- for(int i=0; i<10000; i++){
- client.helloBoolean(false);
- client.helloInt(111);
- client.helloNull();
- client.helloString("360buy");
- client.helloVoid();
- }
- System.out.println("耗時:" + (System.currentTimeMillis() - start));
- //關閉資源
- transport.close();
- }
-----------------------------------------------------------------------------------------------------------------------------------
客戶端異步調用:
- /** 調用[非阻塞IO]服務,異步 */
- public static void main(String[] args) {
- try {
- //異步調用管理器
- TAsyncClientManager clientManager = new TAsyncClientManager();
- //設置傳輸通道,調用非阻塞IO。
- final TNonblockingTransport transport = new TNonblockingSocket("localhost", 7911);
- //設置協議
- TProtocolFactory protocol = new TCompactProtocol.Factory();
- //創建Client
- final Hello.AsyncClient client = new Hello.AsyncClient(protocol, clientManager, transport);
- // 調用服務
- System.out.println("開始:" + System.currentTimeMillis());
- client.helloBoolean(false, new AsyncMethodCallback<Hello.AsyncClient.helloBoolean_call>() {
- public void onError(Exception exception) {
- System.out.println("錯誤1: " + System.currentTimeMillis());
- }
- public void onComplete(helloBoolean_call response) {
- System.out.println("完成1: " + System.currentTimeMillis());
- try {
- client.helloBoolean(false, new AsyncMethodCallback<Hello.AsyncClient.helloBoolean_call>() {
- public void onError(Exception exception) {
- System.out.println("錯誤2: " + System.currentTimeMillis());
- }
- public void onComplete(helloBoolean_call response) {
- System.out.println("完成2: " + System.currentTimeMillis());
- transport.close();
- }
- });
- } catch (TException e) {
- e.printStackTrace();
- }
- }
- });
- System.out.println("結束:" + System.currentTimeMillis());
- Thread.sleep(5000);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-----------------------------------------------------------------------------------------------------------------------------------
使用SSL的服務端:
調用基于SSL服務端的客戶端: