Thrift 实例 Helloworld

参考资料:
thrift.apache.org
http://mingxiao2010.blog.163.com/blog/static/8619048120114273843772/
做一个HelloWorld的例子,基本上跑通了。

过程如下:
环境准备:
开始我选择的是最新的版本:Apache Thrift v0.9.0 ,
1.在linux下有一些lib库没有安装成功,问题: configure: error: "Error: libcrypto required."
2.在windows下了一个windows版本, 运行 thrift-0.9.0.exe -r --gen java Hello.thrift ,发现不是win32版本。
3.重新下载了0.7.0 windows版本,此版本OK,可以在win 32使用
4.下载相关jar包:org.apache.servicemix.bundles.libthrift-0.7.0_1.jar , libfb303-0.7.0.jar
5.接下来就在windows环境下开发

例子:
1. thrift接口定义:Hello.thrift
[java]  view plain copy
  1. namespace java service.demo  
  2. ervice Hello{  
  3.   
  4.    string helloString(1:string para)  
  5.    i32 helloInt(1:i32 para)  
  6.    bool helloBoolean(1:bool para)  
  7.    void helloVoid()  
  8.    string helloNull()   
  9.   
  10.    


2. 生成java接口
     thrift-0.7.0.exe -r --gen java Hello.thrift
     会生如下代码:
[java]  view plain copy
  1. /** 
  2.  * Autogenerated by Thrift Compiler (0.7.0) 
  3.  * 
  4.  * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING 
  5.  */  
  6. package service.demo;  
  7.   
  8. import java.util.List;  
  9. import java.util.ArrayList;  
  10. import java.util.Map;  
  11. import java.util.HashMap;  
  12. import java.util.EnumMap;  
  13. import java.util.Set;  
  14. import java.util.HashSet;  
  15. import java.util.EnumSet;  
  16. import java.util.Collections;  
  17. import java.util.BitSet;  
  18. import java.nio.ByteBuffer;  
  19. import java.util.Arrays;  
  20. import org.slf4j.Logger;  
  21. import org.slf4j.LoggerFactory;  
  22.   
  23. public class Hello {  
  24.   
  25.   public interface Iface {  
  26.   
  27.     public String helloString(String para) throws org.apache.thrift.TException;  
  28.   
  29.     public int helloInt(int para) throws org.apache.thrift.TException;  
  30.   
  31.     public boolean helloBoolean(boolean para) throws org.apache.thrift.TException;  
  32.   
  33.     public void helloVoid() throws org.apache.thrift.TException;  
  34.   
  35.     public String helloNull() throws org.apache.thrift.TException;  
  36.   
  37.   }  
  38.   .....此处省略,详细请看自己生成文件  
  39.    
  40.   
  41. }  


 
3. 将生成的service.demo.Hello.java的导入eclipse中
4. 编写Server端实现类HelloServiceImpl: 即 Hello.Iface 的实现
[java]  view plain copy
  1. package service.demo;  
  2.   
  3. import org.apache.thrift.TException;  
  4.   
  5. public class HelloServiceImpl implements Hello.Iface {  
  6.     @Override  
  7.     public boolean helloBoolean(boolean para) throws TException {  
  8.         return para;  
  9.     }  
  10.   
  11.     @Override  
  12.     public int helloInt(int para) throws TException {  
  13.         try {  
  14.             Thread.sleep(1000);  
  15.         } catch (InterruptedException e) {  
  16.             e.printStackTrace();  
  17.         }  
  18.         return para+1;  
  19.     }  
  20.   
  21.     @Override  
  22.     public String helloNull() throws TException {  
  23.         return null;  
  24.     }  
  25.   
  26.     @Override  
  27.     public String helloString(String para) throws TException {  
  28.         return para;  
  29.     }  
  30.   
  31.     @Override  
  32.     public void helloVoid() throws TException {  
  33.         System.out.println("Hello World");  
  34.     }  
  35. }  


5. Server启动类: HelloServiceServer
[java]  view plain copy
  1. package service.server;  
  2.   
  3. import service.demo.Hello.Processor;  
  4.   
  5. import org.apache.thrift.protocol.TBinaryProtocol;  
  6. import org.apache.thrift.protocol.TBinaryProtocol.Factory;  
  7. import org.apache.thrift.server.TServer;  
  8. import org.apache.thrift.server.TThreadPoolServer;  
  9. import org.apache.thrift.server.TThreadPoolServer.Args;  
  10. import org.apache.thrift.transport.TServerSocket;  
  11. import org.apache.thrift.transport.TTransportException;  
  12.   
  13. import service.demo.Hello;  
  14. import service.demo.HelloServiceImpl;  
  15.   
  16. public class HelloServiceServer {  
  17.     public void startServer() {  
  18.         try {  
  19.   
  20.             TServerSocket serverTransport = new TServerSocket(1234);  
  21.   
  22.             Hello.Processor process = new Processor(new HelloServiceImpl());  
  23.   
  24.             Factory portFactory = new TBinaryProtocol.Factory(truetrue);  
  25.             Args args = new Args(serverTransport);  
  26.             args.processor(process);  
  27.             args.protocolFactory(portFactory);  
  28.   
  29.             TServer server = new TThreadPoolServer(args);  
  30.             server.serve();  
  31.         } catch (TTransportException e) {  
  32.             e.printStackTrace();  
  33.         }  
  34.     }  
  35.   
  36.     public static void main(String[] args) {  
  37.         HelloServiceServer server = new HelloServiceServer();  
  38.         server.startServer();  
  39.     }  
  40. }  


6. 客户端类:HelloClient
[java]  view plain copy
  1. package service.client;  
  2.   
  3. import org.apache.thrift.TException;  
  4. import org.apache.thrift.protocol.TBinaryProtocol;  
  5. import org.apache.thrift.protocol.TProtocol;  
  6. import org.apache.thrift.transport.TSocket;  
  7. import org.apache.thrift.transport.TTransport;  
  8. import org.apache.thrift.transport.TTransportException;  
  9.   
  10. import service.demo.Hello;  
  11.   
  12. public class HelloClient {  
  13.   
  14.     public void startClient() {  
  15.         TTransport transport;  
  16.         try {  
  17.             transport = new TSocket("localhost"1234);  
  18.             TProtocol protocol = new TBinaryProtocol(transport);  
  19.             Hello.Client client = new Hello.Client(protocol);  
  20.             transport.open();  
  21.             System.out.println(client.helloInt(12));  
  22.             transport.close();  
  23.         } catch (TTransportException e) {  
  24.             e.printStackTrace();  
  25.         } catch (TException e) {  
  26.             e.printStackTrace();  
  27.         }  
  28.     }  
  29.   
  30.     public static void main(String[] args) {  
  31.         HelloClient client = new HelloClient();  
  32.         client.startClient();  
  33.     }  
  34. }  


7.运行:
a. 启动HelloServiceServer
b. 启动HelloClient
运行结果:client端会显示13  即:helloInt是进行+1操作。


附:c#客户端代码(参考http://blog.csdn.net/kc87654321/article/details/7226675)
1.   thrift -gen csharp Hello.thrift
2.   导入thrift的C#库工程:/thrift/lib/
3.   将生成的Hello.cs加入到项目
4.   客户端代码如下:
[csharp]  view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using Thrift.Transport;  
  6. using Thrift.Protocol;  
  7.   
  8. namespace ConsoleApplication1  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.     
  15.             TTransport transport = new TSocket("localhost", 1234);   
  16.             TProtocol protocol = new TBinaryProtocol(transport);   
  17.             Hello.Client client = new Hello.Client(protocol);   
  18.             transport.Open();   
  19.             Console.WriteLine("Client calls .....");  
  20.             while (true)  
  21.             {  
  22.                 String str=Console.ReadLine();  
  23.                       
  24.                 Console.WriteLine(client.helloInt(Convert.ToInt32(str)));  
  25.   
  26.             }  
  27.             transport.Close();  
  28.   
  29.             Console.ReadLine();  
  30.         }  
  31.     }  
  32. }  
  5. 设置C#启动项目
  6. 运行
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值