1. 程式人生 > >CentOS 搭建 RabbitMQ && Spring Boot 整合

CentOS 搭建 RabbitMQ && Spring Boot 整合

RabbitMQ

安裝Erlang

RabbitMQ 安裝需要依賴 Erlang 環境

$ cd /opt
$ wget http://www.rabbitmq.com/releases/erlang/erlang-19.0.4-1.el7.centos.x86_64.rpm
$ yum install erlang-19.0.4-1.el7.centos.x86_64.rpm

安裝 RabbitMQ

$ cd /opt
$ wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.10/rabbitmq-server-3.6.10-1.
el7.noarch.rpm $ yum install rabbitmq-server-3.6.10-1.el7.noarch.rpm

啟動服務

$ service rabbitmq-server start

服務狀態

$ service rabbitmq-server status
# service rabbitmq-server status
Redirecting to /bin/systemctl status  rabbitmq-server.service
● rabbitmq-server.service - RabbitMQ broker
   Loaded: loaded (/usr/lib/systemd/system/rabbitmq-server.service
; disabled; vendor preset: disabled) Active: active (running) since Wed 2017-08-16 11:43:33 CST; 8s ago Main PID: 17919 (beam) Status: "Initialized" CGroup: /system.slice/rabbitmq-server.service ├─17919 /usr/lib64/erlang/erts-8.0.3/bin/beam -W w -A 64 -P 1048576 -t 5000000 -stbt db -zdbbl 32000 -K true -- -root /usr/lib64/erlang -progname erl -- -home /var/lib/rabbitmq -- -pa /us... ├─18062
/usr/lib64/erlang/erts-8.0.3/bin/epmd -daemon ├─18160 erl_child_setup 1024 ├─18165 inet_gethost 4 └─18166 inet_gethost 4 Aug 16 11:43:32 localhost.localdomain rabbitmq-server[17919]: RabbitMQ 3.6.10. Copyright (C) 2007-2017 Pivotal Software, Inc. Aug 16 11:43:32 localhost.localdomain rabbitmq-server[17919]: ## ## Licensed under the MPL. See http://www.rabbitmq.com/ Aug 16 11:43:32 localhost.localdomain rabbitmq-server[17919]: ## ## Aug 16 11:43:32 localhost.localdomain rabbitmq-server[17919]: ########## Logs: /var/log/rabbitmq/[email protected] Aug 16 11:43:32 localhost.localdomain rabbitmq-server[17919]: ###### ## /var/log/rabbitmq/[email protected] Aug 16 11:43:32 localhost.localdomain rabbitmq-server[17919]: ########## Aug 16 11:43:32 localhost.localdomain rabbitmq-server[17919]: Starting broker... Aug 16 11:43:33 localhost.localdomain rabbitmq-server[17919]: systemd unit for activation check: "rabbitmq-server.service" Aug 16 11:43:33 localhost.localdomain systemd[1]: Started RabbitMQ broker. Aug 16 11:43:33 localhost.localdomain rabbitmq-server[17919]: completed with 0 plugins.

檢視日誌

$ less /var/log/rabbitmq/rabbit@localhost.log
=INFO REPORT==== 16-Aug-2017::11:43:32 ===
Starting RabbitMQ 3.6.10 on Erlang 19.0.4
Copyright (C) 2007-2017 Pivotal Software, Inc.
Licensed under the MPL.  See http://www.rabbitmq.com/

=INFO REPORT==== 16-Aug-2017::11:43:32 ===
node           : [email protected]
home dir       : /var/lib/rabbitmq
config file(s) : /etc/rabbitmq/rabbitmq.config (not found)
cookie hash    : kuUba2xGLitNNO48qE0Hrg==
log            : /var/log/rabbitmq/[email protected]
sasl log       : /var/log/rabbitmq/[email protected]
database dir   : /var/lib/rabbitmq/mnesia/[email protected]

=INFO REPORT==== 16-Aug-2017::11:43:33 ===
Memory limit set to 390MB of 976MB total.

=INFO REPORT==== 16-Aug-2017::11:43:33 ===
Enabling free disk space monitoring

=INFO REPORT==== 16-Aug-2017::11:43:33 ===
Disk free limit set to 50MB

=INFO REPORT==== 16-Aug-2017::11:43:33 ===
Limiting to approx 924 file handles (829 sockets)

=INFO REPORT==== 16-Aug-2017::11:43:33 ===
FHC read buffering:  OFF
FHC write buffering: ON

=INFO REPORT==== 16-Aug-2017::11:43:33 ===
Database directory at /var/lib/rabbitmq/mnesia/[email protected] is empty. Initialising from scratch...

=INFO REPORT==== 16-Aug-2017::11:43:33 ===
Waiting for Mnesia tables for 30000 ms, 9 retries left

=INFO REPORT==== 16-Aug-2017::11:43:33 ===
Waiting for Mnesia tables for 30000 ms, 9 retries left

顯示沒有找到日誌檔案,建立一個;

$ cd /etc/rabbitmq/
$ vi rabbitmq.config

編輯內容

[{rabbit, [{loopback_users, []}]}].
  • 這裡的意思是開放使用,rabbitmq預設建立的使用者guest,密碼也是guest,這個使用者預設只能是本機訪問,localhost或者127.0.0.1,從外部訪問需要新增上面的配置

重啟

$ service rabbitmq-server restart

開啟管理UI

$ /sbin/rabbitmq-plugins enable rabbitmq_management

重啟

訪問

通過瀏覽器訪問 http://ip:15672

建立新使用者

$ rabbitmqctl add_user zhao 123456

使用者授權

$ rabbitmqctl set_permissions -p "/" zhao".*" ".*" ".*"

新增角色

$ rabbitmqctl set_user_tags zhao administrator

檢視許可權

$ rabbitmqctl list_users

清除許可權

rabbitmqctl  clear_permissions  -p /  zhao

注意

保證伺服器 5672、15672 埠開放狀態;


SpringBoot 與 RabbitMQ 整合

建立SpringBoot專案;
這裡寫圖片描述

在pom檔案中 新增對amqp依賴

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>

application.properties檔案中對rabbitmq配置:

spring.application.name=spirng-boot-rabbitmq-sender
spring.rabbitmq.host=(ip)
spring.rabbitmq.port=5672  (這裡是5672,在瀏覽器UI登入時埠為15672)
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

使用者名稱可以是上面新建立使用者

採用Direct模式

配置Queue(訊息佇列)

@Configuration
public class SenderConf {
@Bean
public Queue queue() {
return new Queue(“queue”);
}
}

傳送端

@Component
public class HelloSender {
    @Autowired
    private AmqpTemplate template;

    public void send() {
    template.convertAndSend("queue","hello,rabbit~");
    }
}

controller 模擬傳送


@Controller
@RequestMapping("/rabbit")
public class HelloController {
    @Autowired
    private HelloSender helloSender;

    @PostMapping("/sender")
    public void testRabbit() {
        helloSender.send();
    }

接受者

@Component
public class HelloReceive {

    @RabbitListener(queues="queue")    //監聽器監聽指定的Queue
    public void processC(String str) {
        System.out.println("Receive:"+str);
    }

}

這裡寫圖片描述

成功接收