首页 > 编程语言 >java——spring boot集成RabbitMQ——高级特效——可靠性投递总结——topic——路由模式——代码汇总

java——spring boot集成RabbitMQ——高级特效——可靠性投递总结——topic——路由模式——代码汇总

时间:2023-02-26 16:47:48浏览次数:38  
标签:java amqp spring boot springframework org import public

可靠性在3个方面:

 

 

 

1、发送到到rmq——confirm机制;

 

2、交换机到队列——return机制;

 

3、队列到消费者——自动ack改为手动ack;

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

=========================================================================

 

 

 

 

 

 

 

生产者:

 

pom文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>springrmqtopicsender</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/>
    </parent>


    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

    </dependencies>

</project>

 

 

 

 

 

 

 

 

 

 

yml文件:

server:
  port: 8082
spring:
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: guest
    password: guest
    publisher-confirm-type: correlated
    publisher-returns: true

 

 

 

 

 

 

 

 

 

回调机制:

package org.example.config;

import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class MyConfirmCallback implements RabbitTemplate.ConfirmCallback,RabbitTemplate.ReturnsCallback {

    //把监听器注入到RabbitTemplate中
    @Autowired
    RabbitTemplate rabbitTemplate;


    @PostConstruct
    public void init()
    {
        rabbitTemplate.setConfirmCallback(this);
        rabbitTemplate.setReturnsCallback(this);

    }


    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {


        /**
         *
         * 生产者发送消息给交换机;交换机返回ack的同时,还会带一些消息云数据,云数据从correlationData中获得
         *
         * correlationData中包括:
         *
         *     private volatile String id;
         *
         *     private volatile ReturnedMessage returnedMessage;
         *
         *     ============================================================================
         *
         *     ack是个布尔值。如果生产者发送一个消息到交换机,交换机签收成功,返回true。否则返回false;
         *
         *     ================================================================================
         *
         *     cause:原因。ack返回false时,返回的具体原因
         *
         */

        String id = correlationData.getId();

        if(ack)
        {
            //消息投递成功
            System.out.println("消息投递成功:" + id);
        }
        else {
            //失败。存入到缓存中,通过定时任务,定时发送
            System.out.println("消息投递失败,原因" + cause);
        }

    }

    @Override
    public void returnedMessage(ReturnedMessage returned) {
        /**
         * 当消息没有传递到队列的时候的回调方法:
         *
         *    private final Message message;
         *
         *     private final int replyCode;
         *
         *     private final String replyText;
         *
         *     private final String exchange;
         *
         *     private final String routingKey;
         *
         */

        System.out.println("消息" + returned.getMessage() + "没有成功投递到队列");
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

定义交换机:

package org.example.config;

import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 主题交换机
 * topic策略可以根据routingKey的规则(通配符方式)进行去匹配队列进行转发规则为*.#.*
 */
@Configuration
public class RabbitTopicConfig
{


    public final static String TOPIC_NAME = "amqp-topic";


    @Bean
    TopicExchange topicExchange()
    {

        return new TopicExchange(TOPIC_NAME,true,false);

    }


}

 

 

 

 

 

 

 

 

定义发送消息组件:

package org.example.sender;

import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * 消息生产者 发送消息
 */
@Component
public class MessageSender {

    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 发送消息
     * @param info
     */
    public void send(String info)
    {

        System.out.println("发送消息>>>"+info);

        CorrelationData correlationData = new CorrelationData();

        String uuid = UUID.randomUUID().toString();
        System.out.println(uuid);

        correlationData.setId(uuid);

        rabbitTemplate.convertAndSend("amqp-topic","huawei.a",info,correlationData);
    }

}

 

 

 

 

 

 

 

 

 

 

 

定义发送消息控制器:

package org.example.controller;

import org.example.sender.MessageSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: [email protected]
 * @Date: 2020/10/4 11:34
 */
@RestController
public class IndexController {

    @Autowired
    MessageSender messageSender;

    @RequestMapping("/index")
    public String index()
    {

        messageSender.send("中国——路由——华为");
        return "SUCCESS";
    }

}

 

 

 

 

 

 

 

 

 

 

 

启动服务:

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloWorldApplication
{

    public static void main(String[] args)
    {

        SpringApplication.run(HelloWorldApplication.class, args);

    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

=====================================================================================

 

 

 

 

 

 

 

 

消费者:

 

 

pom文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>springrmqtopicreceiver</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/>
    </parent>


    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

    </dependencies>

</project>

 

 

 

 

 

 

 

 

yml文件:

server:
  port: 8081
spring:
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: guest
    password: guest
    #关闭自动ack,设置为手动ack
    listener:
      simple:
        acknowledge-mode: manual

 

 

 

 

 

 

 

 

 

 

 

配置文件:

package org.example.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 主题交换机
 * topic策略可以根据routingKey的规则(通配符方式)进行去匹配队列进行转发规则为*.#.*
 */
@Configuration
public class RabbitTopicConfig {

    public final static String TOPIC_NAME = "amqp-topic";

    @Bean
    TopicExchange topicExchange(){
        return new TopicExchange(TOPIC_NAME,true,false);
    }

    @Bean
    Queue xiaomi(){
        return new Queue("xiaomi",true);
    }

    @Bean
    Queue huawei(){
        return new Queue("huawei",true);
    }

    @Bean
    Binding xiaomiBinding(){
        //xiaomi.#:表示消息的routingKey是以xiaomi开头的就会路由到xiaomi的队列
        return BindingBuilder.bind(xiaomi()).to(topicExchange()).with("xiaomi.#");
    }

    @Bean
    Binding huaweiBinding(){
        return BindingBuilder.bind(huawei()).to(topicExchange()).with("huawei.#");
    }
}

 

 

 

 

 

 

接收消息:

package org.example.receiver;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.io.IOException;


@Component
public class TopicReceiver {

    //分别监听名称为xiaomi、huawei的队列
    @RabbitListener(queues = "xiaomi")
    public void handlerXM(Message message,String msg, Channel channel) throws IOException {

        System.out.println("小米:"+msg);

        //手动签收,不启动批量签收
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

        System.out.println(message.getMessageProperties().getDeliveryTag());
    }


    @RabbitListener(queues = "huawei")
    public void handlerHW(Message message,String msg, Channel channel) throws IOException {


        System.out.println("华为:"+msg);

        //手动签收,不启动批量签收
        //告诉rmq签收的消息的id。以及是否批量签收
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

    }
}

 

 

 

 

 

 

 

 

 

 

启动服务:

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloWorldApplication
{

    public static void main(String[] args)
    {

        SpringApplication.run(HelloWorldApplication.class, args);

    }
}

 

标签:java,amqp,spring,boot,springframework,org,import,public
From: https://www.cnblogs.com/xiaobaibailongma/p/17156952.html

相关文章

  • SpringMVC:异常处理器
    目录基于配置的异常处理基于注解的异常处理基于配置的异常处理SpringMVC提供了一个处理控制器方法执行过程中所出现的异常的接口:HandlerExceptionResolverHandlerExcept......
  • Spring MVC学习笔记
    1.为什么要学SPringMVC    SpringMVC是Spring框架中关于Web开发的一部分2.要在其中学习什么?    Web开发的请求、响应数据(最基本);Rest风格;SSM整合;拦截器......
  • SpringMVC:注解配置SpringMVC
    目录创建初始化类,代替web.xml创建SpringConfig配置类,代替spring的配置文件创建WebConfig配置类,代替SpringMVC的配置文件测试功能使用配置类和注解代替web.xml和SpringMVC......
  • spring security笔记一
    创建SecurityConfig类,加上@Configuration注解添加授权方法:/***访问路径授权**@paramhttp*@return*@throwsException*/@BeanpublicSecurityFilterChainfil......
  • SpringMVC:SpringMVC处理Ajax请求
    目录@RequestBody@RequestBody获取json格式的请求参数@ResponseBody@ResponseBody响应浏览器json数据@RestController注解@RequestBody@RequestBody可以获取请求体信息,使......
  • (半原创)java-内部类
    前言本章节使用JDK环境版本如下:C:\Users\chenjz20>java-versionjavaversion"1.8.0_192"Java(TM)SERuntimeEnvironment(build1.8.0_192-b12)JavaHotSpot(......
  • SpringMVC:文件上传和下载
    文件下载ResponseEntity用于控制器方法的返回值类型,该控制器方法的返回值就是响应到浏览器的响应报文使用ResponseEntity实现下载文件的功能@RequestMapping("/testDown......
  • Java流程控制:用户交互Scanner、选择结构
    Java流程控制:用户交互Scanner、选择结构用户交互ScannerScanner类用于获取用户的输入基本语法:Scanners=newScanner(System.in);s.close();packagecom.qiu.first.......
  • 使用骨架创建maven的java工程 不使用骨架创建maven的java工程
    使用骨架创建maven的java工程         不使用骨架创建maven工程新建项目       ......
  • 【springboot】约定优于配置
    spring的核心思想:约定优于配置 @SpringBootApplication这个注解的本质是有以下三个注解1.@SpringBootConfiguration表示该类是一个配置类2.@EnableAutoCon......