Windows下Thrift安装与简单使用

安装

  1. 下载

    下载地址:http://archive.apache.org/dist/thrift/0.10.0/。将thrift-0.10.0.exe放到一个文件下,如F:\thrift下,将其重命名为thrift.exe。如果不重命名,需要使用thrift-0.10.0调用thrift命令。

  2. 配置环境变量

    向Path中添加变量值,值为thrift.exe的地址,如F:\thrift。

  3. 测试

    命令行输入thrift -version,如果输出thrift的版本即表明安装成功。

使用

  1. 编写IDL接口

    HelloService.thrift

    namespace java com.thrift.demo.service 
    
    service HelloService{ 
    	string sayHello(1:string username)
    }
    
  2. 编译

    编译之后会生成类HelloService。

    thrift -gen java HelloService.thrift
    
  3. 编写实现类

    HelloServiceImpl.java

    
    public class HelloServiceImpl implements HelloService.Iface {
    
    	@Override
    	public String sayHello(String username) throws TException {
    		return "Hello Thrift Service : " + username;
    	}
    
    }
    
  4. 编写服务端代码

    
    public class HelloServer {
    	public static final int SERVER_PORT = 8090;
    	 
    	public void startServer() {
    		try {
    			System.out.println("HelloService TSimpleServer start ....");
     
    			TProcessor tprocessor = new HelloService.Processor<HelloService.Iface>(new HelloServiceImpl());
    			
    			// 简单的单线程服务模型,一般用于测试
    			TServerSocket serverTransport = new TServerSocket(SERVER_PORT);
    			TServer.Args tArgs = new TServer.Args(serverTransport);
    			tArgs.processor(tprocessor);
    			tArgs.protocolFactory(new TBinaryProtocol.Factory());
    			TServer server = new TSimpleServer(tArgs);
    			server.serve();
     
    		} catch (Exception e) {
    			System.out.println("Server start error!!!");
    			e.printStackTrace();
    		}
    	}
    
    	public static void main(String[] args) {
    		HelloServer server = new HelloServer();
    		server.startServer();
    	}
    }
    
  5. 编写客户端代码

    
    public class HelloClient {
    	public static final String SERVER_IP = "localhost";
    	public static final int SERVER_PORT = 8090;
    	public static final int TIMEOUT = 30000;
     
    	public void startClient(String userName) {
    		TTransport transport = null;
    		try {
    			transport = new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT);
    			// 协议要和服务端一致
    			TProtocol protocol = new TBinaryProtocol(transport);
    			HelloService.Client client = new HelloService.Client(protocol);
    			transport.open();
    			String result = client.sayHello(userName);
    			System.out.println("Thrify client result =: " + result);
    		} catch (TTransportException e) {
    			e.printStackTrace();
    		} catch (TException e) {
    			e.printStackTrace();
    		} finally {
    			if (null != transport) {
    				transport.close();
    			}
    		}
    	}
    
    	public static void main(String[] args) {
    		HelloClient client = new HelloClient();
    		client.startClient("Michael");
     
    	}
    }
    
  6. 运行

    先运行服务端,再运行客户端。

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值