`
- 浏览:
228494 次
- 性别:
- 来自:
深圳
-
1.完整的pom.xml
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.soft.rabbit.server</groupId>
<artifactId>soft-rabbit-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-cloud.version>Finchley.M9</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--使用 ${spring.cloud.client.ip-address} 需引用下面的包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
</dependency>
<!-- 添加springboot对amqp的支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>soft-rabbit-server</finalName>
<sourceDirectory>${basedir}/src/main/java</sourceDirectory>
<outputDirectory>${basedir}/bin/classes</outputDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/java</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.*</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>*.yml</include>
<include>*.sh</include>
</includes>
<targetPath>${basedir}/target</targetPath>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.soft.rabbit.server.ServerApplication</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/resources</outputDirectory>
<resources>
<resource>
<directory>${basedir}/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2.配置类
package com.soft.rabbit.server.config;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class DirectRabbitConfig {
//队列 起名:TestDirectQueue
@Bean
public Queue TestDirectQueue() {
// durable:是否持久化,默认是false,持久化队列:会被存储在磁盘上,当消息代理重启时仍然存在,暂存队列:当前连接有效
// exclusive:默认也是false,只能被当前创建的连接使用,而且当连接关闭后队列即被删除。此参考优先级高于durable
// autoDelete:是否自动删除,当没有生产者或者消费者使用此队列,该队列会自动删除。
// return new Queue("TestDirectQueue",true,true,false);
//一般设置一下队列的持久化就好,其余两个就是默认false
return new Queue("TestDirectQueue",true);
}
//Direct交换机 起名:TestDirectExchange
@Bean
DirectExchange TestDirectExchange() {
// return new DirectExchange("TestDirectExchange",true,true);
return new DirectExchange("TestDirectExchange",true,false);
}
//绑定 将队列和交换机绑定, 并设置用于匹配键:TestDirectRouting
@Bean
Binding bindingDirect() {
return BindingBuilder.bind(TestDirectQueue()).to(TestDirectExchange()).with("TestDirectRouting");
}
@Bean
DirectExchange lonelyDirectExchange() {
return new DirectExchange("lonelyDirectExchange");
}
}
3.生产者类
package com.soft.rabbit.server.service;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class DirectService {
@Autowired
private AmqpTemplate rabbitTemplate;
public String send() {
String messageId = String.valueOf(UUID.randomUUID());
String messageData = "test message, hello!";
String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
Map<String,Object> map=new HashMap<String,Object>();
map.put("messageId",messageId);
map.put("messageData",messageData);
map.put("createTime",createTime);
//将消息携带绑定键值:TestDirectRouting 发送到交换机TestDirectExchange
rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map);
return "ok";
}
}
4.启动类
package com.soft.rabbit.server;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ServerApplication {
private static Logger logger = LoggerFactory.getLogger(ServerApplication.class);
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
logger.info("server start success!");
}
}
5.bootstrap.yml
#http端口配置
server:
port: 6001
connection-timeout: 5000
tomcat:
max-http-post-size: -1
max-threads: 1000
max-connections: 1000
spring:
application:
name: soft-rabbit-server
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
# eureka注册中心配置
eureka:
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:9001/eureka/
instance:
instance-id: ${spring.cloud.client.ip-address}:${server.port}
prefer-ip-address: true
hostname: ${spring.cloud.client.ip-address}
6.消费者类
package com.soft.rabbit.client.service;
import java.util.Map;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "TestDirectQueue") // 监听的队列名称 TestDirectQueue
public class DirectReceiver {
@RabbitHandler
public void process(Map testMessage) {
System.out.println("DirectReceiver消费者收到消息 : " + testMessage.toString());
}
}
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
rabbit mq入门例子. 使用rabbit mq实现服务器连接,消息发送,接收。
rabbit mq 安装手册,详细介绍了rabbit mq在linux环境下如何安装
连接rabbit mq的简单demo,演示如果连接mq并发送消息接收消息
rabbit mq windows 64 较新,很实用,消息中间件服务端。
spring boot 集成rabbit mq 成功demo,spring boot 集成rabbit mq 成功demo
docker 安裝 rabbit mq 並測試 http://knight-black-bob.iteye.com/blog/2395713
本附件包含Rabbit MQ的安装包以及作者亲自撰写的安装步骤,供大家参考。
Rabbit MQ讲解 和介绍。。。。。。。。。。。。。。。。。。。
更新rabbit mq的demo,演示连接mq并发送消息接收消息 持久化模式、客户端订阅方式、单链接
RabbitMQ消费者代码
Rabbit MQ Server 3.7.24 + Erlang OTP 22.1 for Windows X64 安装文件。 附带安装教程。
Rabbit mq教程
Linux 离线安装Rabbit MQ所需环境及安装包, 主要包含ncurses,perl(perl-5.36.0.tar.gz),OpenSSL(openssl-1.1.1i.tar.gz),erlang(otp_src_23.2.tar.gz),rabbitmq(rabbitmq-server-generic-unix-3.8.11....
windows64位操作系统 rabbit mq3.12.4及配套的Erlang/OTP26.0 安装包。官方资源不好下载
各个微服务统一使用spring-cloud-starter-stream-rabbit 为规范各个微服务之间消息队列的交换机、通道、队列、消息投递更加便于识别、扩展和维护特进行以下要求
spring-rabbit-1.5.1.RELEASE.jar, spring-amqp-1.5.1.RELEASE.jar, rabbitmq-client.jar spring-retry-1.1.2.RELEASE.jar 四个包 之前的资源不好意思传错了一个
本书首先介绍了有关MQ的历史,然后从基本的消息通信原理讲起,带领读者一路探索RabbitMQ的消息通信世界。这当中不仅包含了针对单台RabbitMQ服务器和RabbitMQ集群的讲解,还教导读者如何使用各种工具来进行监控。
消息队列,先进先出
rabbitmq全部安装包,下载后利用其中的文件可以很方便的下载和自己电脑适配的Rabbit MQ,需要注意的是,在下载之前,请保证自己电脑上有c/c++的环境,推介使用visual studio,社区免费版本的就行
Rabbit MQ 是建立在强大的Erlang OTP平台上,因此安装Rabbit MQ的前提是安装Erlang