|
@@ -1,293 +0,0 @@
|
|
|
-package com.usky.dxtop.service.config;
|
|
|
-
|
|
|
-import lombok.Data;
|
|
|
-import org.springframework.amqp.core.*;
|
|
|
-import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
|
|
|
-import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
|
|
|
-import org.springframework.amqp.rabbit.connection.ConnectionFactory;
|
|
|
-import org.springframework.amqp.rabbit.core.RabbitAdmin;
|
|
|
-import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
|
|
-import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
-import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
|
|
|
-import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
|
|
|
-import org.springframework.context.annotation.Bean;
|
|
|
-import org.springframework.context.annotation.Configuration;
|
|
|
-import org.springframework.context.annotation.Primary;
|
|
|
-
|
|
|
-/**
|
|
|
- * @author yq
|
|
|
- * @date 2021/9/9 13:37
|
|
|
- */
|
|
|
-@Data
|
|
|
-@Configuration
|
|
|
-public class RabbitmqConfig {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public static final String PERSON = "profile_trans_produce";
|
|
|
- public static final String CARD = "card_trans_produce";
|
|
|
- public static final String CHARGE = "charge_trans_produce";
|
|
|
- public static final String DISH = "dish_trade_produce";
|
|
|
-
|
|
|
- public final static String CONNECTION = "ConnectionFactory";
|
|
|
- public final static String TEMPLATE = "RabbitTemplate";
|
|
|
- public final static String LISTENER = "ListenerFactory";
|
|
|
- public final static String ADMIN = "Admin";
|
|
|
- public final static String EXCHANGE = "Exchange";
|
|
|
- public final static String QUEUE = "Queue";
|
|
|
- public final static String BINDING = "Bin";
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private RabbitProperties rabbitProperties;
|
|
|
-
|
|
|
- /**
|
|
|
- * 连接工厂
|
|
|
- * @param vHost
|
|
|
- * @return
|
|
|
- */
|
|
|
- public ConnectionFactory connectionFactory(String vHost) {
|
|
|
- CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
|
|
|
- connectionFactory.setHost(rabbitProperties.getHost());
|
|
|
- connectionFactory.setPort(rabbitProperties.getPort());
|
|
|
- connectionFactory.setUsername(rabbitProperties.getUsername());
|
|
|
- connectionFactory.setPassword(rabbitProperties.getPassword());
|
|
|
- connectionFactory.setVirtualHost(vHost);
|
|
|
- return connectionFactory;
|
|
|
- }
|
|
|
- /**
|
|
|
- * 发送消息模版
|
|
|
- * @param connectionFactory
|
|
|
- * @return
|
|
|
- */
|
|
|
- public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
|
|
|
- RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
|
|
|
- rabbitTemplate.setMessageConverter(this.messageConverter());
|
|
|
- return rabbitTemplate;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 监听工厂
|
|
|
- * @param configurer
|
|
|
- * @param connectionFactory
|
|
|
- * @return
|
|
|
- */
|
|
|
- public SimpleRabbitListenerContainerFactory listenerFactory(
|
|
|
- SimpleRabbitListenerContainerFactoryConfigurer configurer,
|
|
|
- ConnectionFactory connectionFactory) {
|
|
|
- SimpleRabbitListenerContainerFactory listenerContainerFactory = new SimpleRabbitListenerContainerFactory();
|
|
|
- //设置手动ack
|
|
|
- listenerContainerFactory.setAcknowledgeMode(AcknowledgeMode.AUTO);
|
|
|
- configurer.configure(listenerContainerFactory, connectionFactory);
|
|
|
- return listenerContainerFactory;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 序列化
|
|
|
- * @return
|
|
|
- */
|
|
|
- public Jackson2JsonMessageConverter messageConverter() {
|
|
|
- return new Jackson2JsonMessageConverter();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 交换机
|
|
|
- * @param name
|
|
|
- * @param amqpAdmin
|
|
|
- * @return
|
|
|
- */
|
|
|
- public DirectExchange exchange(String name, AmqpAdmin amqpAdmin){
|
|
|
- DirectExchange exchange = new DirectExchange(name, true, false);
|
|
|
- exchange.setAdminsThatShouldDeclare(amqpAdmin);
|
|
|
- return exchange;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 队列
|
|
|
- * @param name
|
|
|
- * @param amqpAdmin
|
|
|
- * @return
|
|
|
- */
|
|
|
- public Queue queue(String name, AmqpAdmin amqpAdmin){
|
|
|
- Queue queue = new Queue(name, true);
|
|
|
- queue.setAdminsThatShouldDeclare(amqpAdmin);
|
|
|
- return queue;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 连接配置
|
|
|
- * @return
|
|
|
- */
|
|
|
- @Bean(name = "personConnectionFactory")
|
|
|
- @Primary
|
|
|
- public ConnectionFactory personConnectionFactory(){
|
|
|
- return this.connectionFactory(PERSON);
|
|
|
- }
|
|
|
-
|
|
|
- @Bean(name = "cartConnectionFactory")
|
|
|
- public ConnectionFactory cartConnectionFactory(){
|
|
|
- return this.connectionFactory(CARD);
|
|
|
- }
|
|
|
-
|
|
|
- @Bean(name = "chargeConnectionFactory")
|
|
|
- public ConnectionFactory chargeConnectionFactory(){
|
|
|
- return this.connectionFactory(CHARGE);
|
|
|
- }
|
|
|
-
|
|
|
- @Bean(name = "dishConnectionFactory")
|
|
|
- public ConnectionFactory dishConnectionFactory(){
|
|
|
- return this.connectionFactory(DISH);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 发送模版配置
|
|
|
- */
|
|
|
- @Bean(name = "personRabbitTemplate")
|
|
|
- @Primary
|
|
|
- public RabbitTemplate personRabbitTemplate(@Qualifier("personConnectionFactory") ConnectionFactory connectionFactory ) {
|
|
|
- return this.rabbitTemplate(connectionFactory);
|
|
|
- }
|
|
|
-
|
|
|
- @Bean(name = "cartRabbitTemplate")
|
|
|
- public RabbitTemplate cartRabbitTemplate(@Qualifier("cartConnectionFactory") ConnectionFactory connectionFactory ) {
|
|
|
- return this.rabbitTemplate(connectionFactory);
|
|
|
- }
|
|
|
-
|
|
|
- @Bean(name = "chargeRabbitTemplate")
|
|
|
- public RabbitTemplate chargeRabbitTemplate(@Qualifier("chargeConnectionFactory") ConnectionFactory connectionFactory ) {
|
|
|
- return this.rabbitTemplate(connectionFactory);
|
|
|
- }
|
|
|
-
|
|
|
- @Bean(name = "dishRabbitTemplate")
|
|
|
- public RabbitTemplate dishRabbitTemplate(@Qualifier("dishConnectionFactory") ConnectionFactory connectionFactory ) {
|
|
|
- return this.rabbitTemplate(connectionFactory);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 监听工厂配置
|
|
|
- */
|
|
|
- @Bean(name = "personListenerFactory")
|
|
|
- @Primary
|
|
|
- public SimpleRabbitListenerContainerFactory personListenerFactory(
|
|
|
- SimpleRabbitListenerContainerFactoryConfigurer configurer,
|
|
|
- @Qualifier("personConnectionFactory") ConnectionFactory connectionFactory) {
|
|
|
- return this.listenerFactory(configurer,connectionFactory);
|
|
|
- }
|
|
|
-
|
|
|
- @Bean(name = "cartListenerFactory")
|
|
|
- public SimpleRabbitListenerContainerFactory cartListenerFactory(
|
|
|
- SimpleRabbitListenerContainerFactoryConfigurer configurer,
|
|
|
- @Qualifier("cartConnectionFactory") ConnectionFactory connectionFactory) {
|
|
|
- return this.listenerFactory(configurer,connectionFactory);
|
|
|
- }
|
|
|
-
|
|
|
- @Bean(name = "chargeListenerFactory")
|
|
|
- public SimpleRabbitListenerContainerFactory chargeListenerFactory(
|
|
|
- SimpleRabbitListenerContainerFactoryConfigurer configurer,
|
|
|
- @Qualifier("chargeConnectionFactory") ConnectionFactory connectionFactory) {
|
|
|
- return this.listenerFactory(configurer,connectionFactory);
|
|
|
- }
|
|
|
-
|
|
|
- @Bean(name = "dishListenerFactory")
|
|
|
- public SimpleRabbitListenerContainerFactory dishListenerFactory(
|
|
|
- SimpleRabbitListenerContainerFactoryConfigurer configurer,
|
|
|
- @Qualifier("dishConnectionFactory") ConnectionFactory connectionFactory) {
|
|
|
- return this.listenerFactory(configurer,connectionFactory);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * admin配置
|
|
|
- */
|
|
|
- @Bean("personAdmin")
|
|
|
- @Primary
|
|
|
- AmqpAdmin personAmqpAdmin(@Qualifier("personConnectionFactory") ConnectionFactory connectionFactory) {
|
|
|
- return new RabbitAdmin(connectionFactory);
|
|
|
- }
|
|
|
- @Bean("cartAdmin")
|
|
|
- AmqpAdmin cartAmqpAdmin(@Qualifier("cartConnectionFactory") ConnectionFactory connectionFactory) {
|
|
|
- return new RabbitAdmin(connectionFactory);
|
|
|
- }
|
|
|
- @Bean("chargeAdmin")
|
|
|
- AmqpAdmin chargeAmqpAdmin(@Qualifier("chargeConnectionFactory") ConnectionFactory connectionFactory) {
|
|
|
- return new RabbitAdmin(connectionFactory);
|
|
|
- }
|
|
|
- @Bean("dishAdmin")
|
|
|
- AmqpAdmin dishAmqpAdmin(@Qualifier("dishConnectionFactory") ConnectionFactory connectionFactory) {
|
|
|
- return new RabbitAdmin(connectionFactory);
|
|
|
- }
|
|
|
- /**
|
|
|
- * 交换机
|
|
|
- */
|
|
|
- @Bean("personExchange")
|
|
|
- @Primary
|
|
|
- public DirectExchange personExchange(@Qualifier("personAdmin") AmqpAdmin amqpAdmin) {
|
|
|
- return this.exchange(PERSON,amqpAdmin);
|
|
|
- }
|
|
|
-
|
|
|
- @Bean("cartExchange")
|
|
|
- public DirectExchange cartExchange(@Qualifier("cartAdmin") AmqpAdmin amqpAdmin) {
|
|
|
- return this.exchange(CARD,amqpAdmin);
|
|
|
- }
|
|
|
- @Bean("chargeExchange")
|
|
|
- public DirectExchange chargeExchange(@Qualifier("chargeAdmin") AmqpAdmin amqpAdmin) {
|
|
|
- return this.exchange(CHARGE,amqpAdmin);
|
|
|
- }
|
|
|
- @Bean("dishExchange")
|
|
|
- public DirectExchange dishExchange(@Qualifier("dishAdmin") AmqpAdmin amqpAdmin) {
|
|
|
- return this.exchange(DISH,amqpAdmin);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 队列
|
|
|
- */
|
|
|
- @Bean("personQueue")
|
|
|
- @Primary
|
|
|
- Queue personQueue(@Qualifier("personAdmin")AmqpAdmin amqpAdmin) {
|
|
|
- return this.queue(PERSON,amqpAdmin);
|
|
|
- }
|
|
|
-
|
|
|
- @Bean("cartQueue")
|
|
|
- Queue cartQueue(@Qualifier("cartAdmin")AmqpAdmin amqpAdmin) {
|
|
|
- return this.queue(CARD,amqpAdmin);
|
|
|
- }
|
|
|
- @Bean("chargeQueue")
|
|
|
- Queue chargeQueue(@Qualifier("chargeAdmin")AmqpAdmin amqpAdmin) {
|
|
|
- return this.queue(CHARGE,amqpAdmin);
|
|
|
- }
|
|
|
- @Bean("dishQueue")
|
|
|
- Queue dishQueue(@Qualifier("dishAdmin")AmqpAdmin amqpAdmin) {
|
|
|
- return this.queue(DISH,amqpAdmin);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 绑定
|
|
|
- */
|
|
|
- @Bean("personBin")
|
|
|
- @Primary
|
|
|
- Binding personBindingPersonDirect(@Qualifier("personAdmin")AmqpAdmin amqpAdmin) {
|
|
|
- Binding binding = BindingBuilder.bind(personQueue(amqpAdmin)).to(personExchange(amqpAdmin)).with(PERSON);
|
|
|
- binding.setAdminsThatShouldDeclare(amqpAdmin);
|
|
|
- return binding;
|
|
|
- }
|
|
|
-
|
|
|
- @Bean("cartBin")
|
|
|
- Binding cartBindingPersonDirect(@Qualifier("cartAdmin")AmqpAdmin amqpAdmin) {
|
|
|
- Binding binding = BindingBuilder.bind(cartQueue(amqpAdmin)).to(cartExchange(amqpAdmin)).with(CARD);
|
|
|
- binding.setAdminsThatShouldDeclare(amqpAdmin);
|
|
|
- return binding;
|
|
|
- }
|
|
|
- @Bean("chargeBin")
|
|
|
- Binding chargeBindingPersonDirect(@Qualifier("chargeAdmin")AmqpAdmin amqpAdmin) {
|
|
|
- Binding binding = BindingBuilder.bind(chargeQueue(amqpAdmin)).to(chargeExchange(amqpAdmin)).with(CHARGE);
|
|
|
- binding.setAdminsThatShouldDeclare(amqpAdmin);
|
|
|
- return binding;
|
|
|
- }
|
|
|
- @Bean("dishBin")
|
|
|
- Binding dishBindingPersonDirect(@Qualifier("dishAdmin")AmqpAdmin amqpAdmin) {
|
|
|
- Binding binding = BindingBuilder.bind(dishQueue(amqpAdmin)).to(dishExchange(amqpAdmin)).with(DISH);
|
|
|
- binding.setAdminsThatShouldDeclare(amqpAdmin);
|
|
|
- return binding;
|
|
|
- }
|
|
|
-}
|