<maven.compiler.target>1.7</maven.compiler.target>
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-amqp
org.springframework.boot
spring-boot-starter-web
junit
junit
4.11
test
springboot_rabbitmq
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-war-plugin
3.2.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
2.application.properties配置
server.port=8080
#spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.addresses=110.42.239.246
spring.rabbitmq.virtual-host=springboot
#spring.rabbitmq.addresses=110.42.239.246:5672,110.42.239.247:5672,110.42.239.248:5672
说明:这里免费提供rabbitmq连接方式给大家使用学习
3.config配置
HelloWorldConfig
package com.sky.springbootrabbitmqmodule.config;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
-
HelloWorld rabbitmq课上讲解的第一个工作模式
-
直连模式只需要声明队列,所有消息都通过队列转发。
-
无需设置交换机
*/
@Configuration
public class HelloWorldConfig {
@Bean
public Queue setQueue() {
return new Queue(“helloWorldqueue”);
}
}
FanoutConfig
package com.sky.springbootrabbitmqmodule.config;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
-
Fanout模式需要声明exchange,并绑定queue,由exchange负责转发到queue上。
-
广播模式 交换机类型设置为:fanout
*/
@Configuration
public class FanoutConfig {
//声明队列
@Bean
public Queue fanoutQ1() {
return new Queue(“fanout.q1”);
}
@Bean
public Queue fanoutQ2() {
return new Queue(“fanout.q2”);
}
//声明exchange
@Bean
public FanoutExchange setFanoutExchange() {
return new FanoutExchange(“fanoutExchange”);
}
//声明Binding,exchange与queue的绑定关系
@Bean
public Binding bindQ1() {
return BindingBuilder.bind(fanoutQ1()).to(setFanoutExchange());
}
@Bean
public Binding bindQ2() {
return BindingBuilder.bind(fanoutQ2()).to(setFanoutExchange());
}
}
WorkConfig
package com.sky.springbootrabbitmqmodule.config;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class WorkConfig {
//声明队列
@Bean
public Queue workQ1() {
return new Queue(“work_sb_mq_q”);
}
}
DirectConfig
package com.sky.springbootrabbitmqmodule.config;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
路由模式|Routing模式 交换机类型:direct
*/
@Configuration
public class DirectConfig {
//声明队列
@Bean
public Queue directQ1() {
return new Queue(“direct_sb_mq_q1”);
}
@Bean
public Queue directQ2() {
return new Queue(“direct_sb_mq_q2”);
}
//声明exchange
@Bean
public DirectExchange setDirectExchange() {
return new DirectExchange(“directExchange”);
}
//声明binding,需要声明一个routingKey
@Bean
public Binding bindDirectBind1() {
return BindingBuilder.bind(directQ1()).to(setDirectExchange()).with(“directBind.one”);
}
@Bean
public Binding bindDirectBind2() {
return BindingBuilder.bind(directQ2()).to(setDirectExchange()).with(“directBind.two”);
}
}
TopicConfig
package com.sky.springbootrabbitmqmodule.config;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
Topics模式 交换机类型 topic
- */
@Configuration
public class TopicConfig {
//声明队列
@Bean
public Queue topicQ1() {
return new Queue(“topic_sb_mq_q1”);
}
@Bean
public Queue topicQ2() {
return new Queue(“topic_sb_mq_q2”);
}
//声明exchange
@Bean
public TopicExchange setTopicExchange() {
return new TopicExchange(“topicExchange”);
}
//声明binding,需要声明一个roytingKey
@Bean
public Binding bindTopicHebei1() {
return BindingBuilder.bind(topicQ1()).to(setTopicExchange()).with(“directBind.*”);
}
@Bean
public Binding bindTopicHebei2() {
return BindingBuilder.bind(topicQ2()).to(setTopicExchange()).with(“#.two”);
}
}
4.消费端component
package com.sky.springbootrabbitmqmodule.component;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Component
public class ConcumerReceiver {
//直连模式的多个消费者,会分到其中一个消费者进行消费。类似task模式
//通过注入RabbitContainerFactory对象,来设置一些属性,相当于task里的channel.basicQos
@RabbitListener(queues=“helloWorldqueue”)
public void helloWorldReceive(String message) {
System.out.println("helloWorld模式 received message : " +message);
}
//工作队列模式
@RabbitListener(queues=“work_sb_mq_q”)
public void wordQueueReceiveq1(String message) {
System.out.println("工作队列模式1 received message : " +message);
}
@RabbitListener(queues=“work_sb_mq_q”)
public void wordQueueReceiveq2(String message) {
System.out.println("工作队列模式2 received message : " +message);
}
//pub/sub模式进行消息监听
@RabbitListener(queues=“fanout.q1”)
public void fanoutReceiveq1(String message) {
System.out.println("发布订阅模式1received message : " +message);
}
@RabbitListener(queues=“fanout.q2”)
public void fanoutReceiveq2(String message) {
System.out.println("发布订阅模式2 received message : " +message);
}
//Routing路由模式
@RabbitListener(queues=“direct_sb_mq_q1”)
public void routingReceiveq1(String message) {
System.out.println("Routing路由模式routingReceiveqOne received message : " +message);
}
@RabbitListener(queues=“direct_sb_mq_q2”)
public void routingReceiveq2(String message) {
System.out.println("Routing路由模式routingReceiveqTwo received message : " +message);
}
//topic 模式
//注意这个模式会有优先匹配原则。例如发送routingKey=hunan.IT,那匹配到hunan.(hunan.IT,hunan.eco),之后就不会再去匹配.ITd
@RabbitListener(queues=“topic_sb_mq_q1”)
public void topicReceiveq1(String message) {
System.out.println("Topic模式 topic_sb_mq_q1 received message : " +message);
}
@RabbitListener(queues=“topic_sb_mq_q2”)
public void topicReceiveq2(String message) {
System.out.println("Topic模式 topic_sb_mq_q2 received message : " +message);
}
}
5.生产者controller
package com.sky.springbootrabbitmqmodule.controller;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.UnsupportedEncodingException;
@RestController
public class ProducerController {
@Autowired
private RabbitTemplate rabbitTemplate;
//helloWorld 直连模式
@GetMapping(value=“/helloWorldSend”)
public Object helloWorldSend(String message) throws AmqpException, UnsupportedEncodingException {
//设置部分请求参数
MessageProperties messageProperties = new MessageProperties();
messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
//发消息
rabbitTemplate.send(“helloWorldqueue”,new Message(message.getBytes(“UTF-8”),messageProperties));
return "message sended : "+message;
}
//工作队列模式
@GetMapping(value=“/workqueueSend”)
public Object workqueueSend(String message) throws AmqpException, UnsupportedEncodingException {
MessageProperties messageProperties = new MessageProperties();
messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
//制造多个消息进行发送操作
for (int i = 0; i <10 ; i++) {
rabbitTemplate.send(“work_sb_mq_q”, new Message(message.getBytes(“UTF-8”),messageProperties));
}
return "message sended : "+message;
}
// pub/sub 发布订阅模式 交换机类型 fanout
@GetMapping(value=“/fanoutSend”)
public Object fanoutSend(String message) throws AmqpException, UnsupportedEncodingException {
MessageProperties messageProperties = new MessageProperties();
messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
//fanout模式只往exchange里发送消息。分发到exchange下的所有queue
rabbitTemplate.send(“fanoutExchange”, “”, new Message(message.getBytes(“UTF-8”),messageProperties));
return "message sended : "+message;
}
//routing路由工作模式 交换机类型 direct
@GetMapping(value=“/directSend”)
public Object routingSend(String routingKey,String message) throws AmqpException, UnsupportedEncodingException {
if(null == routingKey) {
routingKey=“directBind.one”;
}
MessageProperties messageProperties = new MessageProperties();
messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
//fanout模式只往exchange里发送消息。分发到exchange下的所有queue
rabbitTemplate.send(“directExchange”, routingKey, new Message(message.getBytes(“UTF-8”),messageProperties));
return “message sended : routingKey >”+routingKey+";message > "+message;
}
//topic 工作模式 交换机类型 topic
@GetMapping(value=“/topicSend”)
public Object topicSend(String routingKey,String message) throws AmqpException, UnsupportedEncodingException {
if(null == routingKey) {
routingKey=“directBind.one”;
}
MessageProperties messageProperties = new MessageProperties();
messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
//fanout模式只往exchange里发送消息。分发到exchange下的所有queue
rabbitTemplate.send(“topicExchange”, routingKey, new Message(message.getBytes(“UTF-8”),messageProperties));
return “message sended : routingKey >”+routingKey+";message > "+message;
}
}
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)
总结
大型分布式系统犹如一个生命,系统中各个服务犹如骨骼,其中的数据犹如血液,而Kafka犹如经络,串联整个系统。这份Kafka源码笔记通过大量的设计图展示、代码分析、示例分享,把Kafka的实现脉络展示在读者面前,帮助读者更好地研读Kafka代码。
麻烦帮忙转发一下这篇文章+关注我
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。[外链图片转存中…(img-SQ8XZaea-1713374036608)]
[外链图片转存中…(img-7DaPkadF-1713374036608)]
[外链图片转存中…(img-myJf32bc-1713374036608)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)
总结
大型分布式系统犹如一个生命,系统中各个服务犹如骨骼,其中的数据犹如血液,而Kafka犹如经络,串联整个系统。这份Kafka源码笔记通过大量的设计图展示、代码分析、示例分享,把Kafka的实现脉络展示在读者面前,帮助读者更好地研读Kafka代码。
麻烦帮忙转发一下这篇文章+关注我
[外链图片转存中…(img-bPxnstbj-1713374036609)]
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!