1)安装thrift:到thrift官网下载exe文件,然后将文件重命名为thrift.exe,拷贝到c:\windows目录下(或者任何目录下),然后就可以在dos环境下使用了

c:\windows>thrift -gen java D:\mywork\javaProject\thriftTest\test.thrift ,输出的java文件默认输出到当前目录下c:\windows,也可以使用-o参数指定输出路径

2)下载相关依赖包

2.1)libthrift.jar ,下载地址:http://repo1.maven.org/maven2/org/apache/thrift/libthrift/0.9.0/

2.2)slf4j-api.jar

2.3)slf4j-simple.jar

3)编写thrift 接口文件

  1. namespace cpp zam.thrift.test
  2. namespace py thriftTest
  3. namespace java com.zam.thrift.test
  4. namespace php thriftTest
  5. service Hello {
  6. string helloString(1:string word)
  7. }

4)编写接口实现代码

  1. package com.zam.server;
  2. import org.apache.thrift.TException;
  3. import com.zam.thrift.test.Hello.Iface;
  4. public class HelloImpl implements Iface{
  5. private static int count = 0;
  6. @Override
  7. public String helloString(String word) throws TException {
  8. // TODO Auto-generated method stub
  9. count += 1;
  10. System.out.println("get " + word + " " +count);     return "hello " + word + " " + count;
  11. }
  12. }

5)编写server代码

  1. package com.zam.server;
  2. import org.apache.thrift.protocol.TBinaryProtocol;
  3. import org.apache.thrift.protocol.TBinaryProtocol.Factory;
  4. import org.apache.thrift.server.TServer;
  5. import org.apache.thrift.server.TThreadPoolServer;
  6. import org.apache.thrift.server.TThreadPoolServer.Args;
  7. import org.apache.thrift.transport.TServerSocket;
  8. import org.apache.thrift.transport.TTransportException;
  9. import com.zam.thrift.test.Hello;
  10. import com.zam.thrift.test.Hello.Processor;
  11. public class Server {
  12. public void startServer() {
  13. try {
  14. System.out.println("thrift server open port 1234");
  15. TServerSocket serverTransport = new TServerSocket(1234);
  16. Hello.Processor process = new Processor(new HelloImpl());
  17. Factory portFactory = new TBinaryProtocol.Factory(true, true);
  18. Args args = new Args(serverTransport);
  19. args.processor(process);
  20. args.protocolFactory(portFactory);
  21. TServer server = new TThreadPoolServer(args);
  22. server.serve();
  23. }
  24. catch (TTransportException e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. public static void main(String[] args) {
  29. System.out.println("thrift server init");
  30. Server server = new Server();
  31. System.out.println("thrift server start");
  32. server.startServer();
  33. System.out.println("thrift server end");
  34. }
  35. }

6)编写client 代码

  1. package com.zam.server;
  2. import org.apache.thrift.TException;
  3. import org.apache.thrift.protocol.TBinaryProtocol;
  4. import org.apache.thrift.protocol.TProtocol;
  5. import org.apache.thrift.transport.TSocket;
  6. import org.apache.thrift.transport.TTransport;
  7. import org.apache.thrift.transport.TTransportException;
  8. import com.zam.thrift.test.Hello;
  9. public class Client {
  10. public void startClient() {
  11. TTransport transport;
  12. try {
  13. System.out.println("thrift client connext server at 1234 port ");
  14. transport = new TSocket("localhost", 1234);
  15. TProtocol protocol = new TBinaryProtocol(transport);
  16. Hello.Client client = new Hello.Client(protocol);
  17. transport.open();
  18. System.out.println(client.helloString("panguso"));
  19. transport.close();
  20. System.out.println("thrift client close connextion");
  21. } catch (TTransportException e) {
  22. e.printStackTrace();
  23. } catch (TException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. public static void main(String[] args) {
  28. System.out.println("thrift client init ");
  29. Client client = new Client();
  30. System.out.println("thrift client start ");
  31. client.startClient();
  32. System.out.println("thrift client end ");
  33. }
  34. }

8)运行server和client代码

8.1)启动server端

  1. thrift server init
  2. thrift server start
  3. thrift server open port 1234

8.2)启动client端

  1. thrift client init
  2. thrift client start
  3. thrift client connext server at 1234 port
  4. hello panguso 1
  5. thrift client close connextion
  6. thrift client end

下载地址:http://download.csdn.net/download/liyonghui123/5742211

windows配置thrift开发环境的更多相关文章

  1. windows配置php开发环境

    1.安装xampp. xampp集成了php.prel.mysql.apache等网站工具,安装超简单,本身也超级好用.点击下载xampp 2.讲xmapp中的php配置到环境变量 比如我的xampp ...

  2. 原创:超简单!windows配置NDK开发环境使用JNI

    前段时间看android版的opencv的配置教程时,看到了它的NDK配置方法,感觉简单又不会出错!!! 1.下载NDK,设置NDK路径: 在windows的系统环境变量中添加NDK的路径,环境变量名 ...

  3. windows配置Lua开发环境

    官网上下载的不靠谱,很多库不能用,你如 require "socket" 如果没有vc那些编译也麻烦,摸索了很久,直接下载安装文件安装即可: 安装这两个就行: 先安装C++2005 ...

  4. Windows下配置Java开发环境

    学习Java第一步是配置本地开发环境,学习最基本的桌面开发,下面以win7为例配置Java开发环境,即:JDK+JRE+Eclipse,安装JDK的时候会默认安装JRE,根据提示安装就可以了. 首先去 ...

  5. 【Objective-C】Windows下Objective-C开发环境配置

    [Objective-C]Windows下Objective-C开发环境配置 ftp://ftpmain.gnustep.org/pub/gnustep/binaries/windows/   最近打 ...

  6. Windows下Eclipse+PyDev配置Python开发环境

    1.简介 Eclipse是一款基于Java的可扩展开发平台.其官方下载中包括J2EE.Java.C/C++.Android等诸多版本.除此之外,Eclipse还可以通过安装插件的方式进行包括Pytho ...

  7. IIS服务器 远程发布(Web Deploy)配置 VS2010 开发环境 Windows Server 2008服务器系统

    原文:IIS服务器 远程发布(Web Deploy)配置 VS2010 开发环境 Windows Server 2008服务器系统 asp.net 网站有三种常用的发布方式:分别是拷贝开发机上发布好的 ...

  8. windows下spark开发环境配置

    http://www.cnblogs.com/davidwang456/p/5032766.html windows下spark开发环境配置 --本篇随笔由同事葛同学提供. windows下spark ...

  9. Windows上Ruby开发环境的配置

    最近公司项目上有需要,需要开发一个puppet的自动化工具,这个工具需要操作存储设备上的各种资源,而鉴于puppet不是善于完成这个任务的首选语言,于是我们选择了puppet的“爹”,Ruby. 熟悉 ...

随机推荐

  1. C# iis 错误配置信息( CS0016: 未能写入输出文件 )

    IIS发布时,编译器错误消息: CS0016: 未能写入输出文件 "c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.N ...

  2. Could not find the following Boost libraries: boost_python3

    安装Boost.NumPy时报错: CMake Error at /usr/share/cmake/Modules/FindBoost.cmake:1794 (message): Unable to ...

  3. Install Shield 打包教程

    我的是已经下载过打包工具InstallShield2013LimitedEdition,没有下载的只有下面那个灰色的的图标,不过没关系选中灰色的点确定直接跳到下载页面了.下载完成后再重新添加安装和部署 ...

  4. UCS2和UTF16有区别

    UCS2是定长的,固定2个字节,所以不能支持扩展字符,而UTF16是变长的.   UCS2是落伍的.   msdn里有这样一段描述: UCS-2 is a predecessor of UTF-16. ...

  5. 今天使用bootstrap中的tagsinput控件,碰到个小问题

    我直接使用时,后台一直报Uncaught Can't add objects when itemValue option is not set错误, 代码如下: <div> <inp ...

  6. Office 365 系列四 ------ 绑定公司域名

    Office 365包含了企业邮箱服务(Exchange Online),我们如果要用微软的企业邮箱,那么我们必须绑定我们公司的自己域名,而不是用微软 提供的二级域名,其实微软的整个Exchange ...

  7. mssql分页存储过程

    本文转自百度文库http://wenku.baidu.com/view/8f6ec149fe4733687e21aa72.html 必须有主键 原代码 Codeuse users go if exis ...

  8. 平板上的js和电脑上js的不同之处

    一.事件 1.平板上没有:onmousedown,onmouseup,onmousemove等事件,由ontouchstart,ontouchmove,ontounchend替代 2.位置问题:平板上 ...

  9. HashMap的两种实现方式

    本文主要简要分析了Java中和Redis中HashMap的实现,并且对比了两者的异同 1.Java的实现 下图表示了Java中一个HashMap的主要实现方式 因为大家对于Java中HashMap的实 ...

  10. SpringRMI远程方法调用

    Spring为各种远程访问技术的集成提供了工具类. 该小段引用自 http://www.open-open.com/lib/view/open1408957290478.html Spring远程支持 ...