欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

activeMq和rabbitMq的搭建

程序员文章站 2022-05-18 14:56:40
...

集群的搭建有2个方案:

共享存储集群的原理:

优缺点:能够高可用,不能用负载均衡

activeMq和rabbitMq的搭建

当节点A挂掉后

activeMq和rabbitMq的搭建

基于复制的levelDB store的原理:broker cluster

优缺点:不能高可用,能负载均衡,至少需要3台服务器

 activeMq和rabbitMq的搭建

俩者之前的对比:

 activeMq和rabbitMq的搭建

俩个方案合并在一起:

 activeMq和rabbitMq的搭建

搭建服务器:创建三个节点在一个服务器

activeMq和rabbitMq的搭建

 

还需要创建一个共享服务目录为b和c节点

配置a节点:

activeMq.xml

注释多余的配置端口:

activeMq和rabbitMq的搭建

 activeMq和rabbitMq的搭建

在jetty.xml

配置管理端口

activeMq和rabbitMq的搭建

 

b节点中的activemq.xml配置文件

activeMq和rabbitMq的搭建

 activeMq和rabbitMq的搭建

 

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

 

相关标签: 消息中间件