码迷,mamicode.com
首页 > 其他好文 > 详细

Thirft简单使用

时间:2017-11-06 15:03:44      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:logs   maven2   down   下载地址   world   dem   编写   demo   nload   

安装Thrift

到thrift官网下载thrift.exe

http://thrift.apache.org/download

 技术分享

技术分享

thrift-0.10.0.exe复制到C:\Program Files\thrift 并改名为thrift.exe

 技术分享

 

配置环境变量

 技术分享

 

打开dos窗口输入thrift -version 查看thrift安装版本,出现下图,则成功

 技术分享

使用Thrift

编写thrift接口文件并生成java文件

编写thrift接口文件HelloWorld.thrift,并放在Daaa文件夹下

  

namespace java com.thrift.demo

service HelloWorldService {
    string sayHello(1:string username)
}

技术分享

 

运行命令thrift -o D:\aaaa -gen java D:\aaa\HelloWorld.thrift

说明:使用-o参数指定输出路径

 技术分享

 

aaaa下就会生成所需要的java文件

 技术分享

技术分享

 

使用由Thrift文件生成的java文件

将接口文件拷贝到自己的工程中,并导入相关jar

Thriftjar包下载地址如下,可以修改版本号来下载与exe版本对应的jar

http://repo1.maven.org/maven2/org/apache/thrift/libthrift/0.10.0/

 技术分享

 

编写接口实现代码

 

package com.thrift.demo;

import org.apache.thrift.TException;

import com.thrift.demo.HelloWorldService.Iface;

public class HelloWorldServiceImpl implements Iface{

     private static int count = 0;  
    
    @Override
    public String sayHello(String username) throws TException {
        count += 1;  
        System.out.println("get " + username + " " +count);   
        return "hello " + username + " " + count;  
    }

}


编写
server代码

 Thrift相关jar包下载:https://github.com/xiaorenwu-dashijie/Thrift.git

package com.thrift.demo;

import org.apache.thrift.protocol.TBinaryProtocol;  
import org.apache.thrift.protocol.TBinaryProtocol.Factory;  
import org.apache.thrift.server.TServer;  
import org.apache.thrift.server.TThreadPoolServer;  
import org.apache.thrift.server.TThreadPoolServer.Args;  
import org.apache.thrift.transport.TServerSocket;  
import org.apache.thrift.transport.TTransportException; 

import com.thrift.demo.HelloWorldService;
import com.thrift.demo.HelloWorldService.Processor;

public class Server {
    public void startServer() {  
        try {  
            System.out.println("thrift server open port 1234");
            TServerSocket serverTransport = new TServerSocket(1234);  
            HelloWorldService.Processor process = new Processor(new HelloWorldServiceImpl());  
            Factory portFactory = new TBinaryProtocol.Factory(true, true);  
            Args args = new Args(serverTransport);  
            args.processor(process);  
            args.protocolFactory(portFactory);  
            TServer server = new TThreadPoolServer(args);  
            server.serve();  
        } catch (TTransportException e) {  
            e.printStackTrace();  
        }  
    }  
      
    public static void main(String[] args) {  
        System.out.println("thrift server init");
        Server server = new Server();  
        System.out.println("thrift server start");
        server.startServer();  
        System.out.println("thrift server end");
    }  
}

 

编写client 代码

 

package com.thrift.demo;

import org.apache.thrift.TException;  
import org.apache.thrift.protocol.TBinaryProtocol;  
import org.apache.thrift.protocol.TProtocol;  
import org.apache.thrift.transport.TSocket;  
import org.apache.thrift.transport.TTransport;  
import org.apache.thrift.transport.TTransportException; 

import com.thrift.demo.HelloWorldService;

public class Client {
    public void startClient() {  
        TTransport transport;  
        try {  
            System.out.println("thrift client connext server at 1234 port ");
            transport = new TSocket("localhost", 1234);  
            TProtocol protocol = new TBinaryProtocol(transport);  
            HelloWorldService.Client client = new HelloWorldService.Client(protocol);  
            transport.open();  
            System.out.println(client.sayHello("panguso"));  
            transport.close();  
            System.out.println("thrift client close connextion");
        } catch (TTransportException e) {  
            e.printStackTrace();  
        } catch (TException e) {  
            e.printStackTrace();  
        }  
    }  
  
    public static void main(String[] args) {  
        System.out.println("thrift client init ");
        Client client = new Client();  
        System.out.println("thrift client start ");
        client.startClient();  
        System.out.println("thrift client end ");
    }  
}


运行
serverclient代码 

启动server

 技术分享

 

启动client

 技术分享

 

Server端输出

 技术分享

 

Thirft简单使用

标签:logs   maven2   down   下载地址   world   dem   编写   demo   nload   

原文地址:http://www.cnblogs.com/java-spring/p/7792839.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!