|
@@ -0,0 +1,100 @@
|
|
|
+package com.persagy.apm.diagnose.config;
|
|
|
+
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.amqp.core.*;
|
|
|
+import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
|
|
|
+import org.springframework.amqp.rabbit.connection.ConnectionFactory;
|
|
|
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.context.annotation.Scope;
|
|
|
+
|
|
|
+
|
|
|
+@Configuration
|
|
|
+public class RabbitConfig {
|
|
|
+
|
|
|
+ private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
|
|
+
|
|
|
+ @Value("${spring.rabbitmq.host}")
|
|
|
+ private String host;
|
|
|
+
|
|
|
+ @Value("${spring.rabbitmq.port}")
|
|
|
+ private int port;
|
|
|
+
|
|
|
+ @Value("${spring.rabbitmq.username}")
|
|
|
+ private String username;
|
|
|
+
|
|
|
+ @Value("${spring.rabbitmq.password}")
|
|
|
+ private String password;
|
|
|
+
|
|
|
+
|
|
|
+ public static final String EXCHANGE_MAINTENANCE = "exchange_maintenance";
|
|
|
+ public static final String EXCHANGE_INDICATOR = "exchange_indicator";
|
|
|
+
|
|
|
+ public static final String QUEUE_MAINTENANCE = "QUEUE_maintenance";
|
|
|
+ public static final String QUEUE_INDICATOR = "QUEUE_indicator";
|
|
|
+
|
|
|
+ public static final String ROUTINGKEY_MAINTENANCE = "routingKey_maintenance";
|
|
|
+ public static final String ROUTINGKEY_INDICATOR = "routingKey_indicator";
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public ConnectionFactory connectionFactory() {
|
|
|
+ CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host,port);
|
|
|
+ connectionFactory.setUsername(username);
|
|
|
+ connectionFactory.setPassword(password);
|
|
|
+ connectionFactory.setVirtualHost("/");
|
|
|
+ connectionFactory.setPublisherConfirms(true);
|
|
|
+ return connectionFactory;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
|
|
+
|
|
|
+ public RabbitTemplate rabbitTemplate() {
|
|
|
+ RabbitTemplate template = new RabbitTemplate(connectionFactory());
|
|
|
+ return template;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * Direct 交换机
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ public TopicExchange defaultExchange() {
|
|
|
+ return new TopicExchange(EXCHANGE_INDICATOR);
|
|
|
+ }
|
|
|
+
|
|
|
+ * 获取队列A
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ public Queue queueIndicator() {
|
|
|
+ return new Queue(QUEUE_INDICATOR, true);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 绑定 将队列和交换机绑定,并设置匹配键
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ public Binding binding() {
|
|
|
+ return BindingBuilder.bind(queueIndicator()).to(defaultExchange()).with(RabbitConfig.ROUTINGKEY_INDICATOR);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 获取队列B
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ public Queue queueMaintenance() {
|
|
|
+ return new Queue(QUEUE_MAINTENANCE, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public Binding bindingMaintenance() {
|
|
|
+ return BindingBuilder.bind(queueMaintenance()).to(defaultExchange()).with(RabbitConfig.ROUTINGKEY_MAINTENANCE);
|
|
|
+ }
|
|
|
+}
|