1、thrift初识
来源:互联网 发布:c语言dll反编译 编辑:程序博客网 时间:2024/05/16 01:01
thrift是一个跨语言服务的软件框架,它能进行不同语言间进行通信。
首先通过一个简单的案例说明每个类的作用。
我们知道对于RPC框架无非有一下三个部分:
第一个部分:通信协议
在不同机器进行通信时,通信各方必须遵守同样的规则,才能进行通信,对于thrift,为了与编程语言无关,则制定了利用.thrift文件作为通信规则。
第二个部分:服务端
第三个部分:客户端
案例如下:
thrift文件:UserService.thrift(用于提供获取所有用户的服务)
namespace java cn.stq.thriftinclude "../Ex.thrift" service UserService {list<map<string,string>> getUser() throws (1:Ex.thriftDataException dataEx,2:Ex.thriftBusinessException businessEx,3:Ex.thriftSystemException systemEx)}
服务的实现:UserServiceImpl.java
package cn.stq.thrift;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.apache.thrift.TException;import cn.ruida.sms.portal.domain.User;import cn.stq.thrift.exception.thriftBusinessException;import cn.stq.thrift.exception.thriftDataException;import cn.stq.thrift.exception.thriftSystemException;public class UserServiceImpl implements UserService.Iface{public List<Map<String, String>> getUser() throws thriftDataException,thriftBusinessException, thriftSystemException, TException {List<Map<String, String>> list = new ArrayList<Map<String,String>>();Map<String,String>map = new HashMap<String, String>();map.put("loginName", "zhangsan");map.put("birthday", "2014-01-01");map.put("realName", "张三");list.add(map);return list;}private List<Map<String, String>> getUser(List<User> list,List<Map<String, String>> users) {for(User user:list){if(user==null){continue;}Map<String,String>map = new HashMap<String, String>();map.put("loginName", user.getLoginName());map.put("birthday", user.getBirthday());map.put("realName", user.getRealName());users.add(map);}return users;}}客户端实现:
package cn.stq.thrift;import java.util.List;import java.util.Map;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 cn.stq.thrift.exception.thriftBusinessException;import cn.stq.thrift.exception.thriftDataException;import cn.stq.thrift.exception.thriftSystemException;public class Client {public static void main(String[] args) throws thriftDataException, thriftBusinessException, thriftSystemException, TException {TTransport ts = new TSocket("192.168.1.109", 9999);TProtocol p = new TBinaryProtocol(ts);UserService.Client client = new UserService.Client(p);ts.open();List<Map<String, String>> user = client.getUser();for(Map<String,String>map:user){if(map==null){continue;}System.out.println("真实姓名:"+map.get("realName")+",登录名:"+map.get("loginName")+",出生日期:"+map.get("birthday"));}}}服务端:
package cn.stq.thrift;import org.apache.thrift.TProcessor;import org.apache.thrift.protocol.TBinaryProtocol;import org.apache.thrift.server.TServer;import org.apache.thrift.server.TSimpleServer;import org.apache.thrift.transport.TServerSocket;public class Server {public static void startServer()throws Exception{//步骤一:创建TProcessorTProcessor processor = new UserService.Processor<UserService.Iface>(new UserServiceImpl());//步骤二:创建SocketTServerSocket serverSocket = new TServerSocket(9999);TServer.Args tAgrs = new TServer.Args(serverSocket);tAgrs.processor(processor);tAgrs.protocolFactory(new TBinaryProtocol.Factory());TServer server = new TSimpleServer(tAgrs);//启动服务System.out.println("服务启动.....");server.serve();}public static void main(String[] args) throws Exception{startServer();}}这个案例简单的实现了thrift。详细介绍如下:
首先介绍一下创建一个thrift RPC的步骤:
第一步:我们首先要编写thrift文件,定义服务的接口
第二步:服务端的开发步骤:
1)实现服务的接口
2)创建TProcessor
3)创建TServerTransport
4)创建TProtocol
5)创建TServer
6)启动Server
第三步:客户端的开发步骤:
1)创建Transport
2)创建TProtocol
3)获取Client
4)调用Client相应的方法
看到上面的例子,是不是对thrift框架产生了兴趣,thrift框架让我们无需关心底层的通信,客户端调用服务端的服务对我们来说是透明的。接下来就进入thrift的世界。
0 0
- 1、thrift初识
- 初识Thrift
- thrift初识
- Thrift学习(1)-Thrift简介
- Thrift 框架分析1
- Thrift 框架分析1
- thrift的编译器(1)
- thrift(1):TNonblockingServer分析
- Thrift
- thrift
- thrift
- thrift
- Thrift
- Thrift
- thrift
- thrift
- thrift
- Thrift
- 第十四周 【项目2-形状类族的中的纯虚函数】
- Deep learning From Image to Sequence
- Qt下QString转char*
- 合并apk和odex
- 关于QT程序中出现"collect2: ld returned 1 exit status"的调试
- 1、thrift初识
- php laravel 框架 之文件上传
- [SQL]递归查询
- c++面试
- 利用Gearman实现并发查询(Multi-Query)
- 2014-第14周。阅读
- 国密算法(SM2,SM3,SM4)辅助工具升级版(OTP+PBOC3.0)
- 第14周 项目1.2 动物这样叫
- 微软面试 续