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. Tomcat并发数优化,修改service.xml性能调优 增加最大并发连接数

    可以在控制台的启动信息里看见,默认状态下没有被打开nio配置,启动时的信息,如下: 2010-2-1 12:59:40 org.apache.coyote.http11.Http11Protocol ...

  2. IE6-8下自定义标签的表现

    IE6-8是支持自定义标签,但只支持<xxx-yyy />这样的形式,它会翻译成<xxx-yyy></xxx-yyy> 如果你想内套子标签会失败, <ms-k ...

  3. apache配置多域名多站点记录

    <VirtualHost *:80>  DocumentRoot "/mnt/web/www.*.cn"  ServerName www.*.cn  ErrorLog ...

  4. RequireJS基础(三)

    这篇来写一个具有依赖的事件模块event. event提供三个方法bind.unbind.trigger来管理DOM元素事件. event依赖于cache模块,cache模块类似于jQuery的$.d ...

  5. [ASP.NET]

    public static int iA; protected void Button1_Click(object sender, EventArgs e) { //Label1.Text Respo ...

  6. vim编辑器详解

    vi(vim)是上Linux非常常用的编辑器,很多Linux发行版都默认安装了vi(vim).vi(vim)命令繁多但是如果使用灵活之后将会大大提高效率. vi是“visual interface”的 ...

  7. machine leanring 笔记 octave命令笔记

    来源于coursea 的公开课 A*B 一般意义的矩阵相乘 A.*B矩阵各位相乘 A.^2 A矩阵的每个数平方 1./A  对A矩阵的各位取倒 .表示对每一项都如此操作 log (A) exp(A) ...

  8. 单位换算(格式化十进制数-B),获取时间工具类CommenUtil

    package com.example.administrator.filemanager.utils;import java.text.DecimalFormat;import java.text. ...

  9. [Docker] docker 基础学习笔记2(共6篇)

    febootstrap 是一个自制image的一个第三方的工具,好像他们现在都要用这个.   安装还是挺方便的. yum -y install febootstrap   febootstrap -i ...

  10. centos7 安装lamp

    1升级gcc4.8以上  yum update gcc2升级openssl     yum update openssl 3安装apache a 安装apr   ./configure --prefi ...