作者 王勇

添加个人rabbitmq,尝试封装

... ... @@ -16,11 +16,15 @@ spring:
# rabbitmq配置
rabbitmq:
host: 192.168.1.63
host: 192.168.37.137
port: 5672
username: mrz
password: vmvnv1v2
username: rabbit
password: 123456
virtual-host: V_zicheng
# host: 192.168.1.63
# port: 5672
# username: mrz
# password: vmvnv1v2
# 多环境配置
profiles:
active: dev
... ...
package com.sunyo.wlpt.message.bus.service.controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author 子诚
* Description:
* 时间:2020/7/16 14:46
*/
@CrossOrigin
@RequestMapping("bus/rabbit")
@RestController
public class RabbitController {
}
... ...
package com.sunyo.wlpt.message.bus.service.rabbit;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author 子诚
* Description:交换机类型是direct(直连)的rabbit的配置文件
* 时间:2020/7/16 16:20
*/
@Configuration
public class DirectExchangeRabbitConfig {
// 定义直连交换机
public static final String DIRECT_EXCHANGE_NAME = "E_direct";
private static final String queue4BindingKey1 = "big";
private static final String queue4BindingKey2 = "small";
private static final String queue5BindingKey = "cat";
// 声明直连交换机
@Bean
public DirectExchange directExchange() {
return new DirectExchange(DIRECT_EXCHANGE_NAME);
}
// 声明消息队列
@Bean
public Queue messageQueue4() {
return new Queue("queue4");
}
@Bean
public Queue messageQueue5() {
return new Queue("queue5");
}
// 向直连交换机上绑定队列
@Bean
Binding bindingQueue4Exchange1(Queue messageQueue4, DirectExchange directExchange) {
return BindingBuilder.bind( messageQueue4 )
.to( directExchange )
.with( queue4BindingKey1 );
}
@Bean
Binding bindingQueue4Exchange2(Queue messageQueue4, DirectExchange directExchange) {
return BindingBuilder.bind( messageQueue4 )
.to( directExchange )
.with( queue4BindingKey2 );
}
@Bean
Binding bindingQueue5Exchange(Queue messageQueue5, DirectExchange directExchange) {
return BindingBuilder.bind( messageQueue5 )
.to( directExchange )
.with( queue5BindingKey );
}
}
... ...
package com.sunyo.wlpt.message.bus.service.rabbit;
import com.sunyo.wlpt.message.bus.service.domain.BusExchange;
import com.sunyo.wlpt.message.bus.service.domain.BusQueue;
import com.sunyo.wlpt.message.bus.service.domain.UserMessageBinding;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* @author 子诚
* Description:
* 时间:2020/7/16 16:32
*/
@Slf4j
@RefreshScope
@Component
public class DirectRabbitUtils {
@Resource
private AmqpAdmin amqpAdmin;
@Resource
private RabbitTemplate rabbitTemplate;
@Value("${spring.rabbitmq.virtual-host}")
private String v_host;
/**
* 创建交换机(交换机名称,是否持久化,是否删除)
*
* @param busExchange {@link BusExchange}
*/
public void createExchange(BusExchange busExchange) {
// 类型-直连路由
String type_direct = "direct";
// 类型-动态路由
String type_topic = "topic";
// 类型-广播
String type_fanout = "fanout";
// 类型-头部
String type_headers = "headers";
// 创建交换机,直连接类型
if (type_direct.equals(busExchange.getExchangeType())) {
amqpAdmin.declareExchange(
new DirectExchange(busExchange.getExchangeName(), busExchange.getDurability(), busExchange.getAutoDelete())
);
log.info("创建了交换机:{};类型:{};", busExchange.getExchangeType(), type_direct);
}
// 创建交换机,扇形交换机
if (type_topic.equals(busExchange.getExchangeType())) {
amqpAdmin.declareExchange(
new TopicExchange(busExchange.getExchangeName(), busExchange.getDurability(), busExchange.getAutoDelete())
);
log.info("创建了交换机:{};类型:{};", busExchange.getExchangeType(), type_topic);
}
// 创建交换机,广播(主题)交换机
if (type_fanout.equals(busExchange.getExchangeType())) {
amqpAdmin.declareExchange(
new FanoutExchange(busExchange.getExchangeName(), busExchange.getDurability(), busExchange.getAutoDelete())
);
log.info("创建了交换机:{};类型:{};", busExchange.getExchangeType(), type_fanout);
}
// 创建交换机,首部交换机
if (type_headers.equals(busExchange.getExchangeType())) {
amqpAdmin.declareExchange(
new HeadersExchange(busExchange.getExchangeName(), busExchange.getDurability(), busExchange.getAutoDelete())
);
log.info("创建了交换机:{};类型:{};", busExchange.getExchangeType(), type_headers);
}
}
/**
* 根据交换机名称,删除虚拟机
*
* @param exchangeName 交换机名称
*/
public void deleteExchange(String exchangeName) {
boolean flag = amqpAdmin.deleteExchange(exchangeName);
}
/**
* 创建队列
*
* @param busQueue {@link BusQueue}
*/
public void createQueue(BusQueue busQueue) {
amqpAdmin.declareQueue(
new Queue(busQueue.getQueueName(), busQueue.getDurability(), false, busQueue.getAutoDelete())
);
}
/**
* 删除队列,根据队列名称
*
* @param queueName 队列名称
*/
public void deleteQueue(String queueName) {
boolean flag = amqpAdmin.deleteQueue(queueName);
}
public void createBing(UserMessageBinding userMessageBinding) {
amqpAdmin.declareBinding(
new Binding(userMessageBinding.getQueueName(), Binding.DestinationType.QUEUE, userMessageBinding.getExchangeName(), userMessageBinding.getRoutingKeyName(), null)
);
}
}
... ...
package com.sunyo.wlpt.message.bus.service.rabbit;
package com.sunyo.wlpt.message.bus.service.utils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
... ...