springboot引入rabbit mq

spring boot 集成 rabbit mq

第一步引入springboot 依赖
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
第二步添加rabbitmq配置
spring:
  rabbitmq:
    host: 127.0.0.1
    port: 17004
    username: admin
    password: ******
    virtual-host: /nonauto
    exchange: EX_ZY_PROPOSALSTATUS
    routingKey: ZY_PROPOSALSTATUS
    queue: Q_ZY_PROPOSALSTATUS
第三步添加rabbitmq配置类
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.amqp.core.Queue;
/**
 * @ClassName RabbitConfig
 * @Description rabbit mq 配置类
 * @Author God丶Man
 * @Date 2022/3/19 11:13
 * @Version 1.0
 **/
@Configuration
public class RabbitConfig {

    @Value("${spring.rabbitmq.exchange}")
    private String exchange;
    @Value("${spring.rabbitmq.routingKey}")
    private String routingKey;
    @Value("${spring.rabbitmq.queue}")
    private String queue;


    @Bean
    public Queue proposalQueue() {
        return new Queue(queue, true);
    }


    @Bean
    public DirectExchange proposalExchange() {
        return new DirectExchange(exchange, true, false);
    }

    @Bean
    public Binding bindingDirect() {
        return BindingBuilder.bind(proposalQueue()).to(proposalExchange()).with(routingKey);
    }
}

第四步消息生产者消费者测试类
import cn.hutool.json.JSONUtil;
import com.dh.marketplatform.vehicle.BaseTest;
import org.junit.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName RabbitMqTest
 * @Description 消息生产者测试类
 * @Author God丶Man
 * @Date 2022/3/19 11:31
 * @Version 1.0
 **/
public class RabbitMqTest extends BaseTest {

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @Test
    public void testMq() {
        // 发送消息 商业险
        Map<String, Object> paramMap = getStringObjectMap();

        String reqStr = JSONUtil.toJsonStr(paramMap);
        System.out.println(reqStr);
        /**
         * Convert a Java object to an Amqp {@link Message} and send it to a specific exchange
         * with a specific routing key.
         *
         * @param exchange the name of the exchange
         * @param routingKey the routing key
         * @param message a message to send
         * @throws AmqpException if there is a problem
         */
        rabbitTemplate.convertAndSend("EX_ZY_PROPOSALSTATUS", "ZY_PROPOSALSTATUS", reqStr);
    }

    private Map<String, Object> getStringObjectMap() {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("proposalNo","110250020221230000003");
        paramMap.put("proposalStatus","0");
        paramMap.put("riskCode","1230");
        return paramMap;
    }

}
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName RabbitMq
 * @Description 消息消费者测试类
 * @Author God丶Man
 * @Date 2022/3/19 11:33
 * @Version 1.0
 **/
@Component
@Slf4j
public class RabbitMqConsumer {


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "${spring.rabbitmq.queue}", autoDelete = "false"),
            exchange = @Exchange(value = "${spring.rabbitmq.exchange}")
    ))
    public void messagerevice(String message) {
        log.info("========接受消息打印: " + message);
    }

}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值