Thrift-0.9.3的多服務接口實現

上篇文章講了單服務接口的實現,若是有多個接口怎麼辦?apache

還好thrift的後續版本提供了,下面就來講下怎麼實現,spa

這裏參考了文章: http://blog.csdn.net/hivon/article/details/11681977.net

服務端server

package service.server;blog

 

import org.apache.thrift.TMultiplexedProcessor;接口

import org.apache.thrift.TProcessor;ip

 

import org.apache.thrift.protocol.TBinaryProtocol;資源

 

import org.apache.thrift.server.THsHaServer;get

 

import org.apache.thrift.server.TServer;io

 

import org.apache.thrift.server.TThreadedSelectorServer;

 

import org.apache.thrift.transport.TFramedTransport;

 

import org.apache.thrift.transport.TNonblockingServerSocket;

 

import org.apache.thrift.transport.TNonblockingServerTransport;

 

import org.apache.thrift.transport.TTransportException;

 

import service.demo.Hello;

 

import service.demo.HelloServiceImpl;

 

public class MultiHelloServer {

 

public static void main(String[] args) {

 

try {

 

// TServerSocket serverTransport = new TServerSocket(7911);

TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(7911);

 

TFramedTransport.Factory transportFactory = new TFramedTransport.Factory();

 

// TNonblockingTransport.

// 設置協議工廠爲 TBinaryProtocol.Factory

TBinaryProtocol.Factory protocolFactory = new TBinaryProtocol.Factory();

 

// 關聯處理器與 Hello 服務的實現

// TProcessor processor = new Hello.Processor(new

// HelloServiceImpl());

TMultiplexedProcessor processor1 = new TMultiplexedProcessor();

processor1.registerProcessor("Service1"new Hello.Processor(new HelloServiceImpl()));

// newTopicService.Processor<TopicService.Iface>(new TopicImpl()));

 

TThreadedSelectorServer.Args tArgs = new TThreadedSelectorServer.Args(serverTransport);

 

tArgs.transportFactory(transportFactory);

 

tArgs.protocolFactory(protocolFactory);

 

tArgs.processor(processor1);

 

// TServer server = new TThreadPoolServer(tArgs);

 

TServer server = new TThreadedSelectorServer(tArgs);

 

System.out.println("Start server on port 7911...");

 

server.serve();

 

catch (TTransportException e) {

 

e.printStackTrace();

 

}

 

}

 

}

客戶端

package service.client;

 

 

 

import org.apache.thrift.protocol.TBinaryProtocol;

import org.apache.thrift.protocol.TMultiplexedProtocol;

import org.apache.thrift.protocol.TProtocol;

 

import org.apache.thrift.transport.TFramedTransport;

 

import org.apache.thrift.transport.TSocket;

 

import org.apache.thrift.transport.TTransport;

 

 

 

import service.demo.Hello;

 

 

 

public class MultiHelloClient {

 

 

 

    public static void main(String[] argsthrows Exception {

 

 

 

        // 設置傳輸通道,對於非阻塞服務,須要使用TFramedTransport,它將數據分塊發送

 

        TTransport transport = new TFramedTransport(new TSocket("127.0.0.1", 7911));

        transport.open(); 

        

 

        // 使用二進制協議

 

        TProtocol protocol = new TBinaryProtocol(transport); 

 

        // 建立Client

        TMultiplexedProtocol tmp = new TMultiplexedProtocol(protocol,"Service1");

        

        Hello.Client client = new Hello.Client(tmp); 

        

        

 

        long start = System.currentTimeMillis();

 

        for (int i = 0; i < 1; i++) {

 

            System.out.println("client.helloBoolean(false)---"+client.helloBoolean(false));

 

            //System.out.println("client.helloInt(111)---"+client.helloInt(111));

 

            //client.helloNull();

 

            System.out.println("client.helloString(\"360buy\")---"+client.helloString("360buy"));

 

            client.helloVoid();

 

        }

 

        System.out.println("耗時:" + (System.currentTimeMillis() - start)); 

 

        // 關閉資源

 

        transport.close();

 

    }

 

}

 

演示結果

相關文章
相關標籤/搜索