配置依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
配置参数
spring.rabbitmq.host= 127.0.0.1
spring.rabbitmq.password = guest
spring.rabbitmq.port = 5672
spring.rabbitmq.username = guest
消息队列
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @ClassName: Queues
* @Description: Queues
* @Author: xuezhouyi
* @Version: V1.0
**/
@Configuration
public class Queues {
/**
* 下单减库存
*/
public static final String CP_ORDER_OPS = "cp:order:ops";
/**
* 发放权益
*/
public static final String CP_RIGHTS_OPS = "cp:rights:ops";
@Bean
public Queue orderOps() {
return new Queue(CP_ORDER_OPS);
}
@Bean
public Queue rightsOps() {
return new Queue(CP_RIGHTS_OPS);
}
}
生产者
import com.icitic.mc.seckill.service.config.Queues;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @ClassName: Producer
* @Description: Producer
* @Author: xuezhouyi
* @Version: V1.0
**/
@Component
public class Producer {
@Autowired
private AmqpTemplate template;
public void orderMQ() {
template.convertAndSend(Queues.CP_ORDER_OPS, "下单");
System.out.println(">>>下单消息发送成功!");
}
public void rightsMQ() {
template.convertAndSend(Queues.CP_RIGHTS_OPS, "权益");
System.out.println(">>>权益消息发送成功!");
}
}
消费者
import com.icitic.mc.seckill.service.config.Queues;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* @ClassName: Consumer
* @Description: Consumer
* @Author: xuezhouyi
* @Version: V1.0
**/
@Component
public class Consumer {
@RabbitListener(queues = Queues.CP_ORDER_OPS)
public void consumerOrder(String str) throws InterruptedException {
Thread.sleep(1000);
System.out.println(">>>下单收到消息:" + str);
}
@RabbitListener(queues = Queues.CP_RIGHTS_OPS)
public void consumerRights(String str) {
System.out.println(">>>权益收到消息:" + str);
}
}
测试
import com.icitic.mc.seckill.service.mq.Producer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @ClassName: SecKillServiceApplicationTests
* @Description: SecKillServiceApplicationTests
* @Author: xuezhouyi
* @Version: V1.0
**/
@SpringBootTest
@RunWith(SpringRunner.class)
public class SecKillServiceApplicationTests {
@Autowired
public Producer producer;
@Test
public void test() throws InterruptedException {
producer.orderMQ();
Thread.sleep(1000);
producer.rightsMQ();
}
}
结果
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.9.RELEASE)20:20:16.345 [main] INFO {org.springframework.boot.StartupInfoLogger: logStarted} com.icitic.mc.seckill.service.service.SecKillServiceApplicationTests : Started SecKillServiceApplicationTests in 19.715 seconds (JVM running for 21.842)
>>>下单消息发送成功!
>>>权益消息发送成功!
20:20:16.903 [Thread-8] INFO {org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer: doShutdown} org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer : Waiting for workers to finish.
>>>权益收到消息:权益
20:20:16.961 [Thread-8] INFO {org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer: doShutdown} org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer : Successfully waited for workers to finish.
20:20:17.005 [Thread-8] INFO {org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer: doShutdown} org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer : Waiting for workers to finish.
>>>下单收到消息:下单
20:20:17.965 [Thread-8] INFO {org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer: doShutdown} org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer : Successfully waited for workers to finish.