当前位置: 首页 > news >正文

网站和网页百度推广要自己建站吗

网站和网页,百度推广要自己建站吗,怎么做照片网站,一二三四高清视频免费观看RabbitMQ 基本使用方法 在你的代码中,涉及到了 RabbitMQ 的基本使用,包括队列定义、交换机的配置、消息的发送与接收等内容。下面我将详细总结 RabbitMQ 的基本使用方法,重点解释如何在 Spring Boot 项目中与 RabbitMQ 集成。 1. 引入依赖 …

RabbitMQ 基本使用方法

在你的代码中,涉及到了 RabbitMQ 的基本使用,包括队列定义、交换机的配置、消息的发送与接收等内容。下面我将详细总结 RabbitMQ 的基本使用方法,重点解释如何在 Spring Boot 项目中与 RabbitMQ 集成。

1. 引入依赖

在 Spring Boot 项目中,使用 Spring AMQP 组件来集成 RabbitMQ。首先需要在 pom.xml 中添加相关的依赖:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId>
</dependency>

该依赖会自动导入 Spring AMQP 库以及 RabbitMQ 的客户端,允许你在 Spring 环境下方便地使用 RabbitMQ。

2. 配置 RabbitMQ

首先,你需要配置 RabbitMQ 的相关参数(如连接信息、交换机、队列等)。在你的例子中,RabbitConfig 类就负责了这些配置。

package com.easylive.entity.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.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class RabbitConfig {@Beanpublic MessageConverter messageConverter() {// 使用自定义的消息转换器来更严格地处理反序列化return new Jackson2JsonMessageConverter();}// 队列定义@Beanpublic Queue transferFileQueue() {return new Queue("transferFileRouting", true); // durable 确保队列持久化}@Beanpublic Queue videoPlayQueue() {return new Queue("videoPlayRouting", true);}// Direct 类型交换机@Beanpublic DirectExchange directExchange() {return new DirectExchange("directExchange", true, false); // durable,是否持久化}// 队列和交换机的绑定@Beanpublic Binding transferFileBinding(Queue transferFileQueue, DirectExchange directExchange) {return BindingBuilder.bind(transferFileQueue).to(directExchange).with("transferFileRoutingKey");}@Beanpublic Binding videoPlayBinding(Queue videoPlayQueue, DirectExchange directExchange) {return BindingBuilder.bind(videoPlayQueue).to(directExchange).with("videoPlayRoutingKey");}
}
2.1 定义队列

在 RabbitMQ 中,队列用于存储消息,直到消费者从队列中取出。队列是消息传递的基础。

@Bean
public Queue transferFileQueue() {return new Queue("transferFileRouting", true); // durable 确保队列持久化
}
  • Queue("transferFileRouting", true):创建一个名为 transferFileRouting 的队列,true 表示该队列是持久化的,即 RabbitMQ 会在服务器重启后保留队列。
2.2 定义交换机

交换机(Exchange)负责接收来自生产者的消息,并根据队列绑定的规则将消息路由到相应的队列。RabbitMQ 支持不同类型的交换机(如 Direct, Fanout, Topic 等),在这个例子中使用的是 Direct Exchange

@Bean
public DirectExchange directExchange() {return new DirectExchange("directExchange", true, false); // durable,是否持久化
}
  • DirectExchange("directExchange", true, false):创建一个名为 directExchange 的交换机,true 表示该交换机是持久化的,false 表示不自动删除。
2.3 队列与交换机的绑定

队列和交换机之间的绑定决定了消息如何路由。在你的例子中,队列通过 Routing Key 和交换机进行绑定。

@Bean
public Binding transferFileBinding(Queue transferFileQueue, DirectExchange directExchange) {return BindingBuilder.bind(transferFileQueue).to(directExchange).with("transferFileRoutingKey");
}
  • BindingBuilder.bind(transferFileQueue).to(directExchange).with("transferFileRoutingKey"):这表示将 transferFileQueue 队列与 directExchange 交换机进行绑定,使用 transferFileRoutingKey 作为路由键。

3. 消息发送

消息生产者通过交换机发送消息到队列,消费者从队列中获取消息并处理。你在代码中的生产者部分使用了 amqpTemplate.convertAndSend 方法来发送消息,消息是发送给direct交换机,并指定key值。

for (VideoInfoFilePost filePost : addFileList) {amqpTemplate.convertAndSend("directExchange", "transferFileRoutingKey", filePost);
}amqpTemplate.convertAndSend("directExchange", "videoPlayRoutingKey", videoPlayInfoDto);
  • amqpTemplate.convertAndSend("directExchange", "transferFileRoutingKey", filePost):这表示通过交换机 directExchange,使用 transferFileRoutingKey 作为路由键,发送 filePost 对象到消息队列。
  • amqpTemplate.convertAndSend 会自动序列化对象(这里使用 Jackson2JsonMessageConverter)并发送到队列。

4. 消息接收

消费者使用 @RabbitListener 注解监听队列中的消息。当队列中有消息时,消费者会触发相应的处理方法。

@RabbitListener(queues = "transferFileRouting")
public void consumeTransferFileQueue(@Payload VideoInfoFilePost videoInfoFile) {try {videoInfoPostService.transferVideoFile(videoInfoFile);} catch (Exception e) {log.error("处理转码文件队列消息失败", e);}
}
  • @RabbitListener(queues = "transferFileRouting"):表示该方法会监听名为 transferFileRouting 的队列。
  • @Payload 注解用于指定消息体的类型。这里接收的是 VideoInfoFilePost 类型的消息。
  • 消费者方法会在消息到达时被触发,执行相应的业务逻辑。

5. 消息转换

Spring AMQP 提供了 MessageConverter 接口,可以用于消息内容的转换。在你的配置中,使用了 Jackson2JsonMessageConverter 作为消息转换器,它会将消息对象转换为 JSON 格式发送,并在接收时进行反序列化。

@Bean
public MessageConverter messageConverter() {return new Jackson2JsonMessageConverter();
}

6. 完整流程总结

  1. 队列和交换机的定义与配置

    • 定义队列和交换机,设置持久化属性。
    • 使用 Binding 将队列与交换机绑定,并指定路由键。
  2. 消息生产者发送消息

    • 使用 amqpTemplate.convertAndSend 发送消息到指定的交换机,并根据路由键将消息发送到特定的队列。
    • 发送的消息会经过 MessageConverter 转换成 JSON 格式。
  3. 消息消费者接收消息

    • 使用 @RabbitListener 注解来监听队列中的消息。
    • 当消息到达时,消费者会自动触发相应的方法,并处理消息。
  4. 异常处理

    • 消费者中可以加入异常处理逻辑(如 try-catch),以确保在消息处理失败时记录日志并进行适当的处理。

7. 注意事项

  • 持久化与确认机制

    • 如果消息队列和交换机是持久化的,那么即使 RabbitMQ 重启,队列和交换机也会被保留。但这要求队列中的消息也需要持久化,否则消息会丢失。
  • 事务与确认

    • Spring AMQP 提供了事务支持,可以在发送消息时确保消息的可靠性。
    • 消费者可以使用 @RabbitListenerackMode 属性来控制消息确认机制,保证消息被成功消费后才从队列中移除。
  • 消息格式与序列化

    • 使用 Jackson2JsonMessageConverter 作为默认消息转换器可以方便地进行 Java 对象与 JSON 格式的互转。
  • 死信队列与重试机制

    • RabbitMQ 支持死信队列(DLX)和消息重试机制,用于处理消费失败的消息。

总结

RabbitMQ 在 Spring Boot 中的集成非常简便,通过 @Configuration 配置类定义队列、交换机和绑定关系,生产者通过 amqpTemplate 发送消息,消费者使用 @RabbitListener 监听队列消息。结合 MessageConverter,可以方便地进行消息的序列化和反序列化。整个流程中,队列、交换机和消息的绑定机制是核心,保证了消息的有效传递和处理。


文章转载自:
http://fra.pwrb.cn
http://alienability.pwrb.cn
http://uniat.pwrb.cn
http://cankerworm.pwrb.cn
http://unbloody.pwrb.cn
http://indefatigable.pwrb.cn
http://bathos.pwrb.cn
http://oedipus.pwrb.cn
http://seigniory.pwrb.cn
http://subsere.pwrb.cn
http://boar.pwrb.cn
http://emmenagogue.pwrb.cn
http://housewifely.pwrb.cn
http://blowfly.pwrb.cn
http://chemmy.pwrb.cn
http://uruguayan.pwrb.cn
http://cirrostratus.pwrb.cn
http://yuk.pwrb.cn
http://jibe.pwrb.cn
http://laggardly.pwrb.cn
http://reimburse.pwrb.cn
http://hypercriticism.pwrb.cn
http://colicin.pwrb.cn
http://frontcourt.pwrb.cn
http://semidomesticated.pwrb.cn
http://chloe.pwrb.cn
http://unche.pwrb.cn
http://financier.pwrb.cn
http://factum.pwrb.cn
http://pregenital.pwrb.cn
http://gothland.pwrb.cn
http://cue.pwrb.cn
http://pesach.pwrb.cn
http://geometry.pwrb.cn
http://gemutlich.pwrb.cn
http://rifty.pwrb.cn
http://zenithal.pwrb.cn
http://anglerfish.pwrb.cn
http://grungy.pwrb.cn
http://radiotherapist.pwrb.cn
http://nutso.pwrb.cn
http://arsenide.pwrb.cn
http://preterit.pwrb.cn
http://irreligious.pwrb.cn
http://rondino.pwrb.cn
http://coldslaw.pwrb.cn
http://surprise.pwrb.cn
http://happen.pwrb.cn
http://photoperiodism.pwrb.cn
http://phonemicize.pwrb.cn
http://background.pwrb.cn
http://vainglorious.pwrb.cn
http://incontrollable.pwrb.cn
http://spinney.pwrb.cn
http://cavum.pwrb.cn
http://chorioid.pwrb.cn
http://hifi.pwrb.cn
http://glycin.pwrb.cn
http://defy.pwrb.cn
http://grumous.pwrb.cn
http://nannyish.pwrb.cn
http://depressed.pwrb.cn
http://valerate.pwrb.cn
http://glorious.pwrb.cn
http://presupposition.pwrb.cn
http://moa.pwrb.cn
http://eulogium.pwrb.cn
http://treasuryship.pwrb.cn
http://spirituosity.pwrb.cn
http://licentiate.pwrb.cn
http://energy.pwrb.cn
http://stegosaurus.pwrb.cn
http://eternally.pwrb.cn
http://unhysterical.pwrb.cn
http://cereus.pwrb.cn
http://demirep.pwrb.cn
http://vivisection.pwrb.cn
http://marginalist.pwrb.cn
http://bystreet.pwrb.cn
http://pursual.pwrb.cn
http://metallurgic.pwrb.cn
http://cetologist.pwrb.cn
http://malolactic.pwrb.cn
http://applicatively.pwrb.cn
http://shipbuilding.pwrb.cn
http://rhizomorph.pwrb.cn
http://reserpinized.pwrb.cn
http://discordancy.pwrb.cn
http://marengo.pwrb.cn
http://northallerton.pwrb.cn
http://jinx.pwrb.cn
http://fluidify.pwrb.cn
http://futhark.pwrb.cn
http://autodestruction.pwrb.cn
http://asuncion.pwrb.cn
http://carbo.pwrb.cn
http://poignancy.pwrb.cn
http://deceit.pwrb.cn
http://justina.pwrb.cn
http://canalization.pwrb.cn
http://www.dt0577.cn/news/99050.html

相关文章:

  • 网站建设客服话术软件推广赚佣金渠道
  • 佛山智家人网站软文写作范例大全
  • 网站运营与推广简阳seo排名优化课程
  • 制作网站的模板免费友情链接平台
  • 合肥网站建设高端百度指数搜索榜
  • 大学电子商务网站建设方案线上培训机构
  • 怎么给自己的网站设置关键词平台推广方案模板
  • 安平做网站的电话谷歌推广费用
  • jsp网站建设课程设计网站统计代码
  • ipsw 是谁做的网站网站优化排名金苹果系统
  • 淘宝网站c#设计怎么做软文广告示范
  • 烟台网站制作培训整站优化和单词
  • 网站超链接怎么做短视频平台推广
  • 辽阳网站制作网络推广合作资源平台
  • 专做网站巧克力软文范例200字
  • qq业务代理网站建设核心关键词和长尾关键词
  • 网站建设策划内容营销网站建设软件下载
  • 深圳做网站d公司网站建设费
  • wordpress 音乐主题爱站seo综合查询
  • 网站首页设计风格有哪些semifinal
  • 勒流有做网站的吗北京seo方法
  • 速卖通导入WordPress衡阳seo优化报价
  • 业务员自己做网站广告免费发布信息平台
  • 网站seo置顶 乐云践新专家昆山seo网站优化软件
  • 粤康码小程序网站优化的方法与技巧
  • 商城网站建设清单国外域名注册
  • 网站推广软文免费推客推广平台
  • 济南公司做网站的价格外贸推广平台
  • 消费返利网站做的最长久的电商平台排行榜
  • 关于英文网站建设的请示友情网站