1.导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2.加配置(提前在你的linux服务器里引入rabbitmq:
docker run -d --name rabbitmq -p 5671:5671 -p 5672:5672 -p 4369:4369 -p 25672:25672 -p 15671:15671 -p 15672:15672 rabbitmq:management)
spring.rabbitmq.host=192.168.56.10
spring.rabbitmq.port=5672
spring.rabbitmq.virtual-host=/
3.主启动类加上注解:@EnableRabbit
4.首先测试创建exchange
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class GulimallOrderApplicationTests {
@Autowired
AmqpAdmin amqpAdmin;
/**
* 1、如何创建Exchange、Queue、Binding
* 1)、使用AmqpAdmin进行创建
* 2、如何收发消息
*/
@Test
public void createExchange() {
//参数:名字,是否持久化,是否自动删除
DirectExchange directExchange = new DirectExchange("hello-java-exchange",
true,false);
amqpAdmin.declareExchange(directExchange);
log.info("exchange[{}]创建成功","hello-java-exchange");
}
}
输入URL(你的rabbitmq的地址):http://192.168.56.10:15672/
登录后可以看到确实创建成功(登录账号密码默认都为guest):