码迷,mamicode.com
首页 > Web开发 > 详细

Apache thrift 安装及使用

时间:2016-11-17 13:29:37      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:types   targe   name   工厂   service   long   package   执行   block   

本篇介绍Apache thrift windows安装教程。

一.首先在Apache官网下载thrift 编译应用,戳这里下载 http://thrift.apache.org/download

    下载好windows 版本的exe文件。在c盘新建一个Thrift文件夹,将下载好的thrift-xxx.exe文件该名成 thrift.exe,放入Thrift文件夹中。

二.配置环境变量

    技术分享

 

 然后运行cmd命令,输入 thrift  -version。看到如下版本信息,则表示thrift 安装成功。

技术分享

是不是很easy! 接下来就可以干正事了。

三.编写thrift脚本文件,生产对应语言的代码,此处我使用java作为案例。

namespace java com.zhj.thrift.server  // defines the namespace   

typedef i32 int  //typedefs to get convenient names for your types  
typedef i16 short
typedef i64 long

service HelloService{
    int add(1:int num1,2:int num2)
    short getShort(1:short value)
    long getLong(1:long value)
    string sayHello(1:string name)

}

保存该文件命名为:helloService.thrift。之后执行该文件会生成一个helloService接口的java代码.

四.找到上述文件的目录,执行该文件。执行后会在相同的目录下生成一个gen-java的包。里面就生产了helloService.java的一个类

技术分享

五.新建一个Java 项目,将上述接口拷到项目中,并实现该接口。实现接口类如下:

技术分享
package com.zhj.thrift.server.impl;

import org.apache.thrift.TException;
import com.zhj.thrift.server.HelloService;

public class HelloServiceImpl implements HelloService.Iface {

    @Override
    public int add(int num1, int num2) throws TException {
        return num1+num2;
    }

    @Override
    public short getShort(short value) throws TException {
        return (short) (value*2);
    }

    @Override
    public long getLong(long value) throws TException {
        Long l_value = value - 24*60*60*1000;
        return l_value;
    }

    @Override
    public String sayHello(String name) throws TException {
        return "hello " + name;
    }

}
View Code

需要引入三个jar包

技术分享

libthrift-0.9.3 依赖包下载地址:http://repo1.maven.org/maven2/org/apache/thrift/libthrift/

 六.编写thrift服务端类,如下:

技术分享
package com.zhj.thrift.server;

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

import com.zhj.thrift.server.HelloService.Processor;
import com.zhj.thrift.server.impl.HelloServiceImpl;

public class HelloServiceServer {

    public static void main(String[] args) {
        try {
            // 设置服务器端口
            TServerSocket ts = new TServerSocket(9090);
            // 设置二进制协议工厂
            Factory protocolFactory = new TBinaryProtocol.Factory();
            //处理器关联业务实现
            Processor<HelloService.Iface> processor = new HelloService.Processor<HelloService.Iface>(new HelloServiceImpl());
            // 1. 使用单线程标准阻塞I/O模型
            TServer.Args simpleArgs = new TServer.Args(ts);
            simpleArgs.processor(processor);
            simpleArgs.protocolFactory(protocolFactory);
            TServer server = new TSimpleServer(simpleArgs);
            System.out.println("开启thrift服务器,监听端口:9090");
            server.serve();
            
             // 2. 使用线程池服务模型
           /* TThreadPoolServer.Args poolArgs = new TThreadPoolServer.Args(ts);
            poolArgs.processor(processor);
            poolArgs.protocolFactory(protocolFactory);
            TServer poolServer = new TThreadPoolServer(poolArgs);
            poolServer.serve();*/
           
        } catch (TTransportException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
View Code

七.编写thrift客户端类,如下:

技术分享
package com.zhj.thrift.client;

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.zhj.thrift.server.HelloService;

public class HelloServiceClient {

    public static void main(String[] args) {
        
        try {
            // 设置调用的服务地址-端口
            TTransport transport = new TSocket("127.0.0.1", 9090);
            //  使用二进制协议
            TProtocol protocol = new TBinaryProtocol(transport);
            // 使用的接口
            HelloService.Client client = new HelloService.Client(protocol);
            //打开socket
            transport.open();
            System.out.println("求和:"+client.add(100, 150));
            System.out.println(client.getLong(System.currentTimeMillis()));
            System.out.println(client.sayHello("李四"));
            System.out.println(client.getShort((short)10));
            
        } catch (TTransportException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
View Code

最后运行效果:

技术分享

至此,简单的thrift服务发布和调用已实现,有不合理之处请小伙伴指点出来,thank you!

 

Apache thrift 安装及使用

标签:types   targe   name   工厂   service   long   package   执行   block   

原文地址:http://www.cnblogs.com/sumingk/p/6073105.html

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