1、springboot集成RabbitMq

1、springboot集成rabbitmq

1.1、springboot,rabbitmq引入pom.xml文件

    <properties>
        <spring-boot-dependencies.version>2.3.2.RELEASE</spring-boot-dependencies.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <!-- dependencies-pom start -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot-dependencies.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

1.2 加入spring配置文件

spring:
  application:
    name: springboot-rabbitmq
server:
  port: 8764

  rabbitmq:
    host: localhost
    password: guest
    port: 5672
    username: guest
    virtual-host: /
    # 确认方式
    listener:
      simple:
        # 手动ack
        acknowledge-mode: AUTO
        retry:
          enabled: true
          max-attempts: 5
          # 重试最大间隔时间
          max-interval: 10000
          # 重试初始间隔时间
          initial-interval: 2000
          # 间隔时间乘子,间隔时间*乘子=下一次的间隔时间,最大不能超过设置的最大间隔时间
          multiplier: 2

1.3 加入一些配置类

package example;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.support.converter.ContentTypeDelegatingMessageConverter;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.amqp.support.converter.SerializerMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author yinyuming
 * @date 2021-08-31 11:17
 */
@Configuration
public class RabbitConfig {

    /**
     * 一些配置详细看:org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration
     */
    /**
     * 配置rabbitmq的一些基础配置,目前可以统一序列化,默认走json序列化,支持一些其他方式的序列化数据
     * @return
     */
    @Bean
    public MessageConverter messageConverter() {
        Jackson2JsonMessageConverter messageConverter = new Jackson2JsonMessageConverter();
        ContentTypeDelegatingMessageConverter result = new ContentTypeDelegatingMessageConverter(messageConverter);
        result.addDelegate(MessageProperties.CONTENT_TYPE_JSON, messageConverter);
        result.addDelegate(MessageProperties.CONTENT_TYPE_SERIALIZED_OBJECT, new SerializerMessageConverter());
        return result;
    }


    @Bean
    public MessageRecoverer messageRecoverer() {
        return new MessageRecoverer() {
            public void recover(Message message, Throwable cause) {
                //根据@RabbitListener监听配置中ackMode=AUTO,进入该方法时,已经自动提交成功了
                //spring客户端达到失败重试最大次数后,进入该方法,用户重试失败恢复
                //1、可以进行回推到某些队列中,
                //2、可以存到库中,以待使用

                //RabbitMq会直接把类型加入到头部,key=__TypeId__,一旦发给其他处理类,会强制检查转换类型,如果不存在类型会报错额。
                System.out.println("失败到恢复=" + message);
            }
        };
    }
}

1.4 加入测试类

package example;

import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;

/**
 * @author donh
 */
@SpringBootApplication
@EnableScheduling
public class Application {

    @Resource
    private RabbitTemplate rabbitTemplate;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
        System.out.println("启动成功");
    }

    public static  int i  = 1;
    @Scheduled(cron = "0/1 * * * * ? ")
    public void scheduled() {
        rabbitTemplate.convertSendAndReceive("exchangeKey1","routingKey1","你好" + i++);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "queue1", durable = "true", autoDelete = "false"),
            exchange = @Exchange(value = "exchangeKey1"),
            key = "routingKey1"
    ), ackMode = "AUTO")
    public void classroomScoreConsume(String body) {
        System.out.println("body=" + body);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值