docker 安装 rabbit

1、下载镜像

#官网镜像地址 :

https://hub.docker.com/_/rabbitmq?tab=tags&page=1&ordering=last_updated

#下载镜像

docker pull rabbitmq:management

 

2、安装

#运行镜像

docker run --name myrabbit -d -p 5672:5672 -p 15672:15672 80070f1f0c8f

Q1:

 重启系统

3、开发端口号

1-1、查看端口:firewall-cmd --list-all

1-2、开启端口:firewall-cmd --zone=public --add-port=15672/tcp --permanent

1-3、重启防火墙:systemctl restart firewalld

4、网页客户端

浏览器输入:http://192.168.2.170:15672/

账号:guest

密码:guest

4、网页客户端测试

#创建交换机

#创建队列

#交换机绑定队列

#发送消息

#接收消息

(以上所有操作  见下图)

 5、整合至springboot

 


import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class RbbitConfig {
    /**
     * 用于序列化格式
     */
    @Bean
     public MessageConverter  messageConverter(){
        return new Jackson2JsonMessageConverter();
    }
}

import org.junit.jupiter.api.Test;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;

@SpringBootTest
class SpringbootRabbitApplicationTests {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private AmqpAdmin amqpAdmin;

    /**
     * 发送消息
     */
    @Test
    void sendMessage() {
        HashMap<Object, Object> msg = new HashMap<>();
        msg.put("code","200");
        msg.put("msg","你好啊");
        rabbitTemplate.convertAndSend("smExchanges1","queue1",msg);
    }

    /**
     * 接收消息
     */
    @Test
    void receviceMessage() {
        Object queue1 = rabbitTemplate.receiveAndConvert("queue1");
        System.out.println(queue1.getClass());
        System.out.println(queue1);
    }

    /**
     * 创建交换器
     */
    @Test
    void CreateExchanage(){
        amqpAdmin.declareExchange(new DirectExchange("bootCreate"));
    }
    /**
     * 创建队列
     */
    @Test
    void CreateQueue(){
        amqpAdmin.declareQueue(new Queue("bootCreateQueue",true));
    }
    /**
     * 交换器绑定队列
     */
    @Test
    void CreateBinding(){
        amqpAdmin.declareBinding(new Binding("bootCreateQueue", Binding.DestinationType.QUEUE,"bootCreate","bootCreateQueue",null));
    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值