添加mq
This commit is contained in:
@@ -1,45 +1,59 @@
|
||||
package com.xwbing.Rabbit;
|
||||
|
||||
import org.springframework.amqp.core.Binding;
|
||||
import org.springframework.amqp.core.BindingBuilder;
|
||||
import org.springframework.amqp.core.Queue;
|
||||
import org.springframework.amqp.core.TopicExchange;
|
||||
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;
|
||||
|
||||
/**
|
||||
* 项目名称: boot-module-demo
|
||||
* 创建时间: 2018/4/25 14:28
|
||||
* 创建时间: 2018/4/27 13:42
|
||||
* 作者: xiangwb
|
||||
* 说明: Rabbit配置类
|
||||
* 说明:
|
||||
*/
|
||||
@Configuration
|
||||
public class RabbitConfig {
|
||||
//声明队列
|
||||
@Bean
|
||||
public Queue serverInvokeQueue() {
|
||||
return new Queue(RabbitConstant.SERVER_INVOKE_QUEUE, true);//true表示持久化该队列
|
||||
}
|
||||
@Value("${spring.rabbitmq.host}")
|
||||
private String host;
|
||||
@Value("${spring.rabbitmq.port}")
|
||||
private String port;
|
||||
@Value("${spring.rabbitmq.username}")
|
||||
private String username;
|
||||
@Value("${spring.rabbitmq.password}")
|
||||
private String password;
|
||||
@Value("${spring.rabbitmq.virtual-host}")
|
||||
private String virtualHost;
|
||||
@Value("${spring.rabbitmq.connection-timeout}")
|
||||
private int timeout;
|
||||
@Value("${spring.rabbitmq.publisher-confirms}")
|
||||
private boolean publisherConfirms;
|
||||
@Value("${spring.rabbitmq.publisher-returns}")
|
||||
private boolean publisherReturns;
|
||||
|
||||
@Bean
|
||||
public Queue httpRequestQueue() {
|
||||
return new Queue(RabbitConstant.HTTP_REQUEST_QUEUE, true);
|
||||
public ConnectionFactory connectionFactory() {
|
||||
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
|
||||
connectionFactory.setAddresses(host + ":" + port);
|
||||
connectionFactory.setUsername(username);
|
||||
connectionFactory.setPassword(password);
|
||||
connectionFactory.setVirtualHost(virtualHost);
|
||||
connectionFactory.setConnectionTimeout(timeout);
|
||||
// 如果要进行消息回调,则这里必须要设置为true
|
||||
connectionFactory.setPublisherConfirms(publisherConfirms);
|
||||
connectionFactory.setPublisherReturns(publisherReturns);
|
||||
return connectionFactory;
|
||||
}
|
||||
|
||||
//声明交换器
|
||||
/**
|
||||
* 因为要设置回调类,所以应是prototype类型,如果是singleton类型,则回调类为最后一次设置
|
||||
*/
|
||||
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
||||
@Bean
|
||||
public TopicExchange topicExchange() {
|
||||
return new TopicExchange(RabbitConstant.CONTROL_EXCHANGE);
|
||||
}
|
||||
|
||||
//绑定
|
||||
@Bean
|
||||
public Binding bindingExchangeMessage() {
|
||||
return BindingBuilder.bind(serverInvokeQueue()).to(topicExchange()).with(RabbitConstant.SERVER_INVOKE_ROUTING_KEY);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Binding bindingExchangeMessages() {
|
||||
return BindingBuilder.bind(httpRequestQueue()).to(topicExchange()).with(RabbitConstant.HTTP_REQUEST_ROUTING_KEY);
|
||||
public RabbitTemplate rabbitTemplate() {
|
||||
return new RabbitTemplate(connectionFactory());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,21 +10,21 @@ public class RabbitConstant {
|
||||
/**
|
||||
* 服务器调用
|
||||
*/
|
||||
public static final String SERVER_INVOKE_QUEUE = "server_invoke";
|
||||
public static final String SERVER_INVOKE_QUEUE = "server.invoke";
|
||||
/**
|
||||
* http请求
|
||||
*/
|
||||
public static final String HTTP_REQUEST_QUEUE = "http_request";
|
||||
public static final String HTTP_REQUEST_QUEUE = "http.request";
|
||||
/**
|
||||
* 服务器调用路由键
|
||||
* 服务器调用路由键(*表示一个词,#表示零个或多个词)
|
||||
*/
|
||||
public static final String SERVER_INVOKE_ROUTING_KEY = "server_invoke.key";
|
||||
public static final String SERVER_INVOKE_ROUTING_KEY = "server.invoke.key";
|
||||
/**
|
||||
* http请求路由键
|
||||
*/
|
||||
public static final String HTTP_REQUEST_ROUTING_KEY = "http_request.key";
|
||||
public static final String HTTP_REQUEST_ROUTING_KEY = "http.request.key";
|
||||
/**
|
||||
* 交换器
|
||||
* 交换机
|
||||
*/
|
||||
public static final String CONTROL_EXCHANGE = "control_exchange";
|
||||
public static final String CONTROL_EXCHANGE = "control.exchange";
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.xwbing.Rabbit;
|
||||
|
||||
import org.springframework.amqp.core.Binding;
|
||||
import org.springframework.amqp.core.BindingBuilder;
|
||||
import org.springframework.amqp.core.Queue;
|
||||
import org.springframework.amqp.core.TopicExchange;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 项目名称: boot-module-demo
|
||||
* 创建时间: 2018/4/25 14:28
|
||||
* 作者: xiangwb
|
||||
* 说明: topic交换机配置
|
||||
*/
|
||||
@Configuration
|
||||
public class TopicExchangeConfig {
|
||||
//声明队列
|
||||
@Bean
|
||||
public Queue serverInvokeQueue() {
|
||||
return new Queue(RabbitConstant.SERVER_INVOKE_QUEUE, true);//true表示持久化该队列
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Queue httpRequestQueue() {
|
||||
return new Queue(RabbitConstant.HTTP_REQUEST_QUEUE, true);
|
||||
}
|
||||
|
||||
//声明交换机
|
||||
@Bean
|
||||
public TopicExchange topicExchange() {
|
||||
return new TopicExchange(RabbitConstant.CONTROL_EXCHANGE);
|
||||
}
|
||||
|
||||
//绑定
|
||||
@Bean
|
||||
public Binding bindingExchangeMessage() {
|
||||
return BindingBuilder.bind(serverInvokeQueue()).to(topicExchange()).with(RabbitConstant.SERVER_INVOKE_ROUTING_KEY);//*表示一个词,#表示零个或多个词
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Binding bindingExchangeMessages() {
|
||||
return BindingBuilder.bind(httpRequestQueue()).to(topicExchange()).with(RabbitConstant.HTTP_REQUEST_ROUTING_KEY);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.xwbing;
|
||||
|
||||
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@@ -9,6 +10,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
* 作者: boot-module-demo
|
||||
* 说明: 启动类
|
||||
*/
|
||||
//开启rabbitmq
|
||||
@EnableRabbit
|
||||
@SpringBootApplication
|
||||
//@Configuration 定义为配置类,相当于xml文件中的<beans></beans>
|
||||
//@ComponentScan 组件扫描
|
||||
|
||||
@@ -52,8 +52,10 @@ spring:
|
||||
port:
|
||||
username:
|
||||
password:
|
||||
virtual-host:
|
||||
virtual-host: /
|
||||
connection-timeout:
|
||||
publisher-confirms: true
|
||||
publisher-returns: true
|
||||
#log
|
||||
logging:
|
||||
config: classpath:log4j2.xml
|
||||
|
||||
Reference in New Issue
Block a user