亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

Spring Boot 配置多源的 RabbitMQ

paulquei / 3677人閱讀

摘要:簡(jiǎn)介是開發(fā)中很平常的中間件,本文講述的是怎么在一個(gè)項(xiàng)目中配置多源的,這里不過多的講解的相關(guān)知識(shí)點(diǎn)。但是需要配置多個(gè)源時(shí),第二個(gè)及其以上的就需要多帶帶配置了,這里我使用的都是多帶帶配置的。源碼個(gè)人日拱一卒,不期速成

簡(jiǎn)介

MQ 是開發(fā)中很平常的中間件,本文講述的是怎么在一個(gè)Spring Boot項(xiàng)目中配置多源的RabbitMQ,這里不過多的講解RabbitMQ的相關(guān)知識(shí)點(diǎn)。如果你也有遇到需要往多個(gè)RabbitMQ中發(fā)送消息的需求,希望本文可以幫助到你。

環(huán)境

rabbitmq 3.7.12

spring boot 2.1.6.RELEASE

當(dāng)然軟件的版本不是硬性要求,只是我使用的環(huán)境而已,唯一的要求是需要啟動(dòng)兩個(gè)RabbitMQ,我這邊是在kubernetes集群中使用helm 官方提供的charts包快速啟動(dòng)的兩個(gè)rabbitmq-ha高可用rabbitmq集群。

想要了解 kubernetes或者helm,可以參看以下 github倉庫:

kubernetes : https://github.com/kubernetes...

helm: https://github.com/helm/helm

charts: https://github.com/helm/charts

SpringBoot中配置兩個(gè)RabbitMQ源

在springboot 中配置單個(gè)RabbitMQ是極其簡(jiǎn)單的,我們只需要使用Springboot為我們自動(dòng)裝配的RabbitMQ相關(guān)的配置就可以了。但是需要配置多個(gè)源時(shí),第二個(gè)及其以上的就需要多帶帶配置了,這里我使用的都是多帶帶配置的。

代碼:
/**
 * @author innerpeacez
 * @since 2019/3/11
 */
@Data
public abstract class AbstractRabbitConfiguration {

    protected String host;
    protected int port;
    protected String username;
    protected String password;

    protected ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        return connectionFactory;
    }
}

第一個(gè)源的配置代碼

package com.zhw.study.springbootmultirabbitmq.config;

import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * @author innerpeacez
 * @since 2019/3/8
 */

@Configuration
@ConfigurationProperties("spring.rabbitmq.first")
public class FirstRabbitConfiguration extends AbstractRabbitConfiguration {

    @Bean(name = "firstConnectionFactory")
    @Primary
    public ConnectionFactory firstConnectionFactory() {
        return super.connectionFactory();
    }

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

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

    @Bean(value = "firstRabbitAdmin")
    public RabbitAdmin firstRabbitAdmin(@Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory) {
        return new RabbitAdmin(connectionFactory);
    }
}

第二個(gè)源的配置代碼

package com.zhw.study.springbootmultirabbitmq.config;

import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author innerpeacez
 * @since 2019/3/8
 */

@Configuration
@ConfigurationProperties("spring.rabbitmq.second")
public class SecondRabbitConfiguration extends AbstractRabbitConfiguration {

    @Bean(name = "secondConnectionFactory")
    public ConnectionFactory secondConnectionFactory() {
        return super.connectionFactory();
    }

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

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

    @Bean(value = "secondRabbitAdmin")
    public RabbitAdmin secondRabbitAdmin(@Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory) {
        return new RabbitAdmin(connectionFactory);
    }
}

配置信息

spring:
  application:
    name: multi-rabbitmq
  rabbitmq:
    first:
      host: 192.168.10.76
      port: 30509
      username: admin
      password: 123456
    second:
      host: 192.168.10.76
      port: 31938
      username: admin
      password: 123456
測(cè)試

這樣我們的兩個(gè)RabbitMQ源就配置好了,接下來我們進(jìn)行測(cè)試使用,為了方便使用,我寫了一個(gè)MultiRabbitTemplate.class 方便我們使用不同的源。

/**
 * @author innerpeacez
 * @since 2019/3/8
 */
@Component
public abstract class MultiRabbitTemplate {

    @Autowired
    @Qualifier(value = "firstRabbitTemplate")
    public AmqpTemplate firstRabbitTemplate;

    @Autowired
    @Qualifier(value = "secondRabbitTemplate")
    public AmqpTemplate secondRabbitTemplate;
}

第一個(gè)消息發(fā)送者類 TestFirstSender.class

/**
 * @author innerpeacez
 * @since 2019/3/11
 */
@Component
@Slf4j
public class TestFirstSender extends MultiRabbitTemplate implements MessageSender {

    @Override
    public void send(Object msg) {
        log.info("rabbitmq1 , msg: {}", msg);
        firstRabbitTemplate.convertAndSend("rabbitmq1", msg);
    }

    public void rabbitmq1sender() {
        this.send("innerpeacez1");
    }
}

第二個(gè)消息發(fā)送者類 TestSecondSender.class

/**
 * @author innerpeacez
 * @since 2019/3/11
 */
@Component
@Slf4j
public class TestSecondSender extends MultiRabbitTemplate implements MessageSender {

    @Override
    public void send(Object msg) {
        log.info("rabbitmq2 , msg: {}", msg);
        secondRabbitTemplate.convertAndSend("rabbitmq2", msg);
    }

    public void rabbitmq2sender() {
        this.send("innerpeacez2");
    }
}

動(dòng)態(tài)創(chuàng)建Queue的消費(fèi)者

/**
 * @author innerpeacez
 * @since 2019/3/11
 */

@Slf4j
@Component
public class TestFirstConsumer implements MessageConsumer {

    @Override
    @RabbitListener(bindings = @QueueBinding(value = @Queue("rabbitmq1")
            , exchange = @Exchange("rabbitmq1")
            , key = "rabbitmq1")
            , containerFactory = "firstFactory")
    public void receive(Object obj) {
        log.info("rabbitmq1 , {}", obj);
    }

}
/**
 * @author innerpeacez
 * @since 2019/3/11
 */

@Slf4j
@Component
public class TestSecondConsumer implements MessageConsumer {

    @Override
    @RabbitListener(bindings = @QueueBinding(value = @Queue("rabbitmq2")
            , exchange = @Exchange("rabbitmq2")
            , key = "rabbitmq2")
            , containerFactory = "secondFactory")
    public void receive(Object obj) {
        log.info("rabbitmq2 , {}", obj);
    }

}

測(cè)試類

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class SpringBootMultiRabbitmqApplicationTests extends MultiRabbitTemplate {

    @Autowired
    private TestFirstSender firstSender;
    @Autowired
    private TestSecondSender secondSender;

    /**
     * 一百個(gè)線程向 First Rabbitmq 的 rabbitmq1 queue中發(fā)送一百條消息
     */
    @Test
    public void testFirstSender() {
        for (int i = 0; i < 100; i++) {
            new Thread(() ->
                    firstSender.rabbitmq1sender()
            ).start();
        }
        try {
            Thread.sleep(1000 * 10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 一百個(gè)線程向 Second Rabbitmq 的 rabbitmq2 queue中發(fā)送一百條消息
     */
    @Test
    public void testSecondSender() {
        for (int i = 0; i < 100; i++) {
            new Thread(() ->
                    secondSender.rabbitmq2sender()
            ).start();
        }
        try {
            Thread.sleep(1000 * 10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}

測(cè)試結(jié)果:

總結(jié)

這樣配置好之后我們就可向兩個(gè)RabbitMQ中發(fā)送消息啦。這里只配置了兩個(gè)源,當(dāng)然如果你需要更多的源,僅僅只需要配置*RabbitConfiguration.class就可以啦。本文沒有多說關(guān)于RabbitMQ的相關(guān)知識(shí),如果未使用過需要自己了解一下相關(guān)知識(shí)。

源碼:https://github.com/innerpeace...

Github: https://github.com/innerpeacez

個(gè)人Blog: https://ipzgo.top

日拱一卒,不期速成

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/75474.html

相關(guān)文章

  • Spring Boot RabbitMQ - 優(yōu)先級(jí)隊(duì)列

    摘要:官方鏡像倉庫地址本地運(yùn)行訪問可視化面板地址默認(rèn)賬號(hào)默認(rèn)密碼集成基本參數(shù)配置配置配置定義優(yōu)先級(jí)隊(duì)列定義交換器定義參考官方文檔應(yīng)用啟動(dòng)后,會(huì)自動(dòng)創(chuàng)建和,并相互綁定,優(yōu)先級(jí)隊(duì)列會(huì)有如圖所示標(biāo)識(shí)。 showImg(https://upload-images.jianshu.io/upload_images/3424642-6085f3f9e43c7a4c.png?imageMogr2/auto...

    jackwang 評(píng)論0 收藏0
  • 慕課網(wǎng)_《RabbitMQ消息中間件極速入門與實(shí)戰(zhàn)》學(xué)習(xí)總結(jié)

    摘要:慕課網(wǎng)消息中間件極速入門與實(shí)戰(zhàn)學(xué)習(xí)總結(jié)時(shí)間年月日星期三說明本文部分內(nèi)容均來自慕課網(wǎng)。 慕課網(wǎng)《RabbitMQ消息中間件極速入門與實(shí)戰(zhàn)》學(xué)習(xí)總結(jié) 時(shí)間:2018年09月05日星期三 說明:本文部分內(nèi)容均來自慕課網(wǎng)。@慕課網(wǎng):https://www.imooc.com 教學(xué)源碼:無 學(xué)習(xí)源碼:https://github.com/zccodere/s... 第一章:RabbitM...

    mykurisu 評(píng)論0 收藏0
  • Spring Cloud構(gòu)建微服務(wù)架構(gòu):消息驅(qū)動(dòng)微服務(wù)(入門)【Dalston版】

    摘要:它通過使用來連接消息代理中間件以實(shí)現(xiàn)消息事件驅(qū)動(dòng)的微服務(wù)應(yīng)用。該示例主要目標(biāo)是構(gòu)建一個(gè)基于的微服務(wù)應(yīng)用,這個(gè)微服務(wù)應(yīng)用將通過使用消息中間件來接收消息并將消息打印到日志中。下面我們通過編寫生產(chǎn)消息的單元測(cè)試用例來完善我們的入門內(nèi)容。 之前在寫Spring Boot基礎(chǔ)教程的時(shí)候?qū)戇^一篇《Spring Boot中使用RabbitMQ》。在該文中,我們通過簡(jiǎn)單的配置和注解就能實(shí)現(xiàn)向Rabbi...

    smallStone 評(píng)論0 收藏0
  • Spring Boot 參考指南(消息傳遞)

    摘要:還自動(dòng)配置發(fā)送和接收消息所需的基礎(chǔ)設(shè)施。支持是一個(gè)輕量級(jí)的可靠的可伸縮的可移植的消息代理,基于協(xié)議,使用通過協(xié)議進(jìn)行通信。 32. 消息傳遞 Spring框架為與消息傳遞系統(tǒng)集成提供了廣泛的支持,從使用JmsTemplate簡(jiǎn)化的JMS API到使用完整的基礎(chǔ)設(shè)施異步接收消息,Spring AMQP為高級(jí)消息隊(duì)列協(xié)議提供了類似的特性集。Spring Boot還為RabbitTempla...

    Doyle 評(píng)論0 收藏0
  • 一起來學(xué)SpringBoot | 第十二篇:初探RabbitMQ消息隊(duì)列

    摘要:用于控制活動(dòng)人數(shù),將超過此一定閥值的訂單直接丟棄。緩解短時(shí)間的高流量壓垮應(yīng)用。目前比較推薦的就是我們手動(dòng)然后將消費(fèi)錯(cuò)誤的消息轉(zhuǎn)移到其它的消息隊(duì)列中,做補(bǔ)償處理消費(fèi)者該方案是默認(rèn)的方式不太推薦。 SpringBoot 是為了簡(jiǎn)化 Spring 應(yīng)用的創(chuàng)建、運(yùn)行、調(diào)試、部署等一系列問題而誕生的產(chǎn)物,自動(dòng)裝配的特性讓我們可以更好的關(guān)注業(yè)務(wù)本身而不是外部的XML配置,我們只需遵循規(guī)范,引入相...

    Baoyuan 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<