SpringBoot+Mysql+Redis+RabbitMQ实现高并发秒杀

场景:商城对某一商品进行秒杀活动,该项目实例中,商品为watch,库存为10,使用jemter测试工具来模拟高并发场景代码实例:mysql表结构:库存表: 订单表: application.yml文件serve...
摘要由CSDN通过智能技术生成

场景:

商城对某一商品进行秒杀活动,该项目实例中,商品为watch,库存为10,使用jemter测试工具来模拟高并发场景

代码实例:

mysql表结构:

库存表:                                                                          订单表:

        

application.yml文件

server:
  port: 8089

spring:
   rabbitmq:
     virtual-host: /
     host: localhost
     username: guest
     password: guest
   application:
     name: concurrency-project
   redis:
      host: localhost
      port: 6379
      jedis:
        pool:
          max-active: 1024
          max-wait: -1s
          max-idle: 200
      password: 123456
   datasource:
         name: db_concurrency
         type: com.alibaba.druid.pool.DruidDataSource
         druid:
           driver-class-name: com.mysql.jdbc.Driver
           url: jdbc:mysql://localhost:3306/db_concurrency
           username: root
           password:
           initial-size: 1
           min-idle: 1
           max-active: 20
                     #获取连接等待超时时间
           max-wait: 60000
            #间隔多久进行一次检测,检测需要关闭的空闲连接
           time-between-eviction-runs-millis: 60000
   #一个连接在池中最小生存的时间
           min-evictable-idle-time-millis: 300000
           validation-query: SELECT 'x'
           test-while-idle: true
           test-on-borrow: false
           test-on-return: false
   #打开PSCache,并指定每个连接上PSCache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false
           pool-prepared-statements: false
           max-pool-prepared-statement-per-connection-size: 20


mybatis:
    mapper-locations: mapper/*.xml
    type-aliases-package: com.concurrency.concurrencyproject.model

pagehelper:
  helper-dialect: mysql

mapper:
  mappers: com.concurrency.concurrencyproject.base.service.GenericMapper
  not-empty: false
  identity: MYSQL


 

项目启动类:

@SpringBootApplication
@MapperScan("com.concurrency.concurrencyproject.mapper")
public class ConcurrencyProjectApplication implements ApplicationRunner {

    public static void main(String[] args) {
        SpringApplication.run(ConcurrencyProjectApplication.class, args);
    }

    @Autowired
    private RedisService redisService;

    /**
     * redis初始化各商品的库存量
     *
     * @param args
     * @throws Exception
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        redisService.put("watch", 10, 20);
    }
}

启动之后redis初始化为:

RabbitMQ配置类:

@Configuration
public class MyRabbitMQConfig {

    //库存交换机
    public static final String STORY_EXCHANGE = "STORY_EXCHANGE";

    //订单交换机
    public static final String ORDER_EXCHANGE = "ORDER_EXCHANGE";

    //库存队列
    public static fina
  • 5
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 18
    评论
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值