网站建设公司名wordpress 添加图片

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

网站建设公司名,wordpress 添加图片,互联网运营公司排行榜,当建设部门网站如何避免消息的重复消费问题 1、 消息的幂等性1.1、概念1.2、产生业务场景 2、全局唯一IDRedis解决消息幂等性问题2.1、application.yml配置文件2.2、生产者发送消息2.3、消费者接收消息2.4、pom.xml引入依赖2.5、RabbitConfig配置类2.6、启动类2.7、订单对象2.8、测试 1、 消息… 如何避免消息的重复消费问题 1、 消息的幂等性1.1、概念1.2、产生业务场景 2、全局唯一IDRedis解决消息幂等性问题2.1、application.yml配置文件2.2、生产者发送消息2.3、消费者接收消息2.4、pom.xml引入依赖2.5、RabbitConfig配置类2.6、启动类2.7、订单对象2.8、测试 1、 消息的幂等性 https://blog.csdn.net/weixin_63267801/article/details/134211065 1.1、概念 消息的幂等性就是即使多次收到了消息也不会重复消费。 对于一个资源不管你请求一次还是请求多次对该资源本身造成的影响应该是相同的不能因为重复的请求而对该资源重复造成影响。 1.2、产生业务场景 同一个消息第一次接收正常处理业务如果该消息第二次再接收那就不能再处理业务否则就处理重复了。 2、全局唯一IDRedis解决消息幂等性问题 2.1、application.yml配置文件 配置rabbitmq和redis server:port: 8080 spring:application:name: rabbit_13_idempotent01_redisrabbitmq:host: 你的rabbitmq服务器IPport: 5672username: 你的rabbitmq服务管理员账号password: 你的rabbitmq服务管理员密码virtual-host: powerpublisher-confirm-type: correlated #开启交换机的确认模式publisher-returns: truelistener:simple:acknowledge-mode: manual #开启消费者的手动确认模式redis:host: 你的redis服务器IPport: 你的redis服务端口password: 你的redis服务密码database: 1 #1号数据库my:exchangeName: exchange.idempotent.01queueName: queue.idempotent.012.2、生产者发送消息 生产者模拟发送两笔相同的订单 使用com.fasterxml.jackson.databind.ObjectMapper对象进行数据序列化与反序列化 package com.power.service;import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.power.vo.Orders; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageBuilder; import org.springframework.amqp.core.MessageDeliveryMode; import org.springframework.amqp.core.MessageProperties; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Service;import javax.annotation.PostConstruct; import javax.annotation.Resource; import java.math.BigDecimal; import java.util.Date;Service Slf4j public class SendMessage {Resourceprivate RabbitTemplate rabbitTemplate;//这个对象可以进行序列化和反序列化json格式Resourceprivate ObjectMapper objectMapper;//构造方法执行后执行PostConstructpublic void init(){//开启生产者的确认模式rabbitTemplate.setConfirmCallback((correlationData, ack, cause) - {//如果交换机接收消息成功ack返回trueif(!ack){log.error(消息没有到达交换机原因是{},cause);//TODO 重发消息或者记录错误日志}});}Beanpublic void sendMsg() throws JsonProcessingException {{//发送第一笔订单消息Orders orders1 Orders.builder().orderId(order_100).orderName(手机).money(new BigDecimal(2345)).orderTime(new Date()).build();//将对象转换成jsonlog.info(orders1::::: orders1.toString());String strOrders1 objectMapper.writeValueAsString(orders1);log.info(strOrders1::::: strOrders1);MessageProperties messageProperties new MessageProperties();//设置单条消息持久化默认技术持久化的messageProperties.setDeliveryMode(MessageDeliveryMode.PERSISTENT);Message message MessageBuilder.withBody(strOrders1.getBytes()).andProperties(messageProperties).build();rabbitTemplate.convertAndSend(exchange.idempotent.01, info, message);}{//发送第二笔订单消息Orders orders2 Orders.builder().orderId(order_100).orderName(手机).money(new BigDecimal(2345)).orderTime(new Date()).build();String strOrders2 objectMapper.writeValueAsString(orders2);MessageProperties messageProperties new MessageProperties();//设置单条消息持久化默认技术持久化的messageProperties.setDeliveryMode(MessageDeliveryMode.PERSISTENT);Message message MessageBuilder.withBody(strOrders2.getBytes()).andProperties(messageProperties).build();rabbitTemplate.convertAndSend(exchange.idempotent.01, info, message);}log.info(消息发送完毕发送时间是new Date());} }2.3、消费者接收消息 package com.power.message;import com.fasterxml.jackson.databind.ObjectMapper; import com.power.vo.Orders; import com.rabbitmq.client.Channel; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.core.Message; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Component;import javax.annotation.Resource; import java.io.IOException;Component Slf4j public class ReceiveMessage {Resourceprivate ObjectMapper objectMapper;Resourceprivate StringRedisTemplate stringRedisTemplate;RabbitListener(queues {queue.idempotent.01})public void receiveMsg(Message message, Channel channel) throws IOException {//获取消息唯一标识long deliveryTag message.getMessageProperties().getDeliveryTag();//使用objectMapper把字节数组反序列化成对象Orders orders objectMapper.readValue(message.getBody(), Orders.class);try{log.info(接收到的消息为{},orders.toString());//如果不存在就在redis中存储Boolean setResult stringRedisTemplate.opsForValue().setIfAbsent(idempotent: orders.getOrderId(), orders.getOrderId());if(setResult){//TODO 向数据插入订单数据log.info(向数据库插入订单);}//手动确认接收消息成功channel.basicAck(deliveryTag,false);}catch (Exception e){log.error(消息处理出现问题);try {channel.basicNack(deliveryTag,false,true);} catch (IOException ex) {ex.printStackTrace();}throw new RuntimeException(e);}} }2.4、pom.xml引入依赖 ?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.power/groupIdartifactIdrabbit_13_idempotent01_redis/artifactIdversion1.0-SNAPSHOT/versionnamerabbit_13_idempotent01_redis/namepropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.6.13/versionrelativePath//parentdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.24/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-amqp/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project2.5、RabbitConfig配置类 package com.power.config;import org.springframework.amqp.core.*; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;Configuration public class RabbitConfig {Value(\({my.exchangeName})private String exchangeName;Value(\){my.queueName})private String queueName;//创建交换机Beanpublic DirectExchange directExchange(){return ExchangeBuilder.directExchange(exchangeName).build();}//创建队列Beanpublic Queue queue(){return QueueBuilder.durable(queueName).build();}Beanpublic Binding binding(DirectExchange directExchange,Queue queue){return BindingBuilder.bind(queue).to(directExchange).with(info);} }2.6、启动类 package com.power;import com.power.service.SendMessage; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;import javax.annotation.Resource;SpringBootApplication public class Application implements ApplicationRunner {Resourceprivate SendMessage messageService;public static void main(String[] args) {SpringApplication.run(Application.class);}Overridepublic void run(ApplicationArguments args) throws Exception {messageService.sendMsg();} }2.7、订单对象 package com.power.vo;import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor;import java.io.Serializable; import java.math.BigDecimal; import java.util.Date;Data AllArgsConstructor NoArgsConstructor Builder public class Orders implements Serializable {private String orderId;private String orderName;private BigDecimal money;private Date orderTime; }2.8、测试 启动服务后生产者发送消息消费者接收消息 两笔消息订单ID相同但是消费者只把接收到的一条消息插入了数据库实现了消息幂等性。