一、簡介:
二、案例:
1、引入依賴
<!-- rabbitmq依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <!--fastJson--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.49</version> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
2、編寫RabbitMQ配置屬性:
# rabbitMQ配置 spring.rabbitmq.host=127.0.0.1 spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest spring.rabbitmq.virtual-host=/ spring.rabbitmq.listener.simple.concurrency= 10 spring.rabbitmq.listener.simple.max-concurrency= 10 spring.rabbitmq.listener.simple.prefetch= 1 spring.rabbitmq.listener.simple.auto-startup=true spring.rabbitmq.listener.simple.default-requeue-rejected= true spring.rabbitmq.template.retry.enabled=true spring.rabbitmq.template.retry.max-attempts=3 spring.rabbitmq.template.retry.multiplier=1.0
3、編寫RabbitMQ配置類:
import org.springframework.amqp.core.Queue; import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; import org.springframework.amqp.support.converter.MessageConverter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MQConfig { // 隊列名稱 public static final String QUEUE = "queue"; /* 在springboot啟動的時候創建一個隊列 */ @Bean public Queue queue() { return new Queue(QUEUE, true); } /* 消息轉換器:將消息轉換為JSON格式 */ @Bean public MessageConverter getMessageConverter() { return new Jackson2JsonMessageConverter(); } }
4、編寫消息類:該消息類是生產者發送給消息隊列的參數
@Data public class CustomMessage { private int id; private String name; }
5、編寫控制層:
@Autowrite public MQSender mQSender; @RequestMapping("/test") public String test(){ CustomMessage customMessage = new CustomMessage(); customMessage.setId(1); customMessage.setName("miss"); mQSender.send(customMessage); return "ok"; }
6、編寫生產者:生產者將java對象轉換為json字符串
import com.alibaba.fastjson.JSON; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service @Slf4j public class MQSender { @Autowired private RabbitTemplate rabbitTemplate; public void send(CustomMessage customMessage){ String json = JSON.toJSONString(customMessage); log.info("send:"+json); rabbitTemplate.convertAndSend(MQConfig.QUEUE,json); } }
7、編寫消費者:消費者監聽“QUEUE”隊列是否有新數據,如果有則獲取,並將json字符串轉換為java對象
import com.alibaba.fastjson.JSON; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @Service @Slf4j public class MQReceiver { @RabbitListener(queues=MQConfig.QUEUE) //監聽 public void receive(String json){ CustomMessage customMessage = JSON.toJavaObject(JSON.parseObject(json), CustomMessage.class); system.out.println(customMessage); } }