First, its dependencies
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.49</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency>
Second, add the configuration
spring: rabbitmq: host: 127.0.0.1 port: 5672 username: admins password: admins virtual-host: /test_rabbitMq
Third, create a queue and switches and bind
package com.liangjian.config; import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.FanoutExchange; import org.springframework.amqp.core.Queue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RabbitMQConfig {
// queue name
public static String FANOUT_SMS_QUEUE = " fanout_sms_queue " ;
// create a queue @Bean public Queue fanoutSmsQueue () { return new new Queue (FANOUT_SMS_QUEUE); }
// Create switch @Bean public FanoutExchange fanoutExchange1 () { return new new FanoutExchange ( " fanoutExchange1 " ); }
// switch queues and binding @Bean the Binding bindingSms () { return BindingBuilder.bind (fanoutSmsQueue ()) to (fanoutExchange1 ());. } }
Four, producer producer sends a message
package com.liangjian.producer; import com.liangjian.config.RabbitMQConfig; import com.liangjian.util.SmsUtil; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; import java.util.Map; @Component public class FanoutProducer { @Autowired private RabbitTemplate rabbitTemplate; public void send(String massage){ String msg = "Creator announced:" + Massage; rabbitTemplate.convertAndSend (RabbitMQConfig.FANOUT_SMS_QUEUE, MSG); } }
Five, consumer consumers get the message
package com.liangjian.consumer; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component @Slf4j public class FanoutSmsConsumer {
@RabbitListener(queues = "fanout_sms_queue")
@RabbitHandler
public void process(Message massage) throws UnsupportedEncodingException {
String id = massage.getMessageProperties().getMessageId();
String msg =new String( massage.getBody(),"UTF-8");
log.info(id+">>>>>>>>>>"+msg);
JSONObject jsonObject = JSONObject.parseObject(msg);
Integer filmID = jsonObject.getInteger("userID");
String nums = jsonObject.getString("phone");
log.info("filmID="+filmID+">>>>>>>>>>>>>>>>>>>>>>>> nums="+nums);
}
}
Six, controller calls the producers announced
package com.liangjian.controller; import com.alibaba.fastjson.JSONObject; import com.liangjian.producer.FanoutProducer; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @RequestMapping("/ticket") @Controller @Slf4j public class TicketController { @Autowired private FanoutProducer fanoutProducer; @GetMapping("/getTicket") @ResponseBody @Transactional(rollbackFor = Exception.class) public String getTicket(Integer userID,String phone){ JSONObject jsonObject=new JSONObject(); jsonObject.put("userID",userID)); jsonObject.put("phone",phone); fanoutProducer.sendTicketMsg(jsonObject.toJSONString()); return "Send Message success!" ; } }
Seven summary
For different types of switches, simply create the appropriate switch.
@Bean DirectExchange directExchange(){ return new DirectExchange("directExchange"); } @Bean Binding bindingSms() { return BindingBuilder.bind(directSmsQueue()).to(directExchange()).with("key.sms"); }
When sending messages, carrying routingKey
public void send(String queueName){ String msg="my_fanout_msg:"+ new Date(); System.out.println("生产者发布消息msg:"+msg); rabbitTemplate.convertAndSend(queueName,"key.sms",msg); }
Topic switch routingkey support wildcard match:
Routingkey String = "# test topic. ';
Routingkey String = "test topic *.";
- * To match only one word
- # Indicates a match more than one word