Shared File System Master Slave 全配置以及测试

本文介绍了一个ActiveMQ集群的搭建过程,包括两个Broker的配置及共享文件测试。通过配置实现消息的高可用性,并验证了主备切换时消息不丢失的特点。

在本机完成2个broker的共享文件测试
2个broker的完整配置文件如下

  
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements. See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at
 
  http://www.apache.org/licenses/LICENSE-2.0
 
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- 
  Create a dynamic network of brokers
  For more information, see:
 
  http://activemq.apache.org/networks-of-brokers.html
 
  To run this example network of ActiveMQ brokers run
 
  $ bin/activemq console xbean:conf/activemq-dynamic-network-broker1.xml
 
  and
 
  $ bin/activemq console xbean:conf/activemq-dynamic-network-broker2.xml
 
  in separate consoles
-->
<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 
 http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring.xsd">


  <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/>


  <broker xmlns="http://activemq.apache.org/schema/core" brokerName="dynamic-broker2" dataDirectory="${activemq.base}/data" >


    <!-- Destination specific policies using destination names or wildcards -->
    <destinationPolicy>
      <policyMap>
        <policyEntries>
          <policyEntry queue=">" producerFlowControl="true" memoryLimit="20mb">
            <deadLetterStrategy>
             <individualDeadLetterStrategy queuePrefix="DLQ." useQueueForQueueMessages="true" />
            </deadLetterStrategy>
          </policyEntry>
          <policyEntry topic=">" producerFlowControl="true" memoryLimit="20mb">
          </policyEntry>
        </policyEntries>
      </policyMap>
    </destinationPolicy>


    <!-- Use the following to configure how ActiveMQ is exposed in JMX -->
    <managementContext>
      <managementContext createConnector="true" connectorPort="1100"/>
    </managementContext>


    <!--
      Configure network connector to use multicast protocol
      For more information, see
     
      http://activemq.apache.org/multicast-transport-reference.html
    -->
    <networkConnectors>
     <networkConnector uri="multicast://default"
      dynamicOnly="true"
      networkTTL="3"
      prefetchSize="1"
      decreaseNetworkConsumerPriority="true" />
    </networkConnectors>
   
	
	  <persistenceAdapter>
	    <!--<kahaDB directory="${activemq.base}/data/dynamic-broker2/kahadb" />-->
	    <kahaDB directory="D:/"/>
	  </persistenceAdapter>
	  
	  <!-- The maximum amount of space the broker will use before slowing down producers -->
	  <systemUsage>
	    <systemUsage>
	      <memoryUsage>
	        <memoryUsage limit="20 mb"/>
	      </memoryUsage>
	      <storeUsage>
	        <storeUsage limit="1 gb" name="foo"/>
	      </storeUsage>
	      <tempUsage>
	        <tempUsage limit="100 mb"/>
	      </tempUsage>
	    </systemUsage>
	  </systemUsage>


    <!--
      The transport connectors ActiveMQ will listen to
      Configure discovery URI to use multicast protocol
    -->
    <transportConnectors>
      <transportConnector name="openwire" uri="tcp://0.0.0.0:61618" discoveryUri="multicast://default" />
    </transportConnectors>


  </broker>


</beans>

另外一个Borker


<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements. See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at
 
  http://www.apache.org/licenses/LICENSE-2.0
 
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- 
  Create a dynamic network of brokers
  For more information, see:
 
  http://activemq.apache.org/networks-of-brokers.html
 
  To run this example network of ActiveMQ brokers run
 
  $ bin/activemq console xbean:conf/activemq-dynamic-network-broker1.xml
 
  and
 
  $ bin/activemq console xbean:conf/activemq-dynamic-network-broker2.xml
 
  in separate consoles
-->
<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">


  <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/>


  <broker xmlns="http://activemq.apache.org/schema/core" brokerName="dynamic-broker1" dataDirectory="${activemq.base}/data" >


    <!-- Destination specific policies using destination names or wildcards -->
    <destinationPolicy>
      <policyMap>
        <policyEntries>
          <policyEntry queue=">" producerFlowControl="true" memoryLimit="20mb">
            <deadLetterStrategy>
             <individualDeadLetterStrategy queuePrefix="DLQ." useQueueForQueueMessages="true" />
            </deadLetterStrategy>
          </policyEntry>
          <policyEntry topic=">" producerFlowControl="true" memoryLimit="20mb">
          </policyEntry>
        </policyEntries>
      </policyMap>
    </destinationPolicy>


    <!-- Use the following to configure how ActiveMQ is exposed in JMX -->
    <managementContext>
      <managementContext createConnector="true"/>
    </managementContext>


    <!--
      Configure network connector to use multicast protocol
      For more information, see
     
      http://activemq.apache.org/multicast-transport-reference.html
    -->
	  <networkConnectors>
	   <networkConnector uri="multicast://default"
	    dynamicOnly="true"
	    networkTTL="3"
	    prefetchSize="1"
	    decreaseNetworkConsumerPriority="true" />
	  </networkConnectors>
	  
    <persistenceAdapter>
      <!--<kahaDB directory="${activemq.base}/data/dynamic-broker1/kahadb"/>-->
      <kahaDB directory="D:/"/>
    </persistenceAdapter>
   
    <!-- The maximum amount of space the broker will use before slowing down producers -->
    <systemUsage>
      <systemUsage>
        <memoryUsage>
          <memoryUsage limit="20 mb"/>
        </memoryUsage>
        <storeUsage>
          <storeUsage limit="1 gb" name="foo"/>
        </storeUsage>
        <tempUsage>
          <tempUsage limit="100 mb"/>
        </tempUsage>
      </systemUsage>
    </systemUsage>


	  <!--
      The transport connectors ActiveMQ will listen to
      Configure discovery URI to use multicast protocol
    -->
    <transportConnectors>
      <transportConnector name="openwire" uri="tcp://0.0.0.0:61616" discoveryUri="multicast://default" />
    </transportConnectors>
   


  </broker>


</beans>

上述2个配置文件中,重点在于2个配置文件在同一个目录,<kahaDB directory="D:/"/>

分别启动2个broker。

客户端连接按照:private static final String URL="failover:(tcp://localhost:61616,tcp://localhost:61618)"; 进行连接,即初始61616端口为Master,61618为Slave .

1、验证是否可以正常发送接收。

启动producer发送消息,日志输出

[Thread-1] Sending message: 'Message: 0 sent at: Wed Oct 30 09:46:42 CST 2013  ...'
[Thread-1] Sending message: 'Message: 1 sent at: Wed Oct 30 09:46:52 CST 2013  ...'
[Thread-1] Sending message: 'Message: 2 sent at: Wed Oct 30 09:47:02 CST 2013  ...'
[Thread-1] Sending message: 'Message: 3 sent at: Wed Oct 30 09:47:12 CST 2013  ...'
[Thread-1] Sending message: 'Message: 4 sent at: Wed Oct 30 09:47:22 CST 2013  ...'
[Thread-1] Sending message: 'Message: 5 sent at: Wed Oct 30 09:47:32 CST 2013  ...'

启动consumer接收消息,日志输出

[Thread-1] Received: 'Message: 0 sent at: Wed Oct 30 09:46:42 CST 2013  ...' (length 255)
[Thread-1] Received: 'Message: 1 sent at: Wed Oct 30 09:46:52 CST 2013  ...' (length 255)
[Thread-1] Received: 'Message: 2 sent at: Wed Oct 30 09:47:02 CST 2013  ...' (length 255)
[Thread-1] Received: 'Message: 3 sent at: Wed Oct 30 09:47:12 CST 2013  ...' (length 255)
[Thread-1] Received: 'Message: 4 sent at: Wed Oct 30 09:47:22 CST 2013  ...' (length 255)
[Thread-1] Received: 'Message: 5 sent at: Wed Oct 30 09:47:32 CST 2013  ...' (length 255)
说明收发正常。

2、验证Master宕机时,Slave进行消息处理,是否有消息丢失。

先停止consumer,记住此时已经接收了消息的序号,(此时消息id是18 待重启的时候看是否从下一个序号开始接收),procuder一直保持消息的发送,

停止61616端口上的master ,然后再启动consumer,发现consumer接收的消息从第19个消息开始接收。

Connecting to URL: failover:(tcp://localhost:61616,tcp://localhost:61618)
Consuming queue: TOOL.DEFAULT.SJ
Using a non-durable subscription
Running 1 parallel threads
log4j:WARN No appenders could be found for logger (org.apache.activemq.transport.failover.FailoverTransport).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
[Thread-1] Received: 'Message: 19 sent at: Wed Oct 30 09:49:52 CST 2013 ...' (length 255)
说明master宕机后,不用进行任何处理,slave就可以继续处理消息了。



<think>嗯,用户想在Jenkins中配置从共享目录下载工具到执行节点的功能。这个问题很具体,说明用户正在搭建Jenkins自动化环境,可能是个DevOps工程师或者系统管理员。 用户提到的"共享目录"这个关键词很重要,需要确认是SMB/NFS网络共享还是版本控制系统的共享。不过从上下文看,应该是指服务器文件系统的共享目录。用户没有说明工具包的类型,可能是二进制文件、脚本或者压缩包。 考虑到Jenkins的特性,这个需求可以通过多种方式实现。最直接的是用"Copy To Slave"插件,但需要确认用户环境是否允许安装插件。如果用户有安限制,用Pipeline的stash/unstash指令也是不错的选择,不过要注意文件大小限制。 用户可能还隐含了版本管理的需求,因为"工具"通常需要更新。虽然问题没明说,但在解决方案里可以提示用户考虑版本控制,比如在共享路径中加入版本号。 这个需求在CI/CD流水线中很常见,特别是当多个节点需要相同部署环境时。用户能提出这个问题,说明对Jenkins的分布式特性已经有基本理解,但可能还不熟悉文件传输的具体机制。 需要提醒用户注意执行节点的操作系统差异,特别是如果共享目录来自Windows而节点是Linux时,路径格式和权限会是个坑。在示例代码里特意用了${env.NODE_NAME}变量就是为这个准备。 最后生成的扩展问题覆盖了用户可能需要的进阶知识,比如大文件传输和权限配置,这些都是实际部署时必然遇到的痛点。特别是"如何自动清理"这点,新手很容易忽略但非常重要,避免磁盘被旧版本工具塞满。</think>在 Jenkins 中配置从共享目录下载工具到执行节点(Agent),可以通过以下几种方式实现: ### 方法 1:使用 "Copy To Slave" 插件(推荐) 1. **安装插件** 在 Jenkins 管理界面安装 [Copy To Slave 插件](https://plugins.jenkins.io/copy-to-slave/)。 2. **配置共享目录路径** 在 Jenkins 配置中设置共享目录路径: - 进入 **Manage Jenkins > Configure System** - 找到 **Copy To Slave Plugin** 部分 - 填写共享目录的绝对路径(例如 `/mnt/shared-tools`) 3. **在 Pipeline 中使用** 通过 `copyToSlave` 步骤将文件复制到 Agent: ```groovy pipeline { agent any stages { stage('Setup Tools') { steps { // 将共享目录中的工具复制到当前 Agent 的工作空间 copyToSlave( includes: 'jdk-17.tar.gz, maven-3.9.6.zip', // 文件名列表 targetLocation: "${env.WORKSPACE}/tools/" // 目标路径 ) // 解压工具 sh 'tar -xzf ${WORKSPACE}/tools/jdk-17.tar.gz -C ${WORKSPACE}/tools/' } } } } ``` ### 方法 2:通过 Pipeline 的 `stash`/`unstash`(适用于小文件) ```groovy pipeline { agent { label 'master' } // 在 Master 节点准备文件 stages { stage('Prepare Tools') { steps { script { // 从共享目录复制到 Master 的临时目录 sh 'cp /mnt/shared-tools/jdk-17.tar.gz .' stash name: 'jdk', includes: 'jdk-17.tar.gz' // 暂存文件 } } } } post { always { agent { label 'linux-agent' } // 在目标 Agent 执行 steps { unstash 'jdk' // 提取文件到 Agent 工作空间 sh 'tar -xzf jdk-17.tar.gz' } } } } ``` ### 方法 3:直接通过 Agent 命令下载(需网络互通) ```groovy stage('Download Tools') { steps { sh ''' # 从共享目录下载(需 Agent 能访问共享存储) scp user@fileserver:/mnt/shared-tools/jdk-17.tar.gz ./ # 或使用 smbclient smbclient //fileserver/share -U user%pass -c "get jdk-17.tar.gz" ''' } } ``` --- ### 关键注意事项 1. **权限配置** - 确保 Jenkins Master 和 Agent 对共享目录有**读写权限**。 - 若使用 SMB/NFS,需在 Agent 节点提前挂载共享目录。 2. **路径兼容性** - Windows Agent 使用 `bat` 和反斜杠路径: ```groovy bat 'copy \\\\fileserver\\share\\jdk-17.zip .' ``` 3. **插件替代方案** 若无法安装插件,可通过 **SSH 插件** 或 **Powershell 插件** 执行远程命令传输文件。 --- ### 最佳实践建议 - **版本管理**:在共享目录中使用带版本号的子目录(如 `/mnt/shared-tools/jdk/17.0.8/`),避免覆盖冲突[^1]。 - **增量同步**:通过 `rsync` 减少传输量(适用于大型工具包): ```groovy sh 'rsync -avz user@fileserver:/mnt/shared-tools/jdk-17/ ${WORKSPACE}/tools/jdk/' ``` - **环境变量**:解压后设置工具路径: ```groovy withEnv(['PATH+TOOLS=${WORKSPACE}/tools/jdk/bin:${WORKSPACE}/tools/maven/bin']) { sh 'mvn --version' } ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值