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>

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

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Prism初研究之Bootstrapper

    Prism初研究之BootstrapperPrism初研究之初始化应用Prism初研究之初始化应用BootstrapperDIShell关键抉择核心步骤创建Bootstrapper实现CreateShell方法实现InitializeShell方法创建并配置ModuleCatalog创建并配置Container核心服务(与应用无关)与应用相关的服务(StockTraderRI)在UnityBootstrapper中创建并配置…

    2022年7月20日
    17
  • Spring Boot 项目最优雅的 HTTP 客户端工具,用它就够了,太香了!

    大家都知道okhttp是一款由square公司开源的java版本http客户端工具。实际上,square公司还开源了基于okhttp进一步封装的retrofit工具,用来支持通过接…

    2021年6月22日
    131
  • @Autowire和@Resource注解使用的正确姿势,别再用错的了!!

    点击上方“全栈程序员社区”,星标公众号 重磅干货,第一时间送达 作者:liuxuzxx juejin.cn/post/6844904064212271117 介绍 今天使用Idea…

    2021年6月27日
    88
  • 顺丰科技QT面试题「建议收藏」

    顺丰科技QT面试题「建议收藏」自定义控件:应该做过吧?能举几个例子吗?还有其他的吗?你觉得自定义控件的方法主要是哪些?答:从外观设计上:QSS、继承绘制函数重绘、继承QStyle相关类重绘、组合拼装等等从功能行为上:重写事件函数、添加或者修改信号和槽等等QSS:QSS平时使用的多吗?能举几个例子吗?都是如何使用,能说说吗?答:1.将QSS统一写在一个文件中,通过程序给主窗口加载;2.写成一个字符串中,通过程序给主窗口加载;3.需要使用的地方,写一个字符串,加载给对象;4.QTDesigner中填写;事件机制:

    2022年6月25日
    33
  • 云服务是免费的吗_云服务器收费

    云服务是免费的吗_云服务器收费近年来,云服务器的普及率快速上升,相当一部分企业从传统服务器转向云服务器,而随着市场的发展,云服务器供应商尤其多,服务器供应商竞争日趋激烈。此时不少服务商表示自己推出永久免费使用的云服务器,面对这样的消息不少企业会感到疑惑,永久免费使用的云服务器究竟是否可信?那么下面就由摩杜云小杜和大家讲一讲有没有永久免费的云服务器。一、首先市场上根本就没有所谓的永久免费使用的云服务器虽然现如今云技术发展快速,但是云资源的成本还是很高的,所以商家为了自己获益,不可能会提供免费的云主机租用服务。但是目前市场上有服务商提供

    2022年10月6日
    7
  • 不允许对虚拟列执行 UPDATE 操作

    不允许对虚拟列执行 UPDATE 操作

    2020年11月9日
    573

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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