springBoot中rabbitMq配置
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=root
spring.rabbitmq.password=123456
spring.rabbitmq.virtual-host=/
#\u5065\u5EB7\u76D1\u63A7 -\u7ED3\u675F
#开启消息发送确认
spring.rabbitmq.publisher-confirms=true
spring.rabbitmq.publisher-returns=true
#返回无法插入队列的消息
spring.rabbitmq.template.mandatory=true
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
一.导入rabbitMq依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
二.定义一个队列并把该队列绑定在交换机上
package com.medicine.management_side.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 FanoutRabbitConfig {
/**
* 定义队列c,服务于挂号接口
* @return
*/
@Bean
public Queue cMessage() {
return new Queue("queue_regist");
}
/**
* 定义交换机
* @return
*/
@Bean
FanoutExchange fanoutExchanges() {
return new FanoutExchange("queueRegistExchange");
}
/**
* 将队列和交换机进行绑定
* @return
*/
@Bean
Binding bindingExchangeC() {
return BindingBuilder.bind(cMessage()).to(fanoutExchanges());
}
}
三.定义生产者
package com.medicine.management_side.websocket;
import com.medicine.management_side.base.BaseController;
import com.medicine.management_side.utils.JsonUtil;
import com.medicine.management_side.utils.PageData;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class MsgSenderFanout extends BaseController {
@Autowired
private AmqpTemplate rabbitTemplate;
/**
* 接收消息,并将消息传到mq交换机上,绑定了该交换机的队列的消费者都可收到该消息
* @param message
*/
public void sendRegist(PageData pageData) {
String jsonstr=JsonUtil.readObject2Json(pageData);
System.out.println("jsonstr : " + jsonstr);
this.rabbitTemplate.convertAndSend("queueRegistExchange","queue_regist",jsonstr);
}
}
四.定义消费者
package com.medicine.management_side.mobileController;
import com.medicine.management_side.base.BaseController;
import com.medicine.management_side.service.PatientService;
import com.medicine.management_side.utils.JsonUtil;
import com.medicine.management_side.utils.PageData;
import com.medicine.management_side.utils.RedisUtil;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@Component
@RabbitListener(queues = "queue_regist")
public class MobileMqController extends BaseController {
@Autowired
private PatientService patientService;
@Resource
private RedisUtil redisUtil;
@RabbitHandler
public void process(String jsonstr) {
try {
System.out.println("消费 : " +jsonstr);
PageData pageData=(PageData) JsonUtil.readJson2Object(jsonstr,PageData.class);
char c = (char) ('A' + Math.random() * ('Z' - 'A' + 1));
String s = Character.toString(c);//产生的随机字符
pageData.put("random", s);
String registNubmer=pageData.get("registNubmer").toString();
pageData.put("registNubmer", registNubmer.replace("_",s));
patientService.newRegisterNum(pageData);//插入挂号表
PageData patientData = new PageData();
String id =pageData.get("id").toString();
patientData.put("uuid",id);
if("1.0".equals(String.valueOf(pageData.get("isPayment")))){//1是已支付
PageData datas = patientService.patientRegister(patientData);//挂号基本信息数据
List<Map<String, Object>> paImg = new LinkedList<Map<String, Object>>();//存放挂号图片用
datas.put("list", paImg);
datas.put("doctorId", "");//医生id
datas.put("subscribeTime", pageData.get("subscribeTime").toString());//问诊时间
//把对象转成json字符串
String jsonData = JsonUtil.readObject2Json(datas);
//key是诊室id item是挂号id value是挂号信息的json字符串放到redis
boolean isSaves = redisUtil.hset(pageData.get("consultationId").toString(), id, jsonData);//目前这里面存放的是预约和今日挂号
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
RabbitMq应用
最新推荐文章于 2024-02-22 11:13:32 发布