从 spring application.properties 禁用监听兔子队列

Posted

技术标签:

【中文标题】从 spring application.properties 禁用监听兔子队列【英文标题】:Disabling the listening to rabbit queues from spring application.properties 【发布时间】:2015-10-13 21:05:53 【问题描述】:

我想在 spring 中创建一个 application-development.properties 文件来定义一个开发环境。在这种环境下想要禁用监听兔子队列,因为我不想在调试等时干扰暂存队列。

问题是 - 我找不到控制它的属性。没有“活动”属性或“启用”属性或任何东西..

这些是我在Spring docs找到的属性:

# RABBIT (RabbitProperties)
spring.rabbitmq.addresses= # connection addresses (e.g. myhost:9999,otherhost:1111)
spring.rabbitmq.dynamic=true # create an AmqpAdmin bean
spring.rabbitmq.host= # connection host
spring.rabbitmq.port= # connection port
spring.rabbitmq.password= # login password
spring.rabbitmq.requested-heartbeat= # requested heartbeat timeout, in seconds; zero for none
spring.rabbitmq.listener.acknowledge-mode= # acknowledge mode of container
spring.rabbitmq.listener.concurrency= # minimum number of consumers
spring.rabbitmq.listener.max-concurrency= # maximum number of consumers
spring.rabbitmq.listener.prefetch= # number of messages to be handled in a single request
spring.rabbitmq.listener.transaction-size= # number of messages to be processed in a transaction
spring.rabbitmq.ssl.enabled=false # enable SSL support
spring.rabbitmq.ssl.key-store= # path to the key store that holds the SSL certificate
spring.rabbitmq.ssl.key-store-password= # password used to access the key store
spring.rabbitmq.ssl.trust-store= # trust store that holds SSL certificates
spring.rabbitmq.ssl.trust-store-password= # password used to access the trust store
spring.rabbitmq.username= # login user
spring.rabbitmq.virtual-host= # virtual host to use when connecting to the broker

我确实找到了一种方法,不使用 Spring profiles 加载包含侦听器定义的 amqp-context.xml bean,并将 <beans profile="development"> .. </beans> 添加到 xml 中,但这不太灵活,因为我必须定义不同的配置文件,更改它们包含的内容涉及更改代码。

编辑这是我的 amqp-context.xml 的样子:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:task="http://www.springframework.org/schema/task" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:rabbit="http://www.springframework.org/schema/rabbit"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/rabbit 
        http://www.springframework.org/schema/rabbit/spring-rabbit-1.3.xsd">

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreResourceNotFound" value="true" />
        <property name="locations">
            <list>
                <value>application.$env:xxxx.properties</value>
            </list>
        </property>
    </bean>
    <rabbit:connection-factory id="connectionFactory" host="$rabbit_host"
        virtual-host="$rabbit_virtual_host" username="$rabbit_username" password="$rabbit_password" port="$rabbit_port"/>   

    <!-- Connection Factory -->
    <bean id="rabbitConnFactory"
        class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
    </bean>


    <!-- Spring AMQP Template -->
    <bean id="template" class="org.springframework.amqp.rabbit.core.RabbitTemplate">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="routingKey" value="$my_queue" />
        <property name="queue" value="$my_queue" />
    </bean>

    <!-- Spring AMQP Admin -->
    <bean id="admin" class="org.springframework.amqp.rabbit.core.RabbitAdmin">
        <constructor-arg ref="rabbitConnFactory" />
    </bean>

    <rabbit:listener-container connection-factory="connectionFactory" requeue-rejected="false" concurrency="10">
        <rabbit:listener ref="ProcessMessage"
            queue-names="$queue_name" />
    </rabbit:listener-container> 

    <bean id="ProcessStuff" class="Process" />



</beans>

有没有人知道如何直接从 application.properties 文件管理侦听队列?请问?

【问题讨论】:

你的 amqp-context.xml 是什么样的? @jst 我把它添加到问题的内容中 【参考方案1】:

作为等待 Boot 1.3 的替代方法,您可以将自己的密钥添加到 application-development.properties,如

rabbit.auto-startup=false

然后像这样修改你的 amqp-context.xml

<rabbit:listener-container connection-factory="connectionFactory" requeue-rejected="false" concurrency="10" auto-startup=$rabbit.auto-startup>

【讨论】:

你能帮我解决相关问题吗***.com/questions/52068307/…【参考方案2】:

好收获!我创建了#3587,它将在 Spring Boot 1.3 中得到解决

谢谢!

【讨论】:

【参考方案3】:

这个“spring.autoconfigure.exclude: org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration”对我没有帮助。所以我只是删除了所有以spring.cloud.stream.* 和 spring.rabbitmq.addresses 开头的属性。也添加到您的 logback 中

<logger name="org.springframework.amqp" level="ERROR"/>
<logger name="org.springframework.boot.actuate.amqp" level="ERROR"/>.

因为当你删除属性时,spring会输出很多WARN日志。

【讨论】:

以上是关于从 spring application.properties 禁用监听兔子队列的主要内容,如果未能解决你的问题,请参考以下文章

进程以退出代码 1 Spring Boot Intellij 结束

如何在运行时编辑 application.properties(以备下次使用)

log4j2.properties 无法从 Docker Environment 或 Kubernetes Env 读取变量

disconf---分布式配置管理平台的搭建(windows版本)

Nacos权限管理

要使用默认的 BatchConfigurer,上下文必须包含不超过一个 DataSource,找到 2