ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Thrift小记

2019-09-09 13:01:23  阅读:254  来源: 互联网

标签:Profile profile Thrift import apache org thrift 小记


原文链接:https://my.oschina.net/mohaiyong/blog/221302

Thrit用的不多,也不够深入,这里小记一笔。

关于Thrift环境如何安装,可以参考官网。 

Thrit跟Java有数据类型的映射关系:

/**
 * The first thing to know about are types. The available types in Thrift are:
 *
 *  bool        Boolean, one byte
 *  byte        Signed byte
 *  i16         Signed 16-bit integer - short
 *  i32         Signed 32-bit integer - int
 *  i64         Signed 64-bit integer - long
 *  double      64-bit floating point value - double
 *  string      String
 *  binary      Blob (byte array)
 *  map<t1,t2>  Map from one type to another
 *  list<t1>    Ordered list of one type
 *  set<t1>     Set of unique elements of one type
 */

 我就不罗嗦了了,能看到这里的都能自动理解。蠢话

 

RPC的核心就是传参调用,参数抛不开基本数据类型、集合,更常用的是自定义对象。

在Thrift中,需要将自定义对象预先定义,类似于C语言编译要求。或者可以include其他thrift文件。

这里用Profile作为对象载体,这里Java的class对应Thrit中的struct,interface对应service。

做一个操作Profile的接口实现,代码如下:

namespace java org.zlex.support.thrift

struct Profile {
	1: string name,
	2: i32 score,
	3: bool enable
}
 
service ProfileService {
    string updateName(1:Profile profile, 2:string name)
    i32 updateScore(1:Profile profile, 2:i32 score)
    map<string,string> toMap(1:Profile profile)
}

保存为Profile.thrift

 

执行命令,生成Java代码:

thrift -r -gen java Profile.thrift

生成的代码详见附件。自动生成的代码中,冗余还是不少。修改代码的冲动闪过,保持原生态。

 

针对接口做个实现类:

/**
 * Mar 14, 2013
 */
package org.zlex.support.thrift.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.thrift.TException;
import org.zlex.support.thrift.Profile;
import org.zlex.support.thrift.ProfileService.Iface;

/**
 * 
 * @author snowolf
 * @version 1.0
 * @since 1.0
 */
public class ProfileServiceImpl implements Iface {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.zlex.support.thrift.ProfileService.Iface#updateName(org.zlex.support
	 * .thrift.Profile, java.lang.String)
	 */
	@Override
	public String updateName(Profile profile, String name) throws TException {
		profile.setName(name);
		return profile.getName();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.zlex.support.thrift.ProfileService.Iface#updateScore(org.zlex.support
	 * .thrift.Profile, int)
	 */
	@Override
	public int updateScore(Profile profile, int score) throws TException {
		profile.setScore(profile.getScore() + score);
		return profile.getScore();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.zlex.support.thrift.ProfileService.Iface#toMap(org.zlex.support.thrift
	 * .Profile)
	 */
	@Override
	public Map<String, String> toMap(Profile profile) throws TException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("name", profile.getName());
		map.put("score", "" + profile.getScore());
		map.put("isEnable", "" + profile.isEnable());
		return map;
	}

}

 

做一个Server实现,“非阻塞&高效二进制编码”:

/**
 * Mar 14, 2013
 */
package org.zlex.support.thrift;

import org.apache.log4j.Logger;

import org.apache.thrift.TProcessorFactory;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.server.THsHaServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TTransportException;
import org.zlex.support.thrift.impl.ProfileServiceImpl;

/**
 * 
 * @author snowolf
 * @version 1.0
 * @since 1.0
 */
public class Server {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(Server.class);

	/**
	 * 
	 */
	private int port;

	/**
	 * @param port
	 */
	public Server(int port) {
		this.port = port;
	}

	/**
	 * 
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void start() {
		try {
			TNonblockingServerSocket socket = new TNonblockingServerSocket(port);
			final ProfileService.Processor processor = new ProfileService.Processor(
					new ProfileServiceImpl());
			THsHaServer.Args arg = new THsHaServer.Args(socket);
			// 高效率的、密集的二进制编码格式进行数据传输
			// 使用非阻塞方式,按块的大小进行传输,类似于 Java 中的 NIO
			arg.protocolFactory(new TCompactProtocol.Factory());
			arg.transportFactory(new TFramedTransport.Factory());
			arg.processorFactory(new TProcessorFactory(processor));
			TServer server = new THsHaServer(arg);
			logger.info("服务启动-使用:非阻塞&高效二进制编码");
			server.serve();
		} catch (TTransportException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

服务器测试用例:

/**
 * Mar 14, 2013
 */
package org.zlex.support.thrift;

import org.junit.Before;
import org.junit.Test;

/**
 * 
 * @author snowolf
 * @version 1.0
 * @since 1.0
 */
public class ServerTest {
	public final static int PORT = 9999;

	private Server server;

	@Before
	public void init() {
		server = new Server(PORT);
	}

	@Test
	public void test() {
		server.start();
	}
}

 

客户端测试用例:

/**
 * Mar 14, 2013
 */
package org.zlex.support.thrift;

import static org.junit.Assert.*;

import java.util.Map;

import org.apache.thrift.TApplicationException;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import org.junit.Test;

/**
 * 
 * @author snowolf
 * @version 1.0
 * @since 1.0
 */
public class ClientTest {
	public final static int PORT = 9999;
	public static final String address = "localhost";
	public static final int clientTimeout = 30000;

	@Test
	public void test() {
		TTransport transport = new TFramedTransport(new TSocket(address, PORT,
				clientTimeout));
		TProtocol protocol = new TCompactProtocol(transport);
		ProfileService.Client client = new ProfileService.Client(protocol);

		try {
			transport.open();
			Profile profile = new Profile();
			profile.setName("Snowolf");
			Map<String, String> map = client.toMap(profile);
			assertEquals(map.get("name"),"Snowolf");

		} catch (TApplicationException e) {
			System.out.println(e.getMessage() + " " + e.getType());
		} catch (TTransportException e) {
			e.printStackTrace();
		} catch (TException e) {
			e.printStackTrace();
		}
		transport.close();
	}
}

 

Client调用Server接口,将Profile对象交由Server处理,转换为Map。

 

如果需要Thirif承载高并发的负载,可以通过nginx来完成负载均衡的实现,详见

Nginx扩展(一):nginx_tcp_proxy_module

 

小记完毕,Go Home!

 

 

 

转载于:https://my.oschina.net/mohaiyong/blog/221302

标签:Profile,profile,Thrift,import,apache,org,thrift,小记
来源: https://blog.csdn.net/choulv4873/article/details/100658088

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有