消息中间件一 ActiveMQ简单使用和安装

消息中间件核心设计

1.持久化是什么?

简单来说jiu是数据存入磁盘,而不是在内存中随服务的重启而消失。

ActivieMQ,RabbitMQ,Kafka,RocketMQ 都支持文件系统 ,ActivieMQ支持数据库

2.消息分发 在这里插入图片描述

3.高可用机制

  1. Master主从共享数据模式
  2. Master主从同步部署方式
  3. Master 多主集群同步部署方式(多台主机 都可以提供服务)
  4. broker多主集群转发部署方式(1.自己本身没有转发到目的主机2.代理)
  5. Master—Slave 与 Broker-cluster结合

4.高可靠

5.协议

消息中间键常用的协议:OpenWire,AMQP,MQTT,Kafka,openMessage

MQTT:即时通信 物联网

kafka:基于TCP的二进制协议

二.ActiveMQ
一.ActiveMQ是什么

​ 1.apach出品遵循于 JMS

​ 2.JMS是什么?

​ Java小修服务 应用程序接口是一个Java平台中关于消息中间键的API应用在两个程序之间或者分布式系统中消息的发送,进行异步通信

​ 3.JMS的对象模型

在这里插入图片描述

  1. JMS消息模型

在这里插入图片描述

消息结构 (JMS)

在这里插入图片描述

JMS的消息体类型

在这里插入图片描述

二.ActiveMQ的特性

服务:broker 客户端:JMS

支持多种编程语言 传输协议 ,有多重持续化方式

一.下载ActiveMQ 安装

演示环境: Centos7、jdk8、activemq5.15.8
下载地址: http://activemq.apache.org/activemq-5158-release.html
解压: tar -zxvf apache-activemq-5.15.8-bin.tar.gz -C /var
修改目录名称 mv /var/apache-activemq-5.15.8/ /var/activemq/
启动: ./bin/activemq start
停止:./bin/activemq stop

操作练习

1、创建一个systemd服务文件:vi /usr/lib/systemd/system/activemq.service

2、 放入内容

[Unit]
Description=ActiveMQ service
After=network.target

[Service]
Type=forking
ExecStart=/var/activemq/bin/activemq start
ExecStop=/var/activemq/bin/activemq stop
User=root
Group=root
Restart=always
RestartSec=9
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=activemq

[Install]
WantedBy=multi-user.target

3、 找到java命令所在的目录 whereis java

4、设置activemq配置文件/var/activemq/bin/env中的JAVA_HOME

# Location of the java installation
# Specify the location of your java installation using JAVA_HOME, or specify the
# path to the "java" binary using JAVACMD
# (set JAVACMD to "auto" for automatic detection)
JAVA_HOME="/usr/local/java/jdk1.8.0_181"
JAVACMD="auto"

5、 通过systemctl管理activemq启停

  • 启动activemq服务: systemctl start activemq
  • 查看服务状态: systemctl status activemq
  • 创建软件链接:ln -s /usr/lib/systemd/system/activemq.service /etc/systemd/system/multi-user.target.wants/activemq.service
  • 开机自启: systemctl enable activemq
  • 检测是否开启成功(enable): systemctl list-unit-files |grep activemq

6、 防火墙配置,Web管理端口默认为8161(admin/admin),通讯端口默认为61616

  • 添加并重启防火墙
firewall-cmd --zone=public --add-port=8161/tcp --permanent
firewall-cmd --zone=public --add-port=61616/tcp --permanent
systemctl restart firewalld.service
  • 或者直接关闭防火墙: systemctl stop firewalld.service

7、 修改web管理系统的部分配置,配置文件在/var/activemq/conf

  • 端口修改
<bean id="jettyPort" class="org.apache.activemq.web.WebConsolePort" init-method="start">
  <!-- the default port number for the web console -->
  <property name="host" value="0.0.0.0"/>
  <!--此处即为管理平台的端口-->
  <property name="port" value="8161"/>
</bean>
  • 关闭登录
<bean id="securityConstraint" class="org.eclipse.jetty.util.security.Constraint">
  <property name="name" value="BASIC" />
  <property name="roles" value="user,admin" />
  <!-- 改为false即可关闭登陆 -->
  <property name="authenticate" value="true" />
</bean>
  • 其他配置: /var/activemq/conf/jetty-realm.properties
## ---------------------------------------------------------------------------
# 在此即可维护账号密码,格式:
# 用户名:密码,角色
# Defines users that can access the web (console, demo, etc.)
# username: password [,rolename ...]
admin: admin, admin
user: 123, user

8、 JAVA客户端的使用

  • 标准客户端使用
<dependency>
  <groupId>org.apache.activemq</groupId>
  <artifactId>activemq-all</artifactId>
  <version>5.15.8</version>
</dependency>
  • Spring中使用: http://spring.io/guides/gs/messaging-jms/
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
    <version>5.1.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-broker</artifactId>
    <version>5.15.8</version>
    <exclusions>
    <exclusion>
        <artifactId>geronimo-jms_1.1_spec</artifactId>
        <groupId>org.apache.geronimo.specs</groupId>
    </exclusion>
    </exclusions>
</dependency>演示环境: Centos7、jdk8、activemq5.15.8
下载地址: http://activemq.apache.org/activemq-5158-release.html
解压: ```tar -zxvf apache-activemq-5.15.8-bin.tar.gz -C /var```
修改目录名称 ```mv /var/apache-activemq-5.15.8/ /var/activemq/```
启动: ```./bin/activemq start```
停止:```./bin/activemq stop```

# 操作练习
1、创建一个systemd服务文件:```vi /usr/lib/systemd/system/activemq.service```

2、 放入内容
​```
[Unit]
Description=ActiveMQ service
After=network.target

[Service]
Type=forking
ExecStart=/var/activemq/bin/activemq start
ExecStop=/var/activemq/bin/activemq stop
User=root
Group=root
Restart=always
RestartSec=9
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=activemq

[Install]
WantedBy=multi-user.target
​```
3、 找到java命令所在的目录 ```whereis java ```

4、设置activemq配置文件/var/activemq/bin/env中的JAVA_HOME

​```
# Location of the java installation
# Specify the location of your java installation using JAVA_HOME, or specify the
# path to the "java" binary using JAVACMD
# (set JAVACMD to "auto" for automatic detection)
JAVA_HOME="/usr/local/java/jdk1.8.0_181"
JAVACMD="auto"
​```

5、 通过systemctl管理activemq启停
* 启动activemq服务: ```systemctl start activemq```
* 查看服务状态: ```systemctl status activemq```
* 创建软件链接:```ln -s /usr/lib/systemd/system/activemq.service /etc/systemd/system/multi-user.target.wants/activemq.service ```
* 开机自启: ```systemctl enable activemq```
* 检测是否开启成功(enable): ```systemctl list-unit-files |grep activemq ```

6、 防火墙配置,Web管理端口默认为8161(admin/admin),通讯端口默认为61616
* 添加并重启防火墙
​```
firewall-cmd --zone=public --add-port=8161/tcp --permanent
firewall-cmd --zone=public --add-port=61616/tcp --permanent
systemctl restart firewalld.service
​```
* 或者直接关闭防火墙: ```systemctl stop firewalld.service```

7、 修改web管理系统的部分配置,配置文件在```/var/activemq/conf```
* 端口修改
​```
<bean id="jettyPort" class="org.apache.activemq.web.WebConsolePort" init-method="start">
  <!-- the default port number for the web console -->
  <property name="host" value="0.0.0.0"/>
  <!--此处即为管理平台的端口-->
  <property name="port" value="8161"/>
</bean>
​```

* 关闭登录
​```
<bean id="securityConstraint" class="org.eclipse.jetty.util.security.Constraint">
  <property name="name" value="BASIC" />
  <property name="roles" value="user,admin" />
  <!-- 改为false即可关闭登陆 -->
  <property name="authenticate" value="true" />
</bean>
​```

* 其他配置: ```/var/activemq/conf/jetty-realm.properties```
​```
## ---------------------------------------------------------------------------
# 在此即可维护账号密码,格式:
# 用户名:密码,角色
# Defines users that can access the web (console, demo, etc.)
# username: password [,rolename ...]
admin: admin, admin
user: 123, user
​```

8、 JAVA客户端的使用
* 标准客户端使用
​```
<dependency>
  <groupId>org.apache.activemq</groupId>
  <artifactId>activemq-all</artifactId>
  <version>5.15.8</version>
</dependency>
​```

* Spring中使用: ```http://spring.io/guides/gs/messaging-jms/```
​```
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
    <version>5.1.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-broker</artifactId>
    <version>5.15.8</version>
    <exclusions>
    <exclusion>
        <artifactId>geronimo-jms_1.1_spec</artifactId>
        <groupId>org.apache.geronimo.specs</groupId>
    </exclusion>
    </exclusions>
</dependency>
​```

演示环境: Centos7、jdk8、activemq5.15.8
下载地址: http://activemq.apache.org/activemq-5158-release.html
解压: ```tar -zxvf apache-activemq-5.15.8-bin.tar.gz -C /var```
修改目录名称 ```mv /var/apache-activemq-5.15.8/ /var/activemq/```
启动: ```./bin/activemq start```
停止:```./bin/activemq stop```

# 操作练习
1、创建一个systemd服务文件:```vi /usr/lib/systemd/system/activemq.service```

2、 放入内容
​```
[Unit]
Description=ActiveMQ service
After=network.target

[Service]
Type=forking
ExecStart=/var/activemq/bin/activemq start
ExecStop=/var/activemq/bin/activemq stop
User=root
Group=root
Restart=always
RestartSec=9
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=activemq

[Install]
WantedBy=multi-user.target
​```
3、 找到java命令所在的目录 ```whereis java ```

4、设置activemq配置文件/var/activemq/bin/env中的JAVA_HOME

​```
# Location of the java installation
# Specify the location of your java installation using JAVA_HOME, or specify the
# path to the "java" binary using JAVACMD
# (set JAVACMD to "auto" for automatic detection)
JAVA_HOME="/usr/local/java/jdk1.8.0_181"
JAVACMD="auto"
​```

5、 通过systemctl管理activemq启停
* 启动activemq服务: ```systemctl start activemq```
* 查看服务状态: ```systemctl status activemq```
* 创建软件链接:```ln -s /usr/lib/systemd/system/activemq.service /etc/systemd/system/multi-user.target.wants/activemq.service ```
* 开机自启: ```systemctl enable activemq```
* 检测是否开启成功(enable): ```systemctl list-unit-files |grep activemq ```

6、 防火墙配置,Web管理端口默认为8161(admin/admin),通讯端口默认为61616
* 添加并重启防火墙
​```
firewall-cmd --zone=public --add-port=8161/tcp --permanent
firewall-cmd --zone=public --add-port=61616/tcp --permanent
systemctl restart firewalld.service
​```
* 或者直接关闭防火墙: ```systemctl stop firewalld.service```

7、 修改web管理系统的部分配置,配置文件在```/var/activemq/conf```
* 端口修改
​```
<bean id="jettyPort" class="org.apache.activemq.web.WebConsolePort" init-method="start">
  <!-- the default port number for the web console -->
  <property name="host" value="0.0.0.0"/>
  <!--此处即为管理平台的端口-->
  <property name="port" value="8161"/>
</bean>
​```

* 关闭登录
​```
<bean id="securityConstraint" class="org.eclipse.jetty.util.security.Constraint">
  <property name="name" value="BASIC" />
  <property name="roles" value="user,admin" />
  <!-- 改为false即可关闭登陆 -->
  <property name="authenticate" value="true" />
</bean>
​```

* 其他配置: ```/var/activemq/conf/jetty-realm.properties```
​```
## ---------------------------------------------------------------------------
# 在此即可维护账号密码,格式:
# 用户名:密码,角色
# Defines users that can access the web (console, demo, etc.)
# username: password [,rolename ...]
admin: admin, admin
user: 123, user
​```

8、 JAVA客户端的使用
* 标准客户端使用
​```
<dependency>
  <groupId>org.apache.activemq</groupId>
  <artifactId>activemq-all</artifactId>
  <version>5.15.8</version>
</dependency>
​```

* Spring中使用: ```http://spring.io/guides/gs/messaging-jms/```
​```
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
    <version>5.1.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-broker</artifactId>
    <version>5.15.8</version>
    <exclusions>
    <exclusion>
        <artifactId>geronimo-jms_1.1_spec</artifactId>
        <groupId>org.apache.geronimo.specs</groupId>
    </exclusion>
    </exclusions>
</dependency>
​```


三.ActiveMQ—Java中的使用

一.简单使用

1.Maven

<dependency>
  <groupId>org.apache.activemq</groupId>
  <artifactId>activemq-all</artifactId>
  <version>5.15.8</version>
</dependency>

2.简单使用

生产者

      ActiveMQConnectionFactory connectionFactory;
            Connection conn;
            Session session;

            try {
                // 1、创建连接工厂
                connectionFactory = new ActiveMQConnectionFactory(brokerUrl);
                // 2、创建连接对象md
                conn = connectionFactory.createConnection();
                conn.start();
                // 3、创建会话
                session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
                // 4、创建点对点发送的目标
                 Destination destination = session.createQueue(destinationUrl);
                // 5、创建生产者消息
                MessageProducer producer = session.createProducer(destination);
                // 设置生产者的模式,有两种可选 持久化 / 不持久化
                producer.setDeliveryMode(DeliveryMode.PERSISTENT);
                // 6、创建一条文本消息
                String text = "Hello world!";
                TextMessage message = session.createTextMessage(text);
                for (int i = 0; i < 1; i++) {
                    // 7、发送消息
                    producer.send(message);
                }
                // 8、 关闭连接
                session.close();
                conn.close();
            } catch (JMSException e) {
                e.printStackTrace();
            }

消费者

同上
     // 6、接收消息(没有消息就持续等待)
            Message message = consumer.receive();
            if (message instanceof TextMessage) {
                System.out.println("收到文本消息:" + ((TextMessage) message).getText());
            } else {
                System.out.println(message);
            }
二.SpringBoot集成
<dependencies>
        <!--直接使用spring-boot-starter-activemq-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
        <!-- MQTT -->
        <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-mqtt</artifactId>
        </dependency>
        </dependencies>
  1. 配置连接

    spring.activemq.broker-url=tcp://activemq.tony.com:61616
    spring.activemq.user=admin
    spring.activemq.password=admin
    

2.发送数据 (生产者)

@SpringBootApplication
public class Producer {

    //JMS
    @Autowired
    private JmsTemplate jmsTemplate;

    @PostConstruct
    public void init() {
        jmsTemplate.convertAndSend("queue1", "Hello Spring 4");
    }

    public static void main(String[] args) {
        SpringApplication.run(Producer.class, args);
    }
}

3.接受消息 (消费者)

@SpringBootApplication
@EnableJms
public class Consumer {

    @JmsListener(destination = "queue1")
    public void receive(String message) {
        System.out.println("收到消息:" + message);
    }

    public static void main(String[] args) {
        SpringApplication.run(Consumer.class, args);
    }
}
消息中间件核心设计

1.持久化是什么?

简单来说jiu是数据存入磁盘,而不是在内存中随服务的重启而消失。

ActivieMQ,RabbitMQ,Kafka,RocketMQ 都支持文件系统 ,ActivieMQ支持数据库

2.消息分发

推送拉取 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cMuavFC6-1573202353117)(/Users/gaofan/Desktop/笔记/image/Java/消息中间键的分发.png)]

3.高可用机制

  1. Master主从共享数据模式
  2. Master主从同步部署方式
  3. Master 多主集群同步部署方式(多台主机 都可以提供服务)
  4. broker多主集群转发部署方式(1.自己本身没有转发到目的主机2.代理)
  5. Master—Slave 与 Broker-cluster结合

4.高可靠

5.协议

消息中间键常用的协议:OpenWire,AMQP,MQTT,Kafka,openMessage

MQTT:即时通信 物联网

kafka:基于TCP的二进制协议

二.ActiveMQ
一.ActiveMQ是什么

​ 1.apach出品遵循于 JMS

​ 2.JMS是什么?

​ Java小修服务 应用程序接口是一个Java平台中关于消息中间键的API应用在两个程序之间或者分布式系统中消息的发送,进行异步通信

​ 3.JMS的对象模型

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-76gTzBIq-1573202353118)(/Users/gaofan/Desktop/笔记/image/Java/JMS对象模型.png)]

  1. JMS消息模型

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YnAzsu6B-1573202353118)(/Users/gaofan/Desktop/笔记/image/Java/JMS消息模型.png)]

消息结构 (JMS)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wuzR9u7C-1573202353119)(/Users/gaofan/Desktop/笔记/image/Java/JMS消息结构.png)]

JMS的消息体类型

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aW7BF3XZ-1573202353119)(/Users/gaofan/Desktop/笔记/image/Java/JMS消息体类型.png)]

二.ActiveMQ的特性

服务:broker 客户端:JMS

支持多种编程语言 传输协议 ,有多重持续化方式

一.下载ActiveMQ 安装

演示环境: Centos7、jdk8、activemq5.15.8
下载地址: http://activemq.apache.org/activemq-5158-release.html
解压: tar -zxvf apache-activemq-5.15.8-bin.tar.gz -C /var
修改目录名称 mv /var/apache-activemq-5.15.8/ /var/activemq/
启动: ./bin/activemq start
停止:./bin/activemq stop

操作练习

1、创建一个systemd服务文件:vi /usr/lib/systemd/system/activemq.service

2、 放入内容

[Unit]
Description=ActiveMQ service
After=network.target

[Service]
Type=forking
ExecStart=/var/activemq/bin/activemq start
ExecStop=/var/activemq/bin/activemq stop
User=root
Group=root
Restart=always
RestartSec=9
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=activemq

[Install]
WantedBy=multi-user.target

3、 找到java命令所在的目录 whereis java

4、设置activemq配置文件/var/activemq/bin/env中的JAVA_HOME

# Location of the java installation
# Specify the location of your java installation using JAVA_HOME, or specify the
# path to the "java" binary using JAVACMD
# (set JAVACMD to "auto" for automatic detection)
JAVA_HOME="/usr/local/java/jdk1.8.0_181"
JAVACMD="auto"

5、 通过systemctl管理activemq启停

  • 启动activemq服务: systemctl start activemq
  • 查看服务状态: systemctl status activemq
  • 创建软件链接:ln -s /usr/lib/systemd/system/activemq.service /etc/systemd/system/multi-user.target.wants/activemq.service
  • 开机自启: systemctl enable activemq
  • 检测是否开启成功(enable): systemctl list-unit-files |grep activemq

6、 防火墙配置,Web管理端口默认为8161(admin/admin),通讯端口默认为61616

  • 添加并重启防火墙
firewall-cmd --zone=public --add-port=8161/tcp --permanent
firewall-cmd --zone=public --add-port=61616/tcp --permanent
systemctl restart firewalld.service
  • 或者直接关闭防火墙: systemctl stop firewalld.service

7、 修改web管理系统的部分配置,配置文件在/var/activemq/conf

  • 端口修改
<bean id="jettyPort" class="org.apache.activemq.web.WebConsolePort" init-method="start">
  <!-- the default port number for the web console -->
  <property name="host" value="0.0.0.0"/>
  <!--此处即为管理平台的端口-->
  <property name="port" value="8161"/>
</bean>
  • 关闭登录
<bean id="securityConstraint" class="org.eclipse.jetty.util.security.Constraint">
  <property name="name" value="BASIC" />
  <property name="roles" value="user,admin" />
  <!-- 改为false即可关闭登陆 -->
  <property name="authenticate" value="true" />
</bean>
  • 其他配置: /var/activemq/conf/jetty-realm.properties
## ---------------------------------------------------------------------------
# 在此即可维护账号密码,格式:
# 用户名:密码,角色
# Defines users that can access the web (console, demo, etc.)
# username: password [,rolename ...]
admin: admin, admin
user: 123, user

8、 JAVA客户端的使用

  • 标准客户端使用
<dependency>
  <groupId>org.apache.activemq</groupId>
  <artifactId>activemq-all</artifactId>
  <version>5.15.8</version>
</dependency>
  • Spring中使用: http://spring.io/guides/gs/messaging-jms/
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
    <version>5.1.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-broker</artifactId>
    <version>5.15.8</version>
    <exclusions>
    <exclusion>
        <artifactId>geronimo-jms_1.1_spec</artifactId>
        <groupId>org.apache.geronimo.specs</groupId>
    </exclusion>
    </exclusions>
</dependency>演示环境: Centos7、jdk8、activemq5.15.8
下载地址: http://activemq.apache.org/activemq-5158-release.html
解压: ```tar -zxvf apache-activemq-5.15.8-bin.tar.gz -C /var```
修改目录名称 ```mv /var/apache-activemq-5.15.8/ /var/activemq/```
启动: ```./bin/activemq start```
停止:```./bin/activemq stop```

# 操作练习
1、创建一个systemd服务文件:```vi /usr/lib/systemd/system/activemq.service```

2、 放入内容
​```
[Unit]
Description=ActiveMQ service
After=network.target

[Service]
Type=forking
ExecStart=/var/activemq/bin/activemq start
ExecStop=/var/activemq/bin/activemq stop
User=root
Group=root
Restart=always
RestartSec=9
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=activemq

[Install]
WantedBy=multi-user.target
​```
3、 找到java命令所在的目录 ```whereis java ```

4、设置activemq配置文件/var/activemq/bin/env中的JAVA_HOME

​```
# Location of the java installation
# Specify the location of your java installation using JAVA_HOME, or specify the
# path to the "java" binary using JAVACMD
# (set JAVACMD to "auto" for automatic detection)
JAVA_HOME="/usr/local/java/jdk1.8.0_181"
JAVACMD="auto"
​```

5、 通过systemctl管理activemq启停
* 启动activemq服务: ```systemctl start activemq```
* 查看服务状态: ```systemctl status activemq```
* 创建软件链接:```ln -s /usr/lib/systemd/system/activemq.service /etc/systemd/system/multi-user.target.wants/activemq.service ```
* 开机自启: ```systemctl enable activemq```
* 检测是否开启成功(enable): ```systemctl list-unit-files |grep activemq ```

6、 防火墙配置,Web管理端口默认为8161(admin/admin),通讯端口默认为61616
* 添加并重启防火墙
​```
firewall-cmd --zone=public --add-port=8161/tcp --permanent
firewall-cmd --zone=public --add-port=61616/tcp --permanent
systemctl restart firewalld.service
​```
* 或者直接关闭防火墙: ```systemctl stop firewalld.service```

7、 修改web管理系统的部分配置,配置文件在```/var/activemq/conf```
* 端口修改
​```
<bean id="jettyPort" class="org.apache.activemq.web.WebConsolePort" init-method="start">
  <!-- the default port number for the web console -->
  <property name="host" value="0.0.0.0"/>
  <!--此处即为管理平台的端口-->
  <property name="port" value="8161"/>
</bean>
​```

* 关闭登录
​```
<bean id="securityConstraint" class="org.eclipse.jetty.util.security.Constraint">
  <property name="name" value="BASIC" />
  <property name="roles" value="user,admin" />
  <!-- 改为false即可关闭登陆 -->
  <property name="authenticate" value="true" />
</bean>
​```

* 其他配置: ```/var/activemq/conf/jetty-realm.properties```
​```
## ---------------------------------------------------------------------------
# 在此即可维护账号密码,格式:
# 用户名:密码,角色
# Defines users that can access the web (console, demo, etc.)
# username: password [,rolename ...]
admin: admin, admin
user: 123, user
​```

8、 JAVA客户端的使用
* 标准客户端使用
​```
<dependency>
  <groupId>org.apache.activemq</groupId>
  <artifactId>activemq-all</artifactId>
  <version>5.15.8</version>
</dependency>
​```

* Spring中使用: ```http://spring.io/guides/gs/messaging-jms/```
​```
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
    <version>5.1.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-broker</artifactId>
    <version>5.15.8</version>
    <exclusions>
    <exclusion>
        <artifactId>geronimo-jms_1.1_spec</artifactId>
        <groupId>org.apache.geronimo.specs</groupId>
    </exclusion>
    </exclusions>
</dependency>
​```

演示环境: Centos7、jdk8、activemq5.15.8
下载地址: http://activemq.apache.org/activemq-5158-release.html
解压: ```tar -zxvf apache-activemq-5.15.8-bin.tar.gz -C /var```
修改目录名称 ```mv /var/apache-activemq-5.15.8/ /var/activemq/```
启动: ```./bin/activemq start```
停止:```./bin/activemq stop```

# 操作练习
1、创建一个systemd服务文件:```vi /usr/lib/systemd/system/activemq.service```

2、 放入内容
​```
[Unit]
Description=ActiveMQ service
After=network.target

[Service]
Type=forking
ExecStart=/var/activemq/bin/activemq start
ExecStop=/var/activemq/bin/activemq stop
User=root
Group=root
Restart=always
RestartSec=9
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=activemq

[Install]
WantedBy=multi-user.target
​```
3、 找到java命令所在的目录 ```whereis java ```

4、设置activemq配置文件/var/activemq/bin/env中的JAVA_HOME

​```
# Location of the java installation
# Specify the location of your java installation using JAVA_HOME, or specify the
# path to the "java" binary using JAVACMD
# (set JAVACMD to "auto" for automatic detection)
JAVA_HOME="/usr/local/java/jdk1.8.0_181"
JAVACMD="auto"
​```

5、 通过systemctl管理activemq启停
* 启动activemq服务: ```systemctl start activemq```
* 查看服务状态: ```systemctl status activemq```
* 创建软件链接:```ln -s /usr/lib/systemd/system/activemq.service /etc/systemd/system/multi-user.target.wants/activemq.service ```
* 开机自启: ```systemctl enable activemq```
* 检测是否开启成功(enable): ```systemctl list-unit-files |grep activemq ```

6、 防火墙配置,Web管理端口默认为8161(admin/admin),通讯端口默认为61616
* 添加并重启防火墙
​```
firewall-cmd --zone=public --add-port=8161/tcp --permanent
firewall-cmd --zone=public --add-port=61616/tcp --permanent
systemctl restart firewalld.service
​```
* 或者直接关闭防火墙: ```systemctl stop firewalld.service```

7、 修改web管理系统的部分配置,配置文件在```/var/activemq/conf```
* 端口修改
​```
<bean id="jettyPort" class="org.apache.activemq.web.WebConsolePort" init-method="start">
  <!-- the default port number for the web console -->
  <property name="host" value="0.0.0.0"/>
  <!--此处即为管理平台的端口-->
  <property name="port" value="8161"/>
</bean>
​```

* 关闭登录
​```
<bean id="securityConstraint" class="org.eclipse.jetty.util.security.Constraint">
  <property name="name" value="BASIC" />
  <property name="roles" value="user,admin" />
  <!-- 改为false即可关闭登陆 -->
  <property name="authenticate" value="true" />
</bean>
​```

* 其他配置: ```/var/activemq/conf/jetty-realm.properties```
​```
## ---------------------------------------------------------------------------
# 在此即可维护账号密码,格式:
# 用户名:密码,角色
# Defines users that can access the web (console, demo, etc.)
# username: password [,rolename ...]
admin: admin, admin
user: 123, user
​```

8、 JAVA客户端的使用
* 标准客户端使用
​```
<dependency>
  <groupId>org.apache.activemq</groupId>
  <artifactId>activemq-all</artifactId>
  <version>5.15.8</version>
</dependency>
​```

* Spring中使用: ```http://spring.io/guides/gs/messaging-jms/```
​```
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
    <version>5.1.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-broker</artifactId>
    <version>5.15.8</version>
    <exclusions>
    <exclusion>
        <artifactId>geronimo-jms_1.1_spec</artifactId>
        <groupId>org.apache.geronimo.specs</groupId>
    </exclusion>
    </exclusions>
</dependency>
​```


三.ActiveMQ—Java中的使用

一.简单使用

1.Maven

<dependency>
  <groupId>org.apache.activemq</groupId>
  <artifactId>activemq-all</artifactId>
  <version>5.15.8</version>
</dependency>

2.简单使用

生产者

      ActiveMQConnectionFactory connectionFactory;
            Connection conn;
            Session session;

            try {
                // 1、创建连接工厂
                connectionFactory = new ActiveMQConnectionFactory(brokerUrl);
                // 2、创建连接对象md
                conn = connectionFactory.createConnection();
                conn.start();
                // 3、创建会话
                session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
                // 4、创建点对点发送的目标
                 Destination destination = session.createQueue(destinationUrl);
                // 5、创建生产者消息
                MessageProducer producer = session.createProducer(destination);
                // 设置生产者的模式,有两种可选 持久化 / 不持久化
                producer.setDeliveryMode(DeliveryMode.PERSISTENT);
                // 6、创建一条文本消息
                String text = "Hello world!";
                TextMessage message = session.createTextMessage(text);
                for (int i = 0; i < 1; i++) {
                    // 7、发送消息
                    producer.send(message);
                }
                // 8、 关闭连接
                session.close();
                conn.close();
            } catch (JMSException e) {
                e.printStackTrace();
            }

消费者

同上
     // 6、接收消息(没有消息就持续等待)
            Message message = consumer.receive();
            if (message instanceof TextMessage) {
                System.out.println("收到文本消息:" + ((TextMessage) message).getText());
            } else {
                System.out.println(message);
            }
二.SpringBoot集成
<dependencies>
        <!--直接使用spring-boot-starter-activemq-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
        <!-- MQTT -->
        <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-mqtt</artifactId>
        </dependency>
        </dependencies>
  1. 配置连接

    spring.activemq.broker-url=tcp://activemq.tony.com:61616
    spring.activemq.user=admin
    spring.activemq.password=admin
    

2.发送数据 (生产者)

@SpringBootApplication
public class Producer {

    //JMS
    @Autowired
    private JmsTemplate jmsTemplate;

    @PostConstruct
    public void init() {
        jmsTemplate.convertAndSend("queue1", "Hello Spring 4");
    }

    public static void main(String[] args) {
        SpringApplication.run(Producer.class, args);
    }
}

3.接受消息 (消费者)

@SpringBootApplication
@EnableJms
public class Consumer {

    @JmsListener(destination = "queue1")
    public void receive(String message) {
        System.out.println("收到消息:" + message);
    }

    public static void main(String[] args) {
        SpringApplication.run(Consumer.class, args);
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值