Redis + RabbitMQ solve spike high concurrency, asynchronous processing

Ideas
commodity spike is a typical high concurrency scenarios, in order to improve performance, reduce the number of database access can load data into redis, make the reduction of stock in redis in, and there will be no security thread, when redis success in reducing trade after the message can be pushed to rabbitMQ, the asynchronous synchronized to the database, so that the database according to his own ability to handle rabbitmq to fetch messages.
project architecture

Project architecture

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.RELEASE</version>
    <relativePath/>
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </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-data-redis</artifactId>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.1</version>
    </dependency>
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-boot-starter</artifactId>
      <version>2.2.0</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <!--  springboot 集成rabbitMQ的包      -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
  </dependencies>

 

Switch and the queues are defined at the consumer end

@Configuration
public class RabbitmqConfig {



    public static final String SPIKE_QUEUE = "SPIKE_QUEUE";

    public static final String EXCHANGE_NAME = "SPIKE_EXCHANGE";

    /**
     * 交换机配置
     * ExchangeBuilder提供了fanout、direct、topic、header交换机类型的配置
     *
     * @return the exchange
     */
    @Bean
    public Exchange EXCHANGE_DIRECT() {
    //durable(true)持久化,消息队列重启后交换机仍然存在
        return ExchangeBuilder.directExchange(EXCHANGE_NAME).durable(false).build();
    }

    //声明队列
    @Bean
    public Queue QUEUE_INFORM_SMS() {
        Queue queue = new Queue(SPIKE_QUEUE);
        return queue;
    }

//    @Value("${spring.rabbitmq.customizeRoutingKey=spike}")
    private String routingKey="spike";


    /**
     * channel.queueBind(INFORM_QUEUE_SMS,"inform_exchange_topic","inform.#.sms.#");
     * 绑定队列到交换机 .
     *
     */
    @Bean
    public Binding BINDING_QUEUE_INFORM_SMS( ) {
        return BindingBuilder.bind(QUEUE_INFORM_SMS()).to(EXCHANGE_DIRECT()).with(routingKey).noargs();
    }


}

 

 

 

End consumer queue listener receiving a message (this message is to take the synchronization database)

@Component
public class SpikeHandler {

    @Autowired
    private IPhoneService phoneService;

    @RabbitListener(queues = {RabbitmqConfig.SPIKE_QUEUE})
    public void synchronizeDB(String message, Message message1, Channel channel) throws IOException {
        System.out.println("接收到的信息" + message);
        if (message == null || StringUtils.isEmpty(message)) {
            throw new RuntimeException("接收消息异常");
        }
        phoneService.update();
//        手动签收消息
        long deliveryTag = message1.getMessageProperties().getDeliveryTag();
        channel.basicAck(deliveryTag,true);
    }
}

 rabbitTemplate.convertAndSend (RabbitmqConfig.EXCHANGE_NAME, "spike", "buying success");

 

References:

https://blog.csdn.net/weixin_45336205/article/details/104230478?utm_source=distribute.pc_category.none-task

 

 

Published 515 original articles · won praise 10 · views 90000 +

Guess you like

Origin blog.csdn.net/xiamaocheng/article/details/104267787