秋风阁-北溪入江流
version: '3.8'
services:
rabbitmq:
image: rabbitmq:3.11.19
container_name: rabbitmq
hostname: rabbitmq
ports:
# amqp协议通讯端口(对外服务必开)
- 5672:5672
# RabbitMq自带管理界面访问端口
- 15672:15672
environment:
- RABBITMQ_DEFAULT_VHOST=${vhost}
- RABBITMQ_DEFAULT_USER=${user}
- RABBITMQ_DEFAULT_PASS=${password}
RabbitMq自带有专门的管理界面,可以在其管理界面对RabbitMq进行管理查看等操作。
RabbitMq的管理界面的对外端口为15672
,当我们启动RabbitMq后,需要启动管理界面插件后才能访问界面。
# 进入容器内部
docker exec -it rabbitmq bash
# 启动插件,启动管理界面
rabbitmq-plugins enable rabbitmq_management
<dependency>
<groupId>com.rabbitmqgroupId>
<artifactId>amqp-clientartifactId>
<version>5.18.0version>
dependency>
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import org.junit.Before;
import org.junit.After;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class TestMessageQueue {
private Connection connection;
private Channel channel;
@Before
public void before() throws IOException, TimeoutException{
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setUsername(${userName});
connectionFactory.setPassword(${password});
connectionFactory.setHost(${host});
connectionFactory.setPort(${port});
connectionFactory.setVirtualHost(${virtualHost});
// 获取RabbitMq连接
this.connection = connectionFactory.newConnection();
}
@After
public void after() throws IOException, TimeoutException {
// 关闭RabbitMq相关连接
this.connection.close();
}
}
import java.io.IOException;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeoutException;
import org.junit.After;
import org.junit.Before;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class TestMessageQueue {
private Connection connection;
private Channel channel;
@Before
public void before()
throws IOException, TimeoutException, URISyntaxException, NoSuchAlgorithmException, KeyManagementException {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setUri("amqp://root:[email protected]:9572/TieTa");
this.connection = connectionFactory.newConnection();
this.channel = this.connection.createChannel();
}
@After
public void after() throws IOException, TimeoutException {
this.channel.close();
this.connection.close();
}
}
@Test
public void producer() throws IOException{
// 声明(创建)队列
this.channel.queueDeclarePassive(${queueName});
// 发送数据
this.channel.basicPublish("", ${queueName}, null, "你好 World!".getBytes());
}
@Test
public void consumer() throws IOException{
// 声明(创建)队列
this.channel.queueDeclarePassive(${queueName});
// 定义消费者
DefaultConsumer consumer = new DefaultConsumer(channel) {
@Override
// 回调函数
public void handleDelivery(String consumerTag,
Envelope envelope,
AMQP.BasicProperties properties,
byte[] body) throws IOException {
String msg = new String(body, "UTF-8");
System.out.println(msg);
}
};
/**
* 获取消息
* queue: 消息队列名称
* autoAck: 自动应答机制
* callback: 回调处理类
*/
channel.basicConsume(${queueName}, true, consumer);
}
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-amqpartifactId>
<version>3.1.1version>
dependency>
在spring-boot-starter-amqp
库中,是通过依赖上面amqd-client
库来获取对RabbitMq的支持
在SpringBoot的配置文件application.yaml
中添加如下配置
spring:
rabbitmq:
host: ${host}
port: ${port}
username: ${userName}
password: ${password}
virtualHost: ${virtualHost}
@Component
public class MessageQueue {
private RabbitTemplate rabbitTemplate;
@Autowired
public MessageQueue(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
public void producer() {
rabbitTemplate.convertAndSend(${queueName}, ${message});
}
}
@Component
public class MessageQueue {
@RabbitHandler
@RabbitListener(queuesToDeclare = @Queue(${queueName}))
public void consumer(String msg) {
System.out.println(msg);
}
}