`
m635674608
  • 浏览: 5055123 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

ActiveMQ配置详解之如何配置自动重新连接

    博客分类:
  • MQ
 
阅读更多
这从这一篇开始,将讲解在activeMQ中的相关配置。由于activeMQ主要是参考apache官方网站上的说明,并在适当的地方加注说明。
一、如何配置自动重新连接
Apache官方说明:
If a JMS broker goes down, ActiveMQ can automatically reconnect to an available JMS broker using thefailover: protocol. Not only does this automatically reconnect, it will also resume any temporary destinations, sessions, producers and most importantly consumers.
All of this happens silently inside the JMS client so you don't need to worry about it in your application code.
e.g. connecting to the URL
failover:tcp:<span class=code-comment>//host1:port1,tcp://host2:port2
关于Failover的更为详细的说明如下:

The Failover(故障转移) Transport(注:这里自然可以翻译为“传输”,但是从根本上来说,我认为主要是在于故障转移是在传输层完成

The Failover transport layers reconnect logic on top of any of the other transports. (We used to call this transport the Reliable transport in ActiveMQ 3).
The Failover configuration syntax allows you to specify any number of composite uris. The Failover transport randomly chooses one of the composite URI and attempts to establish a connection to it. If it does not succeed or if it subsequently fails, a new connection is established to one of the other uris in the list.

Configuration Syntax

failover:(uri1,...,uriN)?transportOptions
or
failover:uri1,...,uriN
The failover transport uses random by default which lets you to load balance clients over a number of brokers.
If you would rather connect to a primary first and only connect to a secondary backup broker if the primary is unavailable, turn off randomizing using something like
failover:(tcp://primary:61616,tcp://secondary:61616)?randomize=false
这些下面的这些参数都可以通过类似http参数传值的方式failover:(tcp://host1:61616,tcp://host2:61616,...)?p1=v1&p2=v2&p3=v3
将参数p1,p2,p3传入。
Transport Options
Option Name Default Value Description
initialReconnectDelay 10 How long to wait before the first reconnect attempt (in ms)
maxReconnectDelay 30000 The maximum amount of time we ever wait between reconnect attempts (in ms)
useExponentialBackOff true Should an exponential backoff be used btween reconnect attempts (在尝试重新连接时是否使用指数回退算法
backOffMultiplier 2.0 The exponent used in the exponential backoff attempts
maxReconnectAttempts -1 >= AMQ v5.6 
0 < AMQ v5.6,
From version 5.6 onwards: -1 is default and means retry forever, 0 means don't retry (only try connection once but no retry). 
Prior to version 5.6: 0 is default and means retry forever. 
All versions: If set to >0, then this is the maximum number of reconnect attempts before an error is sent back to the client. 
这里要注意从v5.6版本以后出现的变化
startupMaxReconnectAttempts 0
If not 0, then this is the maximum number of reconnect attempts before an error is sent back to the client on the first attempt by the client to start a connection, once connected the maxReconnectAttempts option takes precedence.
设定一个连接创建成功之前尝试连接的最大次数) 
randomize true use a random algorithm to choose the the URI to use for reconnect from the list provided
backup false
initialize and hold a second transport connection - to enable fast failover
如果backup=true,并且the URIs to use for reconnect from the list provided的数量大于一个的情况下,broker将会维护着两个连接,其中一个作为备份,在主连接出现故障时实现快速切换 
timeout -1
Enables timeout on send operations (in miliseconds) without interruption of reconnection process
默认为-1,也就是timeout为forever
trackMessages false
keep a cache of in-flight messages that will flushed to a broker on reconnect
设置是否缓存[故障发生时]尚未传送完成的消息,当broker一旦重新连接成功,便将这些缓存中的消息刷新到新连接的代理中,使得消息可以在broker切换前后顺利传送
maxCacheSize 131072
size in bytes for the cache, if trackMessages is enabled
(设定消息缓存时的最大缓存大小,默认为128MB)
updateURIsSupported true
Determines whether the client should accept updates to its list of known URIs from the connected broker. Added in ActiveMQ 5.4
(设定客户端是否应该接受连接代理更新已知的URI列表。特性版本:ActiveMQ 5.4+)
Example URI
failover:(tcp://localhost:61616,tcp://remotehost:61616)?initialReconnectDelay=100
If the above gives errors try it this way (this way works in ActiveMQ 4.1.1 the one above does not)
failover://(tcp://localhost:61616,tcp://remotehost:61616)?initialReconnectDelay=100
Notes
If you use failover, and a broker dies at some point, your sends will block by default.
当使用failover时,如果broker失效,消息发送将默认被阻塞,下面给出两个避免阻塞的解决方法:在ActiveMQConnectionFactory 中配置TransportListener ,以及设定timeout属性值
Using TransportListener can help with this regard. It is best to set the Listener directly on theActiveMQConnectionFactory so that it is in place before any request that may require an network hop.
Additionally you can use timeout option which will cause your current send to fail after specified timeout. The following URL, for example
failover:(tcp://primary:61616)?timeout=3000
will cause send to fail after 3 seconds if the connection isn't established. The connection will not be killed, so you can try sending messages later at some point using the same connection (presumably some of your brokers will be available again). Timeouts on the failover transport are available since 5.3 version.
Transactions
The Failover transport tracks transactions by default. (故障转移的传输默认是跟踪事务的)The inflight transactions are replayed on reconnection.(故障发生时,尚未提交的事务将在故障转移重连之后重新执行) For simple scenarios this works ok. However there is an assumption for acknowledged (or consumer) transactions, that the previously received messages will get relayed after a reconnect. This is not always true when there are many connections and consumers, as redelivery order is not guaranteed. It is possible to have stale outstanding acknowledgements that can interfere with newly delivered messages, potentially leading to unacknowledged messages.
Starting in version 5.3.1, redelivery order is tracked and a transaction will fail to commit (throw a TransactionRolledBackException) if outstanding messages are not redelivered after failover. In addition, in doubt transaction will now result in a rollback such that they can be replayed by the application.(5.3.1版本开始重新传递顺序被跟踪以及事务将无法提交抛出一个TransactionRolledBackException的异常如果突出消息没有故障转移后重新传递的此外,有疑问的事务导致回滚以便它们可以被应用程序重放) In doubt transactions occur when failover happens with a commit message inflight. (有疑问事务出现在故障转移时有尚未提交的消息)It is not possible to know the exact point of failure. Did the transaction commit message get delivered or was it just the commit reply that is lost? In this case, it is necessary to rollback so that the application can get an indication of the failure and deal with any potential problem.
Broker side Options for Failover(broker端的选项设置
This is new in version 5.4:
There are some options that are available on a TransportConnector that is used by the broker that can be used to update clients automatically with information about new brokers to failover to. These are:
Option Name Default Value Description
updateClusterClients false
if true, pass information to connected clients about changes in the topology of the broker cluster(代理群集的拓扑) 
rebalanceClusterClients false if true, connected clients will be asked to rebalance(负载均衡) across a cluster of brokers when a new broker joins the network of brokers
updateClusterClientsOnRemove false
if true, will update clients when a cluster is removed from the network. Having this as separate option enables clients to be updated when new brokers join, but not when brokers leave.
动态更新连接到server的broker cluster的结构
updateClusterFilter null
comma separated list of regular expression filters used to match broker names of brokers to designate as being part of the failover cluster for the clients
这是一个用逗号分隔的正则过滤集合,它用来匹配broker的名字,符合条件的broker将被用于故障转移) 
updateURIsURL null A URL (or path to a local file) to a text file containing a comma separated list of URIs to use for reconnect in the case of failure
An example as defined within the broker's XML configuration file:
<broker> 
    ... 
    <transportConnectors> 
        <transportConnector name="openwire" uri="tcp://0.0.0.0:61616" updateClusterClients="true" updateClusterFilter="*A*,*B*" /> 
    </<transportConnectors> 
    ... 
</broker> 
If updateClusterClients is enabled, then your clients will only need to know about the first broker to connect to in a cluster of brokers - e.g.:
failover://tcp://primary:61616
If new brokers join, the client will automatically be updated with the additional URI of that broker to connect to in the event of a network or broker failure.
More Information
Also check out the following blog entry about using the cluster client updates and rebalancing features titled New Features in ActiveMQ 5.4: Automatic Cluster Update and Rebalance.
Priority Backup(指定备份代理集合中的优先级
If your setup have brokers in both local and remote networks, you probably want your clients connected to the local ones if those are available. As of version 5.6, ActiveMQ supports priority backup feature, so you can have your clients automatically reconnect to so called priority (or local) urls. Consider the following url
failover:(tcp://local:61616,tcp://remote:61616)?randomize=false&priorityBackup=true
If this url is used for the client, the client will try to connect and stay connected to the localbroker. If local broker fails, it will of course fail over to the remote one. But as priorityBackupparameter is used, it will constantly try to reconnect to the local broker. Once it can do so, the client will get back to it without any need for manual intervention.
By default, only the first url in the list is considered prioritized (local). In most cases this will suffice, but in some cases you can have multiple "local" urls. You can configure which urls are considered prioritized, by using priorityURIs parameter, like
failover:(tcp://local1:61616,tcp://local2:61616,tcp://remote:61616)?randomize=false&priorityBackup=true&priorityURIs=tcp://local1:61616,tcp://local2:61616
In this case the client will prioritize either local1 or local2 brokers and (re)connect to them if they are available.
 
分享到:
评论

相关推荐

    spring配置activemq详解

    - 结合Spring Boot的自动配置能力,可以极大地简化ActiveMQ的集成过程,只需少量配置就能快速启动和运行。 7. **监控和管理**: - ActiveMQ提供了Web控制台,可以通过浏览器进行监控和管理,查看队列状态、消息...

    ActiveMQ消息过期时间设置和自动清除解决方案

    本文档详细介绍了在Apache ActiveMQ 5.15.3版本中如何进行消息过期时间的设置,以及如何配置自动清除机制,特别是针对死信队列的处理方式。 #### 1. 消息过期设置 ##### 参数详解 - **Message 过期则客户端不能...

    ActiveMQ消息服务配置

    #### 一、ActiveMQ配置概览 ActiveMQ是一款非常流行的开源消息中间件,它基于Java开发,支持多种消息传递模式,如点对点(P2P)、发布/订阅(Pub/Sub)等。本文将详细介绍ActiveMQ的配置要点,包括Java内存调整、主从...

    SpringBoot集成ActiveMQ实例详解.docx

    在Spring Boot中集成ActiveMQ非常简便,因为Spring Boot提供了`spring-boot-starter-activemq`启动器,该启动器包含了自动配置的支持。首先,我们需要在项目中添加相应的依赖: ```xml &lt;groupId&gt;org.spring...

    activemq集群配置文档

    ### ActiveMQ 集群配置详解 #### 一、引言 随着业务需求的增长和技术的发展,消息中间件作为系统架构中的重要组成部分,在保障系统稳定性和提高应用性能方面扮演着关键角色。ActiveMQ 作为一种高性能的消息中间件...

    ActiveMQ配置

    **ActiveMQ配置详解** Apache ActiveMQ是开源的Java消息服务(JMS)提供商,它作为企业级消息中间件,能够帮助应用程序之间进行异步通信。本文将深入探讨ActiveMQ的配置,包括安装、基本配置、高级特性以及源码分析...

    ActiveMQ_使用failover模式进行连接切换时,线程断开

    ### ActiveMQ Failover模式下连接切换与线程断开问题详解 #### 一、问题背景及现象描述 在使用ActiveMQ消息中间件时,我们常常需要考虑如何在多个实例之间实现高可用性(HA)。其中一种常用的方式是采用**Failover...

    ActiveMQ集群:网络连接模式(network connector)详解.docx

    ActiveMQ 集群网络连接模式(Network Connector)详解 ActiveMQ 集群网络连接模式(Network Connector)是 ActiveMQ 提供的一种集群功能,旨在提高消息服务的横向扩展性和高可用性。通过将多个不同的 broker 实例...

    activemq 集群配置文档

    ### ActiveMQ集群配置详解 #### 一、ActiveMQ与JMS规范基础 在深入了解ActiveMQ集群配置之前,我们首先简要回顾一下Java消息服务(Java Message Service, JMS)的基础概念,这对于理解ActiveMQ的工作原理及其集群...

    ActiveMQ消息服务器 v5.17.6.zip

    2. **配置**:配置文件默认为conf/activemq.xml,可根据需求调整队列、主题、网络连接等设置。 3. **创建和消费消息**:使用JMS API或者ActiveMQ提供的客户端库,可以创建生产者发送消息,消费者接收消息。 4. **...

    配置ActiveMQ 静态集群

    **配置ActiveMQ静态集群详解** 在分布式系统中,消息队列作为重要的中间件,负责解耦应用程序并提高系统的可扩展性。Apache ActiveMQ是广泛使用的开源消息代理,它支持多种消息协议,如AMQP、STOMP、MQTT等。本文将...

    linux版本ActiveMQ 5.15.8

    **Linux版ActiveMQ 5.15.8详解** ActiveMQ是Apache软件基金会下的一个开源消息中间件项目,它提供了一种可靠的消息传递机制,使得应用程序可以进行异步通信。在Linux环境中部署和使用ActiveMQ,能够有效地提高系统...

    activemq-demo

    在"activeMQ详解.docx"文档中,可能会详细解释ActiveMQ的基本配置、如何在Spring中配置ActiveMQ以及如何创建和使用队列和主题。文档可能还会涵盖如何测试消息发送和接收,以及如何处理异常和错误情况。 而"activemq...

    ActiveMQ快速上手 PDF

    ### ActiveMQ 快速上手知识点详解 #### 一、ActiveMQ简介 - **定义**:ActiveMQ 是 Apache 软件基金会所研发的一款开源消息中间件,它完全支持 JMS 1.1 和 J2EE 1.4 规范,能够作为 JMS Provider 实现消息传递功能...

    Spring+ActiveMQ整合实例代码工程

    **Spring与ActiveMQ整合详解** 在Java开发中,Spring框架是极为重要的应用基础,而ActiveMQ作为Apache出品的一款开源消息中间件,常被用于实现应用间的异步通信和解耦。本实例代码工程"Spring+ActiveMQ整合实例代码...

    activemq-pool-5.4.1.jar.zip

    《ActiveMQ Pool 5.4.1:连接池与JAR包详解》 Apache ActiveMQ是业界广泛使用的开源消息中间件,它提供了高效、可靠的消息传递功能,支持多种协议,并且具有良好的可扩展性。在ActiveMQ的生态系统中,ActiveMQ Pool...

    Springboot+ActiveMq

    **Springboot+ActiveMQ 整合详解** Springboot与ActiveMQ的整合是企业级应用中常见的一种消息中间件集成方式,它能有效地实现系统间的异步通信和解耦。在这个项目中,我们将深入探讨如何利用Springboot搭建一个简单...

    activemq in action.pdf

    #### 四、ActiveMQ 的配置详解 - **连接配置**: - **理解Connector URIs**:URIs用于定义连接器的地址和参数,例如TCP连接、SSL加密等。 - **配置Transport Connectors**:用于定义ActiveMQ实例与其他实例之间的...

    Active MQ教程+配置

    ### ActiveMQ教程与配置详解 #### 一、ActiveMQ简介 **ActiveMQ**是目前最受欢迎且功能强大的开源消息中间件之一。它完全遵循了JMS 1.1和J2EE 1.4规范,这使得它能够在各种企业级应用程序中发挥重要作用。尽管JMS...

    activemq热备模式部署

    ### ActiveMQ热备模式部署详解 #### 一、概述 ActiveMQ是一款开源的消息中间件,提供了多种消息传递模式,能够支持大规模的企业级应用。热备模式是ActiveMQ中一种非常重要的高可用性部署方式,其核心是通过共享...

Global site tag (gtag.js) - Google Analytics