activeMq和rabbitMq的搭建
集群的搭建有2个方案:
共享存储集群的原理:
优缺点:能够高可用,不能用负载均衡
当节点A挂掉后
基于复制的levelDB store的原理:broker cluster
优缺点:不能高可用,能负载均衡,至少需要3台服务器
俩者之前的对比:
俩个方案合并在一起:
搭建服务器:创建三个节点在一个服务器
还需要创建一个共享服务目录为b和c节点
配置a节点:
activeMq.xml
注释多余的配置端口:
在jetty.xml
配置管理端口
b节点中的activemq.xml配置文件
RabbitMQ的基本安装
1 准备:
yum install
build-essential openssl openssl-devel unixODBC unixODBC-devel
make gcc gcc-c++ kernel-devel m4 ncurses-devel tk tc xz
2 下载:
wget www.rabbitmq.com/releases/erlang/erlang-18.3-1.el7.centos.x86_64.rpm
wget http://repo.iotti.biz/CentOS/7/x86_64/socat-1.7.3.2-5.el7.lux.x86_64.rpm
wget www.rabbitmq.com/releases/rabbitmq-server/v3.6.5/rabbitmq-server-3.6.5-1.noarch.rpm
rpm的安装方式是有顺序的:
rpm -ivh erlang-18.3-1.el7.centos.x86_64.rpm
rpm -ivh socat-1.7.3.2-5.el7.lux.x86_64.rpm
rpm -ivh rabbitmq-server-3.6.5-1.noarch.rpm
3 配置 vim /etc/hosts 以及 /etc/hostname (Linux防火墙)
3 配置文件:
vim /usr/lib/rabbitmq/lib/rabbitmq_server-3.6.5/ebin/rabbit.app
比如修改密码、配置等等,例如:loopback_users 中的 <<"guest">>,只保留guest
服务启动和停止:
启动 rabbitmq-server start &
停止 rabbitmqctl app_stop
如果发现不了rabbit-server就全局搜索一下
/usr/lib/rabbitmq/bin/rabbitmq-server
4 管理插件:rabbitmq-plugins enable rabbitmq_management
5 访问地址:http://192.168.11.81:15672/
整合rabbitMq和spring boot
发布者:
package com.producer.demo;
import com.producer.demo.entity.Order;
import com.producer.demo.producer.Producer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootProApplicationTests {
@Test
public void contextLoads() {
}
@Autowired
private Producer producer;
@Test
public void send() throws Exception{
Order order = new Order();
order.setId("2018111111");
order.setName("你好");
order.setMessageId("456789098765");
producer.send(order);
}
}
pom.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.producer</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringBoot-Pro</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.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</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--rabbit依赖的包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!--依赖工具-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!--io-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!--阿里工具类-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.36</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
###rabbitmq的配置
spring.rabbitmq.addresses=39.106.199.157:5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.virtual-host=/
spring.rabbitmq.connection-timeout=1500
###配置文件的配置
server.port=8081
server.servlet.context-path=/
####spring的配置
spring.http.encoding.charset=utf-8
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
spring.jackson.default-property-inclusion=NON_null
消费者的代码和配置
package com.producer.demo.consumer;
import com.producer.demo.entity.Order;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* @Description:
* @Author: Wudi
* @Create: 2018-09-09 20:59
**/
@Component
public class Consumer {
//注解监听
@RabbitListener(
bindings = @QueueBinding(
value = @Queue(value = "order-queue",durable = "true"),//队列
exchange = @Exchange(name="order-exchange",durable = "true",type = "topic"),//交换器
key = "order.#" //routingKey
)
)
@RabbitHandler
public void onOrderMessage(@Payload Order order, @Headers Map<String,Object> headers, Channel channel) throws Exception{
System.out.println("订单id{}"+order.getId());
Long number = (Long)headers.get(AmqpHeaders.DELIVERY_TAG);
//ACK 机制,此时是手动机制
channel.basicAck(number,false);
}
}
pom.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>consumer</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.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</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--rabbit依赖的包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!--依赖工具-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!--io-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!--阿里工具类-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.36</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
###rabbitmq的基本配置
spring.rabbitmq.addresses=39.106.199.157:5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.virtual-host=/
spring.rabbitmq.connection-timeout=15000
###consumer的基本配置
#最少监听线程数
spring.rabbitmq.listener.simple.concurrency=5
#最多监听线程数
spring.rabbitmq.listener.simple.max-concurrency=10
#线程限流
spring.rabbitmq.listener.direct.prefetch=1
#手动ack机制manual自动ack机制auto
spring.rabbitmq.listener.simple.acknowledge-mode=manual
###配置文件的配置
server.port=8082
server.servlet.context-path=/
####spring的配置
spring.http.encoding.charset=utf-8
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
spring.jackson.default-property-inclusion=NON_null
上一篇: PHP核心技术与最佳实践之Hash表摩擦
下一篇: PHP排序疑点
推荐阅读
-
搭建Python的Django框架环境并建立和运行第一个App的教程
-
Nginx搭建HTTPS服务器和强制使用HTTPS访问的方法
-
搭建Python的Django框架环境并建立和运行第一个App的教程
-
搭建SSH时的思考和遇到的几个问题的解决方法
-
hadoop搭建步骤(spark和hadoop的区别)
-
Maven实战之搭建Maven私服和镜像的方法(图文)
-
editplus和mingw一起搭建c/c++开发环境的详细步骤
-
Nginx搭建HTTPS服务器和强制使用HTTPS访问的方法
-
docker搭建rabbitmq集群环境的方法
-
Windows系统下使用flup搭建Nginx和Python环境的方法