安装
RabbitMQ需要在Erlang环境下运行,可以直接安装RabbitMQ提供的零依赖Erlang。https://github.com/rabbitmq/erlang-rpm
如是CentOS 7在/etc/yum.repos.d/rabbitmq-erlang.repo文件中写入后 yum install erlang
就可以了。
[rabbitmq-erlang]
name=rabbitmq-erlang
baseurl=https://dl.bintray.com/rabbitmq/rpm/erlang/21/el/7
gpgcheck=1
gpgkey=https://dl.bintray.com/rabbitmq/Keys/rabbitmq-release-signing-key.asc
repo_gpgcheck=0
enabled=1
接下来从官网http://www.rabbitmq.com/install-rpm.html下载rpm文件就可以直接安装了。
rpm --import https://dl.bintray.com/rabbitmq/Keys/rabbitmq-release-signing-key.asc
yum install rabbitmq-server-3.7.7-1.el7.noarch.rpm
加入开机启动
chkconfig rabbitmq-server on
启动和停止服务命令
/sbin/service rabbitmq-server start
/sbin/service rabbitmq-server stop
启用web管理系统
rabbitmq-plugins enable rabbitmq_management
在浏览器就可以直接通过15672端口访问了,默认用户/密码(guest/guest)。如果登录出现如下错误,则需要创建新用户。新版本的RabbitMQ默认guest只能在localhost访问。
创建一个管理员用户
rabbitmqctl add_user admin admin
rabbitmqctl set_user_tags admin administrator
客户端和开发工具:http://www.rabbitmq.com/devtools.html
C++调用
Windows下编译rabbitmq-c
# 下载源码
git clone https://github.com/alanxz/rabbitmq-c.git
借助cmake工具
如果configure时ENABLE_SSL_SUPPORT报错,就取消勾选
The C compiler identification is MSVC 18.0.21005.1
Check for working C compiler: D:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin/cl.exe
Check for working C compiler: D:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin/cl.exe -- works
Detecting C compiler ABI info
Detecting C compiler ABI info - done
Detecting C compile features
Detecting C compile features - done
Found C inline keyword: __inline
Looking for poll
Looking for poll - not found
Looking for clock_gettime in rt
Looking for clock_gettime in rt - not found
Looking for posix_spawnp in rt
Looking for posix_spawnp in rt - not found
CMake Error at D:/Program Files/CMake/share/cmake-3.12/Modules/FindPackageHandleStandardArgs.cmake:137 (message):
Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the
system variable OPENSSL_ROOT_DIR (missing: OPENSSL_CRYPTO_LIBRARY
OPENSSL_INCLUDE_DIR) (Required is at least version "0.9.8")
Call Stack (most recent call first):
D:/Program Files/CMake/share/cmake-3.12/Modules/FindPackageHandleStandardArgs.cmake:378 (_FPHSA_FAILURE_MESSAGE)
D:/Program Files/CMake/share/cmake-3.12/Modules/FindOpenSSL.cmake:412 (find_package_handle_standard_args)
CMakeLists.txt:122 (find_package)
Configuring incomplete, errors occurred!
See also "D:/rabbitmq-c/build/CMakeFiles/CMakeOutput.log".
See also "D:/rabbitmq-c/build/CMakeFiles/CMakeError.log".
接下来点Generate就可以了,然后进入build\librabbitmq目录,用VS打开librabbitmq.sln全部编译就可以了。
全部编译后会在build\librabbitmq\Release或build\librabbitmq\Debug目录生成lib和dll文件。
这样就可以开始在vs中使用刚编译好的库了。首先在C/C++-常规-附加包含目录引入rabbitmq-c的头文件目录。
然后在链接器-常规-附加库目录指定刚编译刚的lib文件所在目录。
最后在链接器-输入-附加依赖项中指定rabbitmq.4.lib,这样就配置完成了,可以开始使用了。
未完待补充
Java调用
Java中与Spring Boot结合使用。如果是Gradle工程,在build.gradle文件中引入一下内容:
compile group: 'org.springframework.boot', name: 'spring-boot-starter-amqp', version: '2.0.3.RELEASE'
编写一个配置文件SenderConf.java,在里面声明exchange和queue并绑定。
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SenderConf {
public static final String QUEUE_NAME = "robot-queue";
public static final String TOPIC_EXCHANGE_NAME = "robot-exchange";
public static final String ROUTING_KEY = "robot-routing-key.*";
public static final String ROUTING_KEY_PREFIX = "robot-routing-key";
@Bean
public Queue queue() {
return new Queue(QUEUE_NAME, false);
}
@Bean
public TopicExchange topicExchange() {
return new TopicExchange(TOPIC_EXCHANGE_NAME, false, false);
}
@Bean
public Binding binding() {
return BindingBuilder.bind(queue()).to(topicExchange()).with(ROUTING_KEY);
}
}
在编写一个Sender.java文件,用来发送消息到MQ
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Sender {
@Autowired
private RabbitTemplate rabbitTemplate;
public void send(String exchange, String routingKey, String msg) {
this.rabbitTemplate.convertAndSend(exchange, routingKey, msg);
}
}
最后在Controller里面调用
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hello")
public class HelloController {
@Autowired
private Sender sender;
private static final String ROUTING_KEY = ".hello";
@RequestMapping(value = "/say", method = RequestMethod.POST)
public String say(@RequestParam("msg") String msg) {
sender.send(SenderConf.TOPIC_EXCHANGE_NAME, SenderConf.ROUTING_KEY_PREFIX.concat(ROUTING_KEY), msg);
return "success";
}
}