linux安装rabbitmq ssm集成rabbitmq

Posted 雨后丶云初霁

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux安装rabbitmq ssm集成rabbitmq相关的知识,希望对你有一定的参考价值。

rabbitmq 3.8.0 & erlang 22.1 源码编译安装

摘自 https://www.cnblogs.com/amosli/p/11765483.html

1.安装erlang编译依赖

sudo yum install -y gcc gcc-c++ glibc-devel make ncurses-devel openssl-devel autoconf java-1.8.0-openjdk-devel git

1.2.下载erlang22.1源码包
wget http://erlang.org/download/otp_src_22.1.tar.gz

1.3 编译并安装

# 解压
tar -zvxf otp_src_22.1.tar.gz
cd otp_src_22.1
 
#编译&安装
./otp_build autoconf
./configure && make && sudo make install
1.4.验证

erl -version
Erlang (SMP,ASYNC_THREADS,HIPE) (BEAM) emulator version 10.5
或

erl
Erlang/OTP 22 [erts-10.5] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [hipe]
 
Eshell V10.5 (abort with ^G)
1>

2.rabbitmq3.8.0 binary安装

2.1.下载rabbitmq安装包
wget https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.8.0/rabbitmq-server-generic-unix-3.8.0.tar.xz

2.2.解压并配置环境变量

解压:
xz -d rabbitmq-server-generic-unix-3.8.0.tar.xz
tar -xvf rabbitmq-server-generic-unix-3.8.0.tar
mv rabbitmq_server-3.8.0 /usr/local/rabbitmq_server-3.8.0

配置环境变量:
vim /etc/profile
# 在最后一行添加如下命令
export PATH=$PATH:/usr/local/lib/erlang/bin:/usr/local/rabbitmq_server-3.8.0/sbin
source /etc/profile

2.3.启动rabbitmq
rabbitmq-plugins enable rabbitmq_management
rabbitmq-server 或 rabbitmq-server -detached(后台运行)
2.4.添加新用户

默认账号密码是(guest/guest)
这里新添加账号:admin/admin

rabbitmqctl add_user admin admin
rabbitmqctl set_user_tags admin administrator

2.5停止rabbitmq
rabbitmqctl shutdown

2.6.修改核心配置参数

rabbitmq的核心配置参数在/usr/local/rabbitmq_server-3.8.0/ebin/rabbit.app里

vim /usr/local/rabbitmq_server-3.8.0/ebin/rabbit.app
# 找到loopback_users 修改为如下内容(去除 <<"guest">>中<<"">>) {loopback_users, [guest]},

修改完毕后就能解除guest账户的本地登录限制了。


ssm集成rabbitmq

摘自:https://blog.csdn.net/qq_30764991/article/details/80241205

rabbitmq管理地址:http://127.0.0.1:15672/

 

 

 

 ssm配置

pom.xml

<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>4.0.0</version>
</dependency>

<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>





applicationContext-rabbitmq.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.6.xsd">


 


<!-- rabbitmq连接配置 -->
<!-- virtual-host 就是在(1)中配置的VH-->
<!-- -->
<!-- -->
<rabbit:connection-factory id="connectionFactory" virtual-host="test"
host="192.168.1.49" username="admin" password="admin"
port="5672" />
<rabbit:admin connection-factory="connectionFactory" />


<!-- 声明消息队列,消息系统监听队列 -->
<!-- messageQueue 是在(1)中创建的队列 -->
<rabbit:queue id="messageQueue" durable="true"
auto-delete="false" exclusive="false" name="messageQueue" />
<rabbit:queue id="messageQueue" durable="true"
auto-delete="false" exclusive="false" name="messageQueue1" />
<rabbit:queue id="messageQueue" durable="true"
auto-delete="false" exclusive="false" name="messageQueue2" />


<!-- exchange queue binging key 绑定 -->
<!--声明交换 在(1)配置的交换-->
<rabbit:topic-exchange name="messageExchange"
durable="true" auto-delete="false" id="messageExchange">
<rabbit:bindings>
<!-- 声明队列和交换的关系 -->
<rabbit:binding pattern="message.*" queue="messageQueue"></rabbit:binding>
<rabbit:binding pattern="message.*" queue="messageQueue1"></rabbit:binding>
<rabbit:binding pattern="message.*" queue="messageQueue2"></rabbit:binding>
</rabbit:bindings>
</rabbit:topic-exchange>


<rabbit:template exchange="messageExchange" id="rabbitTemplate"
connection-factory="connectionFactory" />


<rabbit:listener-container
connection-factory="connectionFactory" acknowledge="auto">
<!-- 让消费者监听这个队列 -->
<rabbit:listener ref="messageConsumer" queues="messageQueue" />
<rabbit:listener ref="messageConsumer1" queues="messageQueue1" />
<rabbit:listener ref="messageConsumer2" queues="messageQueue2" />
</rabbit:listener-container>
<!--声明生产者 -->
<bean id="messageSender" class="com.shop.mq.MessageSender">
<property name="amqpTemplate" ref="rabbitTemplate"></property>
<!-- <property name="routingKey" value="message.test" ></property> -->
</bean>
<!--声明消费者 -->
<bean id="messageConsumer" class="com.shop.mq.MessageConsumer" />

<!--声明生产者 -->
<bean id="messageSender1" class="com.shop.mq.MessageSender1">
<property name="amqpTemplate" ref="rabbitTemplate"></property>
<!-- <property name="routingKey" value="message.test" ></property> -->
</bean>
<!--声明消费者 -->
<bean id="messageConsumer1" class="com.shop.mq.MessageConsumer1" />

<!--声明生产者 -->
<bean id="messageSender2" class="com.shop.mq.MessageSender2">
<property name="amqpTemplate" ref="rabbitTemplate"></property>
<!-- <property name="routingKey" value="message.test" ></property> -->
</bean>
<!--声明消费者 -->
<bean id="messageConsumer2" class="com.shop.mq.MessageConsumer2" />


</beans>

 


生产者MessageSender

package com.shop.mq;


import org.springframework.amqp.core.AmqpTemplate;


public class MessageSender {

private AmqpTemplate amqpTemplate;

private String routingKey;

public AmqpTemplate getAmqpTemplate() {
return amqpTemplate;
}

public void setAmqpTemplate(AmqpTemplate amqpTemplate) {
this.amqpTemplate = amqpTemplate;
}

public String getRoutingKey() {
return routingKey;
}

public void setRoutingKey(String routingKey) {
this.routingKey = routingKey;
}

public void sendDataToQueue(Object obj) {

amqpTemplate.convertAndSend(this.routingKey, obj);
}

}



消费者MessageConsumer 

package com.shop.mq;



import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

public class MessageConsumer implements MessageListener{

private Logger logger = LoggerFactory.getLogger(MessageConsumer.class);
@Override
public void onMessage(Message message) {
// createOrder();
logger.info("receive message:{}",message);
}

}




Controller

package com.shop.controller.test;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.shop.mq.MessageSender;
import com.shop.mq.MessageSender1;
import com.shop.mq.MessageSender2;


 


@Controller
@RequestMapping("/rabbit")
public class MainController {

// 注入消息生产者
@Autowired
private MessageSender messageSender;
// 注入消息生产者
@Autowired
private MessageSender1 messageSender1;
// 注入消息生产者
@Autowired
private MessageSender2 messageSender2;

private Logger logger = LoggerFactory.getLogger(MessageSender.class);

@RequestMapping(value = "/main")
public String main() {
return "main";
}

@RequestMapping(value = "/doValid")
@ResponseBody
public String doValid() {

// 设置RoutingKey,匹配message.*即可
messageSender.setRoutingKey("message.*");
// 发送消息
messageSender.sendDataToQueue("queue insert Queue");

logger.info("to send message:{}","insert Queue");

return "{\\"key\\" : \\"value\\"}";
}

@RequestMapping(value = "/doValid1")
@ResponseBody
public String doValid1() {

// 设置RoutingKey,匹配message.*即可
messageSender1.setRoutingKey("message.*");
// 发送消息
messageSender1.sendDataToQueue("queue1 insert Queue");

logger.info("to send message:{}","insert Queue");

return "{\\"key\\" : \\"value\\"}";
}

@RequestMapping(value = "/doValid2")
@ResponseBody
public String doValid2() {

// 设置RoutingKey,匹配message.*即可
messageSender2.setRoutingKey("message.*");
// 发送消息
messageSender2.sendDataToQueue("queue2 insert Queue");

logger.info("to send message:{}","insert Queue");

return "{\\"key\\" : \\"value\\"}";
}
}







 
 

 

以上是关于linux安装rabbitmq ssm集成rabbitmq的主要内容,如果未能解决你的问题,请参考以下文章

Java网络商城项目 SpringBoot+SpringCloud+Vue 网络商城(SSM前后端分离项目)十七(安装RabbitMQ(Centos6)(入门使用教程))以及Spring AMQP的)

RabbitMQ安装和使用(和Spring集成)

ssm+RabbitMQ 整合

Spring Boot(十三)RabbitMQ安装与集成

[SpringBoot] Spring Boot(13)RabbitMQ安装与集成

SSM框架搭建总结