新闻资讯

新闻资讯 通知公告

【SpringBoot MQ系列教程】RabbitMq初体验

编辑:016     时间:2020-02-15

I.环境准备

在测试之前,需要安装rabbitmq,下面分别指定mac + centos的安装教程


1. mac安装

安装命令

brew install rabbitmq ## 进入安装目录 cd /usr/local/Cellar/rabbitmq/3.7.5 # 启动 brew services start rabbitmq # 当前窗口启动 rabbitmq-server 复制代码

启动控制台之前需要先开启插件

./rabbitmq-plugins enable rabbitmq_management 复制代码

进入控制台:http:// localhost:15672 /

用户名和密码:guest,guest

2. centos安装

安装命令

yum install erlang
wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.15/rabbitmq-server-3.6.15-1.el6.noarch.rpm
yum install rabbitmq-server-3.6.15-1.el6.noarch.rpm 复制代码

插件开启

rabbitmq-plugins enable rabbitmq_management # 启动 rabbitmq-server -detached 复制代码

3.配置

添加账号,设置权限

## 添加账号 ./rabbitmqctl add_user admin admin ## 添加访问权限 ./rabbitmqctl set_permissions -p "/" admin ".*" ".*" ".*" ## 设置超级权限 ./rabbitmqctl set_user_tags admin administrator 复制代码

4.项目环境

接下我们创建一个SpringBoot项目,用于简单的体验一下Rabbitmq的发布和消费消息

  • springboot版本为2.2.1.RELEASE
  • rabbitmq版本为 3.7.5

依赖配置文件pom.xml

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </pluginManagement> </build> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/libs-snapshot-local</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone-local</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>spring-releases</id> <name>Spring Releases</name> <url>https://repo.spring.io/libs-release-local</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> 复制代码

application.yml配置文件中,添加rabbitmq的相关属性

spring:  rabbitmq:  virtual-host: /  username: admin  password: admin  port: 5672 复制代码

二。实例演示

接下来我们看一个hello world版本的Rabbitmq的使用姿势,一个简单发布消息,消费消息

1.发布消息

消息发布,我们主要采取AmqpTemplate来实现

@Component public class PublishDemo { @Autowired private AmqpTemplate amqpTemplate; public String publish2mq(String ans) {
        String msg = "hello world = " + ans;
        System.out.println("publish: " + msg);
        amqpTemplate.convertAndSend(Pkg.exchange, Pkg.routing, msg); return msg;
    }
} 复制代码

上面的case中,主要方法在于amqpTemplate#convertAndSend,第一个参数为exchangeName,第二个为routingKey

恒定配置如下

class Pkg { final static String exchange = "topic.e"; final static String routing = "r"; final static String queue = "topic.a";
} 复制代码

2.消费消息

消费消息,需要指定队列,通过routingKey绑定绑定交换,如下

@Service public class ConsumerDemo { @RabbitListener(bindings = @QueueBinding(value = @Queue(value = Pkg.queue, durable = "false", autoDelete = "true"),
            exchange = @Exchange(value = Pkg.exchange, ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC), key = Pkg.routing)) public void consumer(String msg) {
        System.out.println("consumer msg: " + msg);
    }
} 复制代码

3.测试演示

写一个简单的rest接口,用于接收参数,发布消息到mq,并被ConsumerDemo消费

@RestController public class PubRest { @Autowired private PublishDemo publishDemo; @GetMapping(path = {"", "/", "/publish"}) public String publish(String name) { return publishDemo.publish2mq(name);
    }
}

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

回复列表

相关推荐