RabbitMQ镜像队列初始化连接时的“优化”
之前发过一篇帖子应用.Net+Consul维护RabbitMq的高可用性,然后最近老大问我当初我这么搞是抽的什么想法- -然后顺便贴了两行C#代码:
var factory = new ConnectionFactory()
{
UserName = "username",
Password = "password",
AutomaticRecoveryEnabled = true,
TopologyRecoveryEnabled = true
};
Connection = factory.CreateConnection(new string[] { "ip1", "ip2", "ip3" });
AutomaticRecoveryEnabled的作用就是断线重连,假如当前的连接断开了,连接不会释放
TopologyRecoveryEnabled 重连后恢复当前的工作进程,比如channel、queue、发布的消息进度等。
看上去其实却是比较方便的,为了狡辩我当场列出了我设计的方案的主要优点:
1.rabbitmq网关设计的时候一方面是为了让客户端能够保证建立与master节点的连接,直连master性能较高。
2.通过配置队列名+VirthHost可以获取队列信息,Consul可以起到配置中心的作用,可配置性高一些(其实此处是狡辩。。)
3.RabbitMQ与Master队列建立的连接在发生故障时会第一时间查询网关获取新的Mater队列信息进行重连(当然代码内部也有重试机制,不会随便就更换连接的),相比于AutomaticRecoveryEnabled效率更高一些。
4.客户端SDK内部实现定时更新队列连接,发现Master节点更换时重新建立连接
看上去我设计的方案还是有点意义的(其实是真懒得改代码)
不过此处有个问题就是既然创建连接时的参数可以提供多个IP的集合,假如RabbitMQ提供的客户端SDK内部实现的更好,那我狡辩什么不也完戏了吗。。。于是假装下载了下客户端sdk代码扫了扫,此处以C#代码为例,代码还是比较简单的。github地址
直接定位ConnectionFactory类找CreateConnection()方法
/// <summary>
/// Create a connection using a list of hostnames using the configured port.
/// By default each hostname is tried in a random order until a successful connection is
/// found or the list is exhausted using the DefaultEndpointResolver.
/// The selection behaviour can be overriden by configuring the EndpointResolverFactory.
/// </summary>
/// <param name="hostnames">
/// List of hostnames to use for the initial
/// connection and recovery.
/// </param>
/// <returns>Open connection</returns>
/// <exception cref="BrokerUnreachableException">
/// When no hostname was reachable.
/// </exception>
public IConnection CreateConnection(IList<string> hostnames)
{
return CreateConnection(hostnames, null);
} /// <summary>
/// Create a connection using a list of hostnames using the configured port.
/// By default each endpoint is tried in a random order until a successful connection is
/// found or the list is exhausted.
/// The selection behaviour can be overriden by configuring the EndpointResolverFactory.
/// </summary>
/// <param name="hostnames">
/// List of hostnames to use for the initial
/// connection and recovery.
/// </param>
/// <param name="clientProvidedName">
/// Application-specific connection name, will be displayed in the management UI
/// if RabbitMQ server supports it. This value doesn't have to be unique and cannot
/// be used as a connection identifier, e.g. in HTTP API requests.
/// This value is supposed to be human-readable.
/// </param>
/// <returns>Open connection</returns>
/// <exception cref="BrokerUnreachableException">
/// When no hostname was reachable.
/// </exception>
public IConnection CreateConnection(IList<string> hostnames, String clientProvidedName)
{
var endpoints = hostnames.Select(h => new AmqpTcpEndpoint(h, this.Port, this.Ssl));
return CreateConnection(new DefaultEndpointResolver(endpoints), clientProvidedName);
} /// <summary>
/// Create a connection using a list of endpoints. By default each endpoint will be tried
/// in a random order until a successful connection is found or the list is exhausted.
/// The selection behaviour can be overriden by configuring the EndpointResolverFactory.
/// </summary>
/// <param name="endpoints">
/// List of endpoints to use for the initial
/// connection and recovery.
/// </param>
/// <returns>Open connection</returns>
/// <exception cref="BrokerUnreachableException">
/// When no hostname was reachable.
/// </exception>
public IConnection CreateConnection(IList<AmqpTcpEndpoint> endpoints)
{
return CreateConnection(new DefaultEndpointResolver(endpoints), null);
} /// <summary>
/// Create a connection using an IEndpointResolver.
/// </summary>
/// <param name="endpointResolver">
/// The endpointResolver that returns the endpoints to use for the connection attempt.
/// </param>
/// <param name="clientProvidedName">
/// Application-specific connection name, will be displayed in the management UI
/// if RabbitMQ server supports it. This value doesn't have to be unique and cannot
/// be used as a connection identifier, e.g. in HTTP API requests.
/// This value is supposed to be human-readable.
/// </param>
/// <returns>Open connection</returns>
/// <exception cref="BrokerUnreachableException">
/// When no hostname was reachable.
/// </exception>
public IConnection CreateConnection(IEndpointResolver endpointResolver, String clientProvidedName)
{
IConnection conn;
try
{
if (AutomaticRecoveryEnabled)
{
var autorecoveringConnection = new AutorecoveringConnection(this, clientProvidedName);
autorecoveringConnection.Init(endpointResolver);
conn = autorecoveringConnection;
}
else
{
IProtocol protocol = Protocols.DefaultProtocol;
conn = protocol.CreateConnection(this, false, endpointResolver.SelectOne(this.CreateFrameHandler), clientProvidedName);
}
}
catch (Exception e)
{
throw new BrokerUnreachableException(e);
} return conn;
}
代码比较直观,第二个方法的时候把传入的字符串集合转换成了AmqpTcpEndpoint集合,AmqpTcpEndpoint里包含队列绑定信息,包含默认ip,端口,SSL配置信息,RabbitMQ的Amqp协议信息,初始化Socket连接的协议类型(一个AddressFamily枚举),当然都是可配置的(除了ip貌似都喜欢使用默认的)。
然后就倒主要部分了。
if (AutomaticRecoveryEnabled)
{
var autorecoveringConnection = new AutorecoveringConnection(this, clientProvidedName);
autorecoveringConnection.Init(endpointResolver);
conn = autorecoveringConnection;
}
else
{
IProtocol protocol = Protocols.DefaultProtocol;
conn = protocol.CreateConnection(this, false, endpointResolver.SelectOne(this.CreateFrameHandler), clientProvidedName);
}
由于主要想看看传入多个连接是不是可以智能的选择master连接,所以此处直接看else里的代码就行了。。。(其实一样。。)
protocol.CreateConnection方法调用时传入的第三个参数endpointResolver.SelectOne(this.CreateFrameHandler)作用其实就是选择连接然后扔到CreateFrameHandler委托里作为参数进行Socket初始化的。看一下SelectOne这个扩展方法即可。。看了两行终于可以继续狡辩了。。
public static T SelectOne<T>(this IEndpointResolver resolver, Func<AmqpTcpEndpoint, T> selector)
{
var t = default(T);
Exception exception = null;
foreach(var ep in resolver.All())
{
try
{
t = selector(ep);
if(t.Equals(default(T)) == false)
{
return t;
}
}
catch (Exception e)
{
exception = e;
}
} if(Object.Equals(t, default(T)) && exception != null)
{
throw exception;
} return t;
}
这个foreach还是“比较好的”,能够建立一个Socket连接就愉快的返回了!,不过这个resolver.All()里是不是还有玄机呢!看了一眼。。终于放心了。。
public class DefaultEndpointResolver : IEndpointResolver
{
private List<AmqpTcpEndpoint> endpoints;
private Random rnd = new Random(); public DefaultEndpointResolver (IEnumerable<AmqpTcpEndpoint> tcpEndpoints)
{
this.endpoints = tcpEndpoints.ToList();
} public IEnumerable<AmqpTcpEndpoint> All()
{
return endpoints.OrderBy(item => rnd.Next());
}
就是随机排序一下。。不过这么看自己实现下IEndpointResolver接口改个选择master队列的策略也是不错的。
RabbitMQ镜像队列初始化连接时的“优化”的更多相关文章
- rabbitmq——镜像队列
转自:http://my.oschina.net/hncscwc/blog/186350?p=1 1. 镜像队列的设置 镜像队列的配置通过添加policy完成,policy添加的命令为: rabbit ...
- RabbitMQ镜像队列集群搭建、与SpringBoot整合
镜像模式 集群模式非常经典的就是Mirror镜像模式,保证100%数据不丢失,在实际工作中也是用的最多的,并且实现集群比较的简单. Mirror镜像队列,目的是为了保证 RabbitMQ 数据的高可靠 ...
- springboot~rabbitmq的队列初始化和绑定
配置文件,在rabbit中自动建立exchange,queue和绑定它们的关系 代码里初始化exchange 代码里初始化queue 代码里绑定exchange,queue和routekey 配置文件 ...
- RabbitMQ如何保证发送端消息的可靠投递-发生镜像队列发生故障转移时
上一篇最后提到了mandatory这个参数,对于设置mandatory参数个人感觉还是很重要的,尤其在RabbitMQ镜像队列发生故障转移时. 模拟个测试环境如下: 首先在集群队列中增加两个镜像队列的 ...
- Oracle 表三种连接方式(sql优化)
在查看sql执行计划时,我们会发现表的连接方式有多种,本文对表的连接方式进行介绍以便更好看懂执行计划和理解sql执行原理. 一.连接方式: 嵌套循环(Nested Loops (NL)) (散列)哈希 ...
- 18-基于CentOS7搭建RabbitMQ3.10.7集群镜像队列+HaProxy+Keepalived高可用架构
集群架构 虚拟机规划 IP hostname 节点说明 端口 控制台地址 192.168.247.150 rabbitmq.master rabbitmq master 5672 http://192 ...
- RabbitMQ镜像模式双节点部署时故障转移过程中队列中消息的状态
场景 现有节点Node1和Node2,建立Exchange:yu.exchange,创建队列yu1.queue镜像队列master位于Node1,yu2.queue镜像队列位于Node2,使用topi ...
- RabbitMQ 高可用之镜像队列
如果RabbitMQ集群只有一个broker节点,那么该节点的失效将导致整个服务临时性的不可用,并且可能会导致message的丢失(尤其是在非持久化message存储于非持久化queue中的时候).可 ...
- RabbitMQ高可用镜像队列
## RabbitMQ高可用镜像队列 在分布式系统中,通常使用多个术语来标识主要副本和辅助副本.本指南通常使用"主"来引用队列的主要副本,而对于辅助副本则使用"镜像&qu ...
随机推荐
- Tomcat配置连接c3p0连接池
一.Tomcat配置JNDI资源 JNDI(Java Naming and Directory Interface),Java 命名和目录接口. JNDI的作用就是:在服务器上配置资源,然后通过统一的 ...
- SQLHappy微软数据库连接查询操作,对数据的处理和查询
(软件已更新,部分介绍与新版软件有出处) 1.服务连接界面介绍 2.主界面介绍 3.表搜索介绍 4.命令菜单部分介绍 5.插件介绍 6.帮助菜单介绍 7.数据库列表右键菜单 8.数据库结构和数据操作( ...
- RESTORE DATABASE命令还原SQLServer 2005 数据库
--返回由备份集内包含的数据库和日志文件列表组成的结果集. --主要获得逻辑文件名 USE master RESTORE FILELISTONLY FROM DISK = 'g:\back.Bak' ...
- RN canvas画布大小之谜
一.需求 在一个高640.宽360的canvas内画一些坐标点. 二.问题 坐标点只显示了一部分,剩下的点没显示(其坐标属于(640,360)区域). 三.原因 canvas默认的画布大小是高150, ...
- Activiti 数据库表自动生成策略
Activiti 引擎启动时默认会检测数据库版本与程序版本是否相符,不相符就会抛出异常停止引擎的初始化. 这一策略可以通过引擎的初始化配置参数databaseSchemaUpdate来控制, 如下图的 ...
- Android学习笔记(5)----启动 Theme.Dialog 主题的Activity时程序崩溃的解决办法
新建了一个Android Studio工程,在MainActivity的主界面中添加了两个按钮,点击其中一个按钮用来启动 NormalActivity,点击另一按钮用来启动DialogActivity ...
- 何时需要做urlEncode,以及为什么要做
在RFC1738中,对于URL可以使用的字符集做了如下规定: “ 只有0-9a-zA-Z的字母以及$-_.+!*'(),"这几个特殊字符 ” 而在html4中扩展了所有的unicode ch ...
- Java学习---Excel读写操作
1.1.1. 简介 Apache POI 使用Apache POI 完成Excel读写操作 Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API ...
- Linux iptables命令详解
iptables命令主要是设置防火墙信息的 常见命令参数 Usage: iptables -[AD] chain rule-specification [options] iptables -I ch ...
- redis主从,哨兵,集群
本次所有操作在docker下进行,搭建方便,迅速构建redis集群. 1. docker安装redis 获取redis:latest(使用官方最新的) 镜像 $ docker pull redis r ...