由于以下原因无法启动网桥:java.net.ConnectException:连接被拒绝:连接

Could not start network bridge due to: java.net.ConnectException: Connection refused: connect

我正在尝试在非主站点中的代理和主站点中的代理之间定义一个网桥,其预期行为是将非主站点中生成的消息转发到主站点,并且在那里加工。

在网上做了一些研究后,我在 xml 文件中为指向主代理的非主代理添加了一个 <networkConnectors/> 元素。

这是主代理的 XML 文件:

<beans
  xmlns="http://www.springframework.org/schema/beans"
  xmlns:amq="http://activemq.apache.org/schema/core"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">

    <broker xmlns="http://activemq.apache.org/schema/core" brokerName="Broker" dataDirectory="SOME_PATH" destroyApplicationContextOnStop="true" advisorySupport="false" schedulerSupport="true">

        <destinationPolicy>
            <policyMap>
              <policyEntries>
                <policyEntry topic=">" producerFlowControl="true" memoryLimit="20mb">
                  <pendingSubscriberPolicy>
                    <vmCursor />
                  </pendingSubscriberPolicy>
                </policyEntry>
                <policyEntry queue=">" producerFlowControl="true" memoryLimit="20mb">
                </policyEntry>
              </policyEntries>
            </policyMap>
        </destinationPolicy> 

        <managementContext>
            <managementContext createConnector="false"/>
        </managementContext>

        <persistenceAdapter>
            <kahaDB directory="SOME_PATH"/>
        </persistenceAdapter>

        <systemUsage>
            <systemUsage>
                <memoryUsage>
                    <memoryUsage limit="50 mb"/>
                </memoryUsage>
                <storeUsage>
                    <storeUsage limit="1 gb"/>
                </storeUsage>
                <tempUsage>
                    <tempUsage limit="100 mb"/>
                </tempUsage>
            </systemUsage>
        </systemUsage>

        <transportConnectors>
            <transportConnector name="primary_broker" uri="tcp://localhost:2384"/>
        </transportConnectors>

    </broker>    
</beans>

这是非主要经纪人的 XML 文件:

<beans
  xmlns="http://www.springframework.org/schema/beans"
  xmlns:amq="http://activemq.apache.org/schema/core"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">

    <broker xmlns="http://activemq.apache.org/schema/core" brokerName="Broker" dataDirectory="SOME_PATH" destroyApplicationContextOnStop="true" advisorySupport="false" schedulerSupport="true">

        <destinationPolicy>
            <policyMap>
              <policyEntries>
                <policyEntry topic=">" producerFlowControl="true" memoryLimit="20mb">
                  <pendingSubscriberPolicy>
                    <vmCursor />
                  </pendingSubscriberPolicy>
                </policyEntry>
                <policyEntry queue=">" producerFlowControl="true" memoryLimit="20mb">
                </policyEntry>
              </policyEntries>
            </policyMap>
        </destinationPolicy> 

        <managementContext>
            <managementContext createConnector="false"/>
        </managementContext>

        <networkConnectors>
            <networkConnector name="bridge" uri="static:(tcp://PRIMARY_SERVER_NAME:2384)"/>
        </networkConnectors>

        <persistenceAdapter>
            <kahaDB directory="SOME_PATH"/>
        </persistenceAdapter>

        <systemUsage>
            <systemUsage>
                <memoryUsage>
                    <memoryUsage limit="50 mb"/>
                </memoryUsage>
                <storeUsage>
                    <storeUsage limit="1 gb"/>
                </storeUsage>
                <tempUsage>
                    <tempUsage limit="100 mb"/>
                </tempUsage>
            </systemUsage>
        </systemUsage>

        <transportConnectors>
            <transportConnector name="secondary_broker" uri="tcp://localhost:2386"/>
        </transportConnectors>

    </broker>    
</beans>

我在尝试启动非主代理时看到的错误是:

Could not start network bridge between: vm://Broker?async=false&network=true and: tcp://PRIMARY_SERVER_NAME:2384 due to: java.net.ConnectException: Connection refused: connect

我做了一些调查,让 IT 团队参与进来,确认这不是防火墙问题。

我试图在同一个非主主机上的 2 个代理之间建立网络。我使用相同的配置(即仅通过更新 networkConnector 中的 uri 值),这次网络建立成功。

我不确定发生了什么以及为什么使用相同的配置,假设没有网络问题,我可以在同一主机上桥接 2 个代理,但不能在不同的主机上桥接。

我正在使用 ActiveMQ v5.5。

问题是主代理中的 <transportConnector/> 元素配置为 localhost
将其更改为使用实际的服务器名称后,它起作用了

<transportConnectors>
    <transportConnector name="primary_broker" uri="tcp://SERVER_NAME:2384"/>
</transportConnectors>

如果您使用的是 activemq 版本 5.13.3(或更高版本),您只需要创建一个共享文件系统挂载点,并在 activemq.xml 配置文件中指定它的位置:

<persistenceAdapter>
   <levelDB directory="/shared-File-System-Mount-Point"/>
</persistenceAdapter>

我一直在使用 AWS efs 文件系统,它运行良好。 这些不需要指定网络连接器。 加载时,其中一个activemq服务器会在这个位置锁定一个文件,并成为主人。 如果它因任何原因失败,可用的从站之一将自动成为主站。

可以找到更多信息here

另外,this is an awesome tool to test the fail over.