美文网首页
rabbitmq笔记

rabbitmq笔记

作者: 兮兮码字的地方 | 来源:发表于2021-07-07 17:38 被阅读0次

1.安装rabbitmq

推荐使用docker安装rabbitmq, 简单快速。

######安装docker

#下载docker-ce的yum源

(yum -y install wget #安装wget)

wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo

#把docker-ce源的地址修改为清华源的地址

sed -i 's#download.docker.com#mirrors.tuna.tsinghua.edu.cn/docker-ce#g' /etc/yum.repos.d/docker-ce.repo

#更新docker-ce.repo

yum makecache fast

#安装docker-ce

yum install -y docker-ce

#启动

systemctl start docker

#检查是否安装成功

ps -ef |grep docker

docker version

######docker 安装 rabbitmq

#下载镜像

docker pull rabbitmq:3.8.12-management-alpine

#查看所有镜像

docker images

#启动Rabbit MQ容器

docker run -d --hostname rabbit_host1 --name rabbit -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=admin -p 15672:15672 -p 5672:5672 rabbitmq:3.8.12-management-alpine

#管理页面

http://127.0.0.1:15672

2.推送消息给rabbitmq

###springboot集成rabbitmq

#引入依赖

<dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-amqp</artifactId>

</dependency>

#application.properties

server.servlet.context-path=/event

spring.application.name=event-send

server.port = 8090

spring.rabbitmq.addresses=7.211.2.3:5672,7.211.2.4:5672,7.211.2.5:5672

spring.rabbitmq.port=5672

spring.rabbitmq.username=admin

spring.rabbitmq.password=123456

spring.rabbitmq.virtual-host=cmp

spring.rabbitmq.dynamic=true

#配置类RabbitConfig

package com.enmotech.event.send;

import org.springframework.amqp.core.Queue;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

@Configuration

public class RabbitConfig {

    @Bean

    public Queue Queue() {

        return new Queue("bocloud.dbm.alarm.queue");

    }

}

#生产者类(发送)SendToRabbitMq

package com.enmotech.event.send;

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;

import lombok.extern.slf4j.Slf4j;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.amqp.core.Message;

import org.springframework.amqp.core.MessageBuilder;

import org.springframework.amqp.rabbit.core.RabbitTemplate;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

/**

* @date :2021/6/24 16:50

*/

@Slf4j

@RestController

@RequestMapping("/mq")

public class SendToRabbitMq {

    private static final Logger LOGGER = LoggerFactory.getLogger(SendToRabbitMq.class);

    private final static String QUEUE_NAME = "bocloud.dbm.alarm.queue";

    private final static String KEY="eventStatus";

    @Autowired

    private RabbitTemplate rabbitTemplate;

    @GetMapping("/test")

    public String test() {

        LOGGER.info("test...");

        return "success!";

    }

    @PostMapping("/send")

    public String send(@RequestBody String msg) {

        LOGGER.info("ready to send,msg={}",msg);

        JSONObject jsonObject = JSON.parseObject(msg);

        String message = JSON.toJSONString(jsonObject);

        Message m  = MessageBuilder.withBody(message.getBytes())

                .setContentType("text/json")

                .setContentEncoding("UTF-8")

//                .setMessageId(UUID.randomUUID() + "")

                .build();

        rabbitTemplate.convertAndSend(QUEUE_NAME,m);

        LOGGER.info("send complete!");

        return "send success";

    }

}

相关文章

网友评论

      本文标题:rabbitmq笔记

      本文链接:https://www.haomeiwen.com/subject/dxqaultx.html