1、thrift初识

  thrift是一个跨语言服务的软件框架,它能进行不同语言间进行通信。

首先通过一个简单的案例说明每个类的作用。

  我们知道对于RPC框架无非有一下三个部分:

第一个部分:通信协议

    在不同机器进行通信时,通信各方必须遵守同样的规则,才能进行通信,对于thrift,为了与编程语言无关,则制定了利用.thrift文件作为通信规则。

第二个部分:服务端

第三个部分:客户端

案例如下:

thrift文件:UserService.thrift(用于提供获取所有用户的服务)

namespace java cn.stq.thrift
include "../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{
		//步骤一:创建TProcessor
		TProcessor processor = new UserService.Processor<UserService.Iface>(new UserServiceImpl());
		//步骤二:创建Socket
		TServerSocket 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值