首页  ·  知识 ·  编程语言
在springboot项目中使用RabbitMq做消息队列
网友  CSDN  JAVA  编辑:Amanda   图片来源:网络
面向消息的程序还有一个特点就是你关心的是完成任务,但并不是实时完成的,无须应答请求。而当我们完成任务的动作无法跟上请求的速度时,我们还可以利用自动轮询的模式,把MQ充当负载均衡器来使

第一步:在spring boot项目中添加RabbitMq的maven依赖

 <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-amqp</artifactId>

 </dependency>


第二步:在配置文件中添加RabbitMq的配置

#配置RabbitMQ

spring.rabbitmq.host=192.168.1.103

spring.rabbitmq.port=5672

spring.rabbitmq.username=guest

spring.rabbitmq.password=guest

spring.rabbitmq.publisherConfirms=true


第三步:添加RabbitMq配置类

import org.springframework.amqp.core.Queue;

import org.springframework.beans.factory.annotation.Configurable;

import org.springframework.context.annotation.Bean;

 

@Configurable

public class RabbitMqConfig {

 

    @Bean

    public Queue countPalletBatchQueue(){

        return new Queue("count-pallet-batch-queue");    

    };

}


第四步:添加生产者类

@Component

public class producter {

 

   

 

    @Autowired

    private AmqpTemplate amqpTemplate;

 

 

    void create() {

        String message = "这是一条消息";

        amqpTemplate.convertAndSend("count-pallet-batch-queue",message);

    }

 

     

}



第五步:添加消费者类

@Component

@Service

@Transactional

public class  customer {

 

 

 

    @Autowired

    private AmqpTemplate amqpTemplate;

 

 

 

    @RabbitListener(queues = "count-pallet-batch-queue")

    public String custom(String message){

        return message;

    }

 

 

}


本文作者:网友 来源:CSDN
CIO之家 www.ciozj.com 微信公众号:imciow
    >>频道首页  >>网站首页   纠错  >>投诉
版权声明:CIO之家尊重行业规范,每篇文章都注明有明确的作者和来源;CIO之家的原创文章,请转载时务必注明文章作者和来源;
延伸阅读
也许感兴趣的
我们推荐的
主题最新
看看其它的