Spring Boot 使用Java配置执行器JMS健康检查返回误报

aydmsdu9  于 2023-10-16  发布在  Spring
关注(0)|答案(1)|浏览(236)

我遇到了一个问题,即使我的路由可以连接到JMS并向JMS生成消息,Actuator探测器也会失败。因此,在短期内执行器是说,它是下来,但它是工作。
技术堆栈和技术说明:

  • Spring罩:2.3.1.RELEASE
  • Camel :3.4.1
  • Artemis:2.11.0
  • Artemis已设置为使用用户名和密码(artemis/artemis)。
  • 使用org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory作为连接工厂。

我的路线很简单:

<route id="timer-cluster-producer-route">
            <from uri="timer:producer-ticker?delay=5000"/>
          
            <setBody>
                <groovy>
                    result = ["Name":"Johnny"]
                </groovy>
            </setBody>
            <marshal>
                <json library="Jackson"/>
            </marshal>
            <to uri="ref:jms-producer-cluster-event" />
   </route>

基于XML的Artemis配置

由于Spring-boot支持基于java的配置,我正忙碌相应地迁移我们的XML bean。因此,我将一个工作beans.xml文件粘贴到项目中,并启动路由,我可以发送消息流,健康检查返回OK。

<beans xmlns="http://www.springframework.org/schema/beans"
    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.xsd
         http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
    <bean id="jmsConnectionFactory"
        class="org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://localhost:61616" />
        <property name="user" value="artemis"/>
        <property name="password" value="artemis"/>
        <property name="connectionLoadBalancingPolicyClassName" value="org.apache.activemq.artemis.api.core.client.loadbalance.RoundRobinConnectionLoadBalancingPolicy"/>
    </bean>
    <!--org.messaginghub.pooled.jms.JmsPoolConnectionFactory-->
    <!--org.apache.activemq.jms.pool.PooledConnectionFactory-->
    <bean id="jmsPooledConnectionFactory"
        class="org.apache.activemq.jms.pool.PooledConnectionFactory"
        init-method="start" destroy-method="stop">
        <property name="maxConnections" value="64" />
        <property name="MaximumActiveSessionPerConnection"
            value="500" />
        <property name="connectionFactory" ref="jmsConnectionFactory" />
    </bean>
    <bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration">
        <property name="connectionFactory"
            ref="jmsPooledConnectionFactory" />
        <property name="concurrentConsumers" value="1" />
        <property name="artemisStreamingEnabled" value="true"/>
    </bean>
    <bean id="jms"
          class="org.apache.camel.component.jms.JmsComponent">
        <property name="configuration" ref="jmsConfig"/>

    </bean>

<!--    <bean id="activemq"
        class="org.apache.activemq.camel.component.ActiveMQComponent">
        <property name="configuration" ref="jmsConfig" />
    </bean>-->
    

</beans>

Spring启动自动(黑色)魔术配置

然后,我使用application.yaml文件配置artemis连接,方法如Spring-boot文档中所述。当我的应用程序.yaml文件包含以下配置时,这也有效:

artemis:
  user: artemis
  host: localhost
  password: artemis
  pool:
    max-sessions-per-connection: 500
    enabled: true
    max-connections: 16

这就像一个魅力。

勇敢尝试Java配置。

因此,我选择了黄金,并尝试了基于Java的配置,如下所示:

@SpringBootApplication
@ImportResource("classpath:/camel/camel.xml")
public class ClusterProducerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ClusterProducerApplication.class, args);
    }
    @Bean
    public JmsComponent jms() throws JMSException {
        // Create the connectionfactory which will be used to connect to Artemis
        ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory();
        cf.setBrokerURL("tcp://localhost:61616");
        cf.setUser("artemis");
        cf.setPassword("artemis");

        //Create connection pool using connection factory
        PooledConnectionFactory pooledConnectionFactory = new PooledConnectionFactory();
        pooledConnectionFactory.setMaxConnections(2);
        pooledConnectionFactory.setConnectionFactory(cf);

        //Create configuration which uses connection factory
        JmsConfiguration jmsConfiguration = new JmsConfiguration();
        jmsConfiguration.setConcurrentConsumers(2);
        jmsConfiguration.setArtemisStreamingEnabled(true);
        jmsConfiguration.setConnectionFactory(pooledConnectionFactory);

        // Create the Camel JMS component and wire it to our Artemis configuration
        JmsComponent jms = new JmsComponent();
        jms.setConfiguration(jmsConfiguration);
        return jms;
    }
}

因此,当camel启动时,我看到以下启动时记录的警告:

020-07-28 12:33:38.631  WARN 25329 --- [)-192.168.1.158] o.s.boot.actuate.jms.JmsHealthIndicator  : JMS health check failed

javax.jms.JMSSecurityException: AMQ229031: Unable to validate user from /127.0.0.1:42028. Username: null; SSL certificate subject DN: unavailable

5秒延迟后,计时器开始计时,并生成消息。我登录到Artemis控制台,我可以浏览消息,并可以看到他们正在创建。但是,当我运行get on actuator health时,我会看到以下内容:

"jms": {
            "status": "DOWN",
            "details": {
                "error": "javax.jms.JMSSecurityException: AMQ229031: Unable to validate user from /127.0.0.1:42816. Username: null; SSL certificate subject DN: unavailable"
            }
        },

我觉得这是个大虫子。

关于连接池实现的观察。

我注意到AMQ连接池已经被移动到以下maven依赖项中:

<dependency>
  <groupId>org.messaginghub</groupId>
  <artifactId>pooled-jms</artifactId>
</dependency>

我想让我也给予一次。它显示了与上面概述的相同的行为,还有一个有趣的事情。当使用org.messaginghub.pooled-jms作为连接池时(spring-boot docs也推荐),启动时会记录以下内容。

2020-07-28 12:41:37.255  INFO 26668 --- [           main] o.m.pooled.jms.JmsPoolConnectionFactory  : JMS ConnectionFactory on classpath is not a JMS 2.0+ version.

这很奇怪,因为根据the official repo,连接器符合JMS 2.0。

**快速总结:**通过Java配置JMS组件时,actuator似乎没有获取连接工厂的凭据。虽然目前存在使用spring-boot application.yaml配置的解决方案,但它限制了您在Camel上配置JMS客户端的方式。

vdzxcuhz

vdzxcuhz1#

所以经过一些挖掘和reaching out to the Spring Boot people on GitHub,我发现了问题所在。在使用Java配置时,我使用连接工厂配置Camel的JMS组件。然而,Spring-boot完全没有意识到这一点,因为它是Camel组件。因此,JMS使用的连接工厂需要暴露给Spring-boot才能工作。
解决办法相对简单。代码如下:

@Configuration
public class ApplicationConfiguration {
    private ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory();
    @Bean
    public JmsComponent jms() throws JMSException {
        // Create the connectionfactory which will be used to connect to Artemis
        cf.setBrokerURL("tcp://localhost:61616");
        cf.setUser("artemis");
        cf.setPassword("artemis");
        
        // Setup Connection pooling
        PooledConnectionFactory pooledConnectionFactory = new PooledConnectionFactory();
        pooledConnectionFactory.setMaxConnections(2);
        pooledConnectionFactory.setConnectionFactory(cf);
        JmsConfiguration jmsConfiguration = new JmsConfiguration();
        jmsConfiguration.setConcurrentConsumers(2);
        jmsConfiguration.setArtemisStreamingEnabled(true);
        jmsConfiguration.setConnectionFactory(pooledConnectionFactory);
        // Create the Camel JMS component and wire it to our Artemis connectionfactory
        JmsComponent jms = new JmsComponent();
        jms.setConfiguration(jmsConfiguration);
        return jms;
    }
    /*
       This line will expose the connection factory to Spring-boot.
    */
    @Bean
    public ConnectionFactory jmsConnectionFactory() {
        return cf;
    }
}

相关问题