设为首页 收藏本站
查看: 2524|回复: 0

Thrift在windows7下的安装与实践

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-10-14 08:27:07 | 显示全部楼层 |阅读模式
首先上官网下载代码

https://thrift.apache.org/download

下载源码thrift-0.9.3.tar.gz

解压之后放在路径C:\thrift-0.9.3\thrift-0.9.3

并下载windows执行版thrift-0.9.3.exe

放在路径C:\thrift-0.9.3下


下载apache ant项目,用于打jar包

下载路径

http://ant.apache.org/bindownload.cgi

解压之后放在路径C:\apache-ant-1.9.7-bin\apache-ant-1.9.7

配置环境变量

ANT_HOME  : C:\apache-ant-1.9.7-bin\apache-ant-1.9.7

把C:\apache-ant-1.9.7-bin\apache-ant-1.9.7\bin\ant.bat复制到路径C:\thrift-0.9.3\thrift-0.9.3\lib\java下

在cmd中运行ant.bat,会生成jar包libthrift-0.9.3 在路径C:\thrift-0.9.3\thrift-0.9.3\lib\java\build


在路径C:\thrift-0.9.3中创建一个文本文件

复制代码

namespace java com.winwill.thrift
enum RequestType {
    SAY_HELLO,   //问好
    QUERY_TIME,  //询问时间}struct Request {   
    1: required RequestType type;  // 请求的类型,必选
    2: required string name;       // 发起请求的人的名字,必选
    3: optional i32 age;           // 发起请求的人的年龄,可选
    }

exception RequestException {   
1: required i32 code;   
2: optional string reason;
}
// 服务名
service HelloWordService {   
string doAction(1: Request request) throws (1:RequestException qe);
// 可能抛出异常。
}

保存为Test.thrift


在cmd中进入路径C:\thrift-0.9.3

执行命令thrift-0.9.3 -gen java Test.thrift

会在路径C:\thrift-0.9.3下生成一个文件夹gen-java


在Eclipse中创建工程TestThrift

按照简书教程生成package

com.winwill.thrift

把上面生成的gen-java中的代码复制到package中

并在package中创建代码,由于可能跟简书教程使用的版本不同,简书教程中的某些写法无法编译,

经过修改后使用如下代码

1.服务端

package com.winwill.thrift;


import org.apache.commons.lang3.StringUtils;
import org.apache.thrift.TException;

import java.util.Date;

public class HelloWordServiceImpl implements com.winwill.thrift.HelloWordService.Iface {
    // 实现这个方法完成具体的逻辑。
    public String doAction(com.winwill.thrift.Request request) throws com.winwill.thrift.RequestException, TException {
        System.out.println("Get request: " + request);
        if (StringUtils.isBlank(request.getName()) || request.getType() == null) {
            throw new com.winwill.thrift.RequestException();
        }
        String result = "Hello, " + request.getName();
        if (request.getType() == com.winwill.thrift.RequestType.SAY_HELLO) {
            result += ", Welcome!";
        } else {
            result += ", Now is " + new Date().toLocaleString();
        }
        return result;
    }
}

2.启动服务

package com.winwill.thrift;

import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TJSONProtocol;
import org.apache.thrift.protocol.TProtocolFactory;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TFastFramedTransport;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportFactory;
import org.slf4j.*;
import java.net.ServerSocket;

public class HelloWordServer {
    public static void main(String[] args) throws Exception {
        int port = 7912;
        String transport_type = "buffered";
        String protocol_type = "binary";
        String server_type = "thread-pool";
        String domain_socket = "";
//        ServerSocket socket = new ServerSocket(7912);
        // Protocol factory
        TProtocolFactory tProtocolFactory = null;
        if (protocol_type.equals("json")) {
          tProtocolFactory = new TJSONProtocol.Factory();
        } else if (protocol_type.equals("compact")) {
          tProtocolFactory = new TCompactProtocol.Factory();
        } else {
          tProtocolFactory = new TBinaryProtocol.Factory();
        }

        TTransportFactory tTransportFactory = null;

        if (transport_type.equals("framed")) {
          tTransportFactory = new TFramedTransport.Factory();
        } else if (transport_type.equals("fastframed")) {
          tTransportFactory = new TFastFramedTransport.Factory();
        } else { // .equals("buffered") => default value
          tTransportFactory = new TTransportFactory();
        }
        TServerSocket serverTransport = new TServerSocket(new TServerSocket.ServerSocketTransportArgs().port(port));;
        com.winwill.thrift.HelloWordService.Processor processor = new com.winwill.thrift.HelloWordService.Processor(new HelloWordServiceImpl());
        TServer.Args tServerArgs = new TServer.Args(serverTransport);
        tServerArgs.processor(processor);
        tServerArgs.protocolFactory(tProtocolFactory);
        tServerArgs.transportFactory(tTransportFactory);
        TServer server = new TSimpleServer(tServerArgs);
        System.out.println("Running server...");
        server.serve();
    }
}

3.客户端请求

package com.winwill.thrift;

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

public class HelloWordClient {
    public static void main(String[] args) throws Exception {
        TTransport transport = new TSocket("【此处使用服务器ip地址】", 7912);
        TProtocol protocol = new TBinaryProtocol(transport);

        // 创建client
        com.winwill.thrift.HelloWordService.Client client = new com.winwill.thrift.HelloWordService.Client(protocol);

        transport.open();  // 建立连接

        // 第一种请求类型
        com.winwill.thrift.Request request = new com.winwill.thrift.Request()
                .setType(com.winwill.thrift.RequestType.SAY_HELLO).setName("winwill2012").setAge(24);
        System.out.println(client.doAction(request));

        // 第二种请求类型
        request.setType(com.winwill.thrift.RequestType.QUERY_TIME).setName("winwill2012");
        System.out.println(client.doAction(request));

        transport.close();  // 请求结束,断开连接
    }
}

在一台服务器上测试启动服务

输出Running server...

在另一台机器启动客户端

输出

Hello, winwill2012, Welcome!

Hello, winwill2012, Now is 2016-10-13 17:06:47

此时服务器端输出

Get request: Request(type:SAY_HELLO, name:winwill2012, age:24)

Get request: Request(type:QUERY_TIME, name:winwill2012, age:24)

说明已经成功连接啦

本文所用到的工具和工程已经打包上传到云盘,欢迎大家下载

链接:http://pan.baidu.com/s/1jHQXSma 密码:65uh

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-285699-1-1.html 上篇帖子: 请问我的票证通系统怎么不见了,我无法打票证通,怎么办? 下篇帖子: SCCM OSD 部署Windows 8过程中跳过WIFI连接 windows7
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表