环境:windows
安装erlang和rabbitmq的过程这里不多说,记得两个的版本要对应。
1.启动rabbitmq:
进入sbin目录:
其中可能报错:
则需要终止已经开始的进程(可能是之前启动过)
tasklist|find /i “erl”
erlsrv.exe 4676 Services 0 716 K
erl.exe 5916 Services 0 21,328 K
erl.exe 29252 Console 1 23,428 K
C:\Users\14172>taskkill /pid “4676” -f
SUCCESS: The process with PID 4676 has been terminated.
C:\Users\14172>taskkill /pid “29252” -f
SUCCESS: The process with PID 29252 has been terminated.
C:\Users\14172>taskkill /pid “5916” -f
SUCCESS: The process with PID 5916 has been terminated.
再次输入
rabbitmq-server.bat start
2.在springboot中使用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
配置:
spring:
rabbitmq:
addresses: 127.0.0.1
port: 5672
username: guest
password: guest
代码:
public interface IOrderService {
ResponseVo<OrderVo> create(Integer uid, Integer shippingId);
ResponseVo<PageInfo> list(Integer uid, Integer pageNum, Integer pageSize);
ResponseVo<OrderVo> detail(Integer uid, Long orderNo);
ResponseVo cancel(Integer uid, Long orderNo);
void paid(Long orderNo);
}
@Component
@RabbitListener(queues = "payNotify")
@Slf4j
public class PayMsgListener {
@Autowired
private IOrderService orderService;
@RabbitHandler
public void process(String msg) {
log.info("【接收到消息】=> {}", msg);
PayInfo payInfo = new Gson().fromJson(msg, PayInfo.class);
if (payInfo.getPlatformStatus().equals("SUCCESS")) {
//修改订单里的状态
orderService.paid(payInfo.getOrderNo());
}
}
}