springboot配置多个yml_spring几种配置方式

springboot配置多个yml_spring几种配置方式YMLrabbitmq:first:username:${app.appkey}password:${app.appkey}virtual-host:${app.appid}addresses:x.x.x.x:5672,x.x.x.x:5672second:username:guestpassword:guestvirtual-host:/host:12

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

  • YML
 rabbitmq:
    first:
      username: ${app.appkey}
      password: ${app.appkey}
      virtual-host: ${app.appid}
      addresses: x.x.x.x:5672,x.x.x.x:5672 #集群
    second:
      username: guest 
      password: guest
      virtual-host: /
      host: 127.0.0.1
      port: 5672
  • 配置源
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.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
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;

/**
 * RabbitMq多源配置
 *
 * @author Lenovo
 */
@Configuration
public class RabbitConfig {

    @Bean(name = "firstConnectionFactory")
    @Primary
    public ConnectionFactory firstConnectionFactory(
            @Value("${spring.rabbitmq.first.addresses}") String addresses,
            @Value("${spring.rabbitmq.first.username}") String username,
            @Value("${spring.rabbitmq.first.password}") String password,
            @Value("${spring.rabbitmq.first.virtual-host}") String virtualHost
    ) {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();

        connectionFactory.setAddresses(addresses);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(virtualHost);
        return connectionFactory;
    }

    @Bean(name = "secondConnectionFactory")
    public ConnectionFactory secondConnectionFactory(
            @Value("${spring.rabbitmq.second.host}") String host,
            @Value("${spring.rabbitmq.second.port}") int port,
            @Value("${spring.rabbitmq.second.username}") String username,
            @Value("${spring.rabbitmq.second.password}") String password,
            @Value("${spring.rabbitmq.second.virtual-host}") String virtualHost
    ) {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(virtualHost);
        return connectionFactory;
    }

    @Bean(name = "firstRabbitTemplate")
    @Primary
    public RabbitTemplate firstRabbitTemplate(
            @Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory
    ) {
        RabbitTemplate firstRabbitTemplate = new RabbitTemplate(connectionFactory);
        return firstRabbitTemplate;
    }

    @Bean(name = "secondRabbitTemplate")
    public RabbitTemplate secondRabbitTemplate(
            @Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory
    ) {
        RabbitTemplate secondRabbitTemplate = new RabbitTemplate(connectionFactory);
        return secondRabbitTemplate;
    }


    @Bean(name = "firstFactory")
    public SimpleRabbitListenerContainerFactory firstFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer,
            @Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory
    ) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        return factory;
    }

    @Bean(name = "secondFactory")
    public SimpleRabbitListenerContainerFactory secondFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer,
            @Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory
    ) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        return factory;
    }
}
  • 信道构建器
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;

/**
 * 信道构建器
 *
 * @author Lenovo
 */
@Configuration
public class CreateQueue {

    @Bean
    public String chargeQueue(@Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory) {
        try {
            connectionFactory.createConnection().createChannel(false).queueDeclare("test.add", true, false, false, null);
        }catch (IOException e){
            e.printStackTrace();
        }
        return "test.add";
    }
}
  • 信道监听器
package com.ciih.authcenter.client.mq;

import com.ciih.authcenter.manager.entity.Permission;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

/**
 * 信道监听器
 *
 * @author Lenovo
 */
@Slf4j
@Component
public class ListeningHandle {

    public static final String ENCODING = "UTF-8";

    @RabbitHandler
    @RabbitListener(queues = {RabbitConfig.USERS_ADD}, containerFactory = "firstFactory")
    @SneakyThrows
    public void onMessageUserAdd(Message message, Channel channel) {
        log.info("[listenerManualAck 监听的消息userAdd] - [{}]", new String(message.getBody(), ENCODING));
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (
                IOException e) {
        }
    }

    @RabbitHandler
    @RabbitListener(queues = {RabbitConfig.USERS_UPDATE}, containerFactory = "firstFactory")
    @SneakyThrows
    public void onMessageUserUpdate(Message message, Channel channel) {
        log.info("[listenerManualAck 监听的消息userUpdate] - [{}]", new String(message.getBody(), ENCODING));
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (
                IOException e) {
        }
    }

    @RabbitHandler
    @RabbitListener(queues = {RabbitConfig.USERS_DELETE}, containerFactory = "firstFactory")
    @SneakyThrows
    public void onMessageUserDelete(Message message, Channel channel) {
        log.info("[listenerManualAck 监听的消息userDelete] - [{}]", new String(message.getBody(), ENCODING));
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (
                IOException e) {
        }
    }

    @RabbitHandler
    @RabbitListener(queues = {RabbitConfig.ORGS_ADD}, containerFactory = "firstFactory")
    @SneakyThrows
    public void onMessageOrgsAdd(Message message, Channel channel) {
        log.info("[listenerManualAck 监听的消息orgsAdd] - [{}]", new String(message.getBody(), ENCODING));
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (
                IOException e) {
        }
    }

    @RabbitHandler
    @RabbitListener(queues = {RabbitConfig.ORGS_UPDATE}, containerFactory = "firstFactory")
    @SneakyThrows
    public void onMessageOrgsUpdate(Message message, Channel channel) {
        log.info("[listenerManualAck 监听的消息orgsUpdate] - [{}]", new String(message.getBody(), ENCODING));
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (
                IOException e) {
        }
    }

    @RabbitHandler
    @RabbitListener(queues = {RabbitConfig.ORGS_DELETE}, containerFactory = "firstFactory")
    @SneakyThrows
    public void onMessageOrgsDelete(Message message, Channel channel) {
        log.info("[listenerManualAck 监听的消息orgsDelete] - [{}]", new String(message.getBody(), ENCODING));
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (
                IOException e) {
        }
    }

    @RabbitListener(queues = {"test.add"}, containerFactory = "secondFactory")
    @SneakyThrows
    public void hospitalAdd(List<Permission> permissions, Message message, Channel channel) {
        System.out.println(permissions);
    }
}
  • 发送消息
import com.ciih.authcenter.manager.entity.Permission;
import com.ciih.authcenter.manager.service.PermissionService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class Sender {

    @Resource
    PermissionService permissionService;

    @Resource(name = "secondRabbitTemplate")
    private RabbitTemplate secondRabbitTemplate;

    @GetMapping("test1")
    public void send1() {
        List<Permission> list = permissionService.lambdaQuery().last("limit 0, 10").list();
        this.secondRabbitTemplate.convertAndSend("test.add", list);
    }
}
  • 依赖
    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/186332.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 【编程语言】CentOS 7 下pip更新命令

    【编程语言】CentOS 7 下pip更新命令CentOS7下pip更新命令CentOS7需要更新pip时,只需要一下简单的命令即可搞定:pipinstall–upgradepip

  • Java面试题及答案2019版(上)

    1、面向对象的特征有哪些方面?答:面向对象的特征主要有以下几个方面:抽象:抽象是将一类对象的共同特征总结出来构造类的过程,包括数据抽象和行为抽象两方面。抽象只关注对象有哪些属性和行为,并不关注这些行为的细节是什么。 继承:继承是从已有类得到继承信息创建新类的过程。提供继承信息的类被称为父类(超类、基类);得到继承信息的类被称为子类(派生类)。继承让变化中的软件系统有了一定的延续性,同时继…

  • Apache和Nginx有什么区别

    Apache和Nginx有什么区别Apache和Nginx最核心的区别在于apache是同步多进程模型,一个连接对应一个进程;而nginx是异步的,多个连接(万级别)可以对应一个进程。区别:Apacheapache的rewrite比nginx强大,在rewrite频繁的情况下,用apacheapache模块多apache更为成熟,少bugapache超稳定apache对PHP支持比较交单,nginx需要配合其他后端用apche在处理动态请求有优势,nginx在这方面是鸡肋,一般动态请求用apache去做,nginx适合静态

  • dataGrip 2021.4.12 激活码【在线破解激活】

    dataGrip 2021.4.12 激活码【在线破解激活】,https://javaforall.cn/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

  • 二进制8进制10进制16进制代码_不同进制之间的转换

    二进制8进制10进制16进制代码_不同进制之间的转换为什么要使用进制数数据在计算机中的表示,最终以二进制的形式存在,就是各种&lt;黑客帝国&gt;电影中那些0101010…的数字;我们操作计算机,实际就是使用程序和软件在计算机上各种读写数据,如果我们直接操作二进制的话,面对这么长的数进行思考或操作,没有人会喜欢。C,C++语言没有提供在代码直接写二进制数的方法。用16进制或8进制可以…

  • shuffle model_什么是did模型

    shuffle model_什么是did模型原文链接::https://arxiv.org/abs/1707.01083Abstract论文提出了一种计算效率极高的卷积神经网络结构——ShuffleNet,它是专门为计算能力有限的移动平台设计的。这个新结构用来两个新操作——逐渐群卷积(pointwisegroupconvulution)和通道混洗(channelshuffle)在保障精确率损失不大的同时大大减少了计算成本。基于Im…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号