广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >Redis 抽奖大转盘的实战示例
  • 782
分享到

Redis 抽奖大转盘的实战示例

2024-04-02 19:04:59 782人浏览 八月长安
摘要

目录1. 项目介绍2. 项目演示3. 表结构4. 项目搭建4.1 依赖4.2 YML配置4.3 代码生成4.4 Redis 配置4.5 常量管理4.6 业务代码4.7 总结5. 项目

1. 项目介绍

这是一个基于Spring Boot + mybatis Plus + Redis 的简单案例。

主要是将活动内容、奖品信息、记录信息等缓存到Redis中,然后所有的抽奖过程全部从Redis中做数据的操作。

大致内容很简单,具体操作下面慢慢分析。

2. 项目演示

话不多说,首先上图看看项目效果,如果觉得还行的话咱们就来看看他具体是怎么实现的。

image-20211229100617994

image-20211229101138854

3. 表结构

该项目包含以下四张表,分别是活动表、奖项表、奖品表以及中奖记录表。具体的sql会在文末给出。

image-20211229095750532

4. 项目搭建

咱们首先先搭建一个标准的spring boot 项目,直接idea创建,然后选择一些相关的依赖即可。

4.1 依赖

该项目主要用到了:Redis,thymeleaf,mybatis-plus等依赖。


<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-WEB</artifactId>
    </dependency>

    <dependency>
        <groupId>Mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.4.3</version>
    </dependency>

    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-generator</artifactId>
        <version>3.4.1</version>
    </dependency>

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastJSON</artifactId>
        <version>1.2.72</version>
    </dependency>

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>1.1.22</version>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.9</version>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.12</version>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
        <version>2.8.0</version>
    </dependency>

    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct</artifactId>
        <version>1.4.2.Final</version>
    </dependency>

    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct-jdk8</artifactId>
        <version>1.4.2.Final</version>
    </dependency>

    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct-processor</artifactId>
        <version>1.4.2.Final</version>
    </dependency>

    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.10.6</version>
    </dependency>
</dependencies>

4.2 YML配置

依赖引入之后,我们需要进行相应的配置:数据库连接信息、Redis、mybatis-plus、线程池等。


server:
  port: 8080
  servlet:
    context-path: /
spring:
  datasource:
    druid:
      url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
      username: root
      passWord: 123456
      driver-class-name: com.mysql.cj.jdbc.Driver
      initial-size: 30
      max-active: 100
      min-idle: 10
      max-wait: 60000
      time-between-eviction-runs-millis: 60000
      min-evictable-idle-time-millis: 300000
      validation-query: SELECT 1 FROM DUAL
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      filters: stat,wall
  redis:
    port: 6379
    host: 127.0.0.1
    lettuce:
      pool:
        max-active: -1
        max-idle: 2000
        max-wait: -1
        min-idle: 1
        time-between-eviction-runs: 5000
  mvc:
    view:
      prefix: classpath:/templates/
      suffix: .html
# mybatis-plus
mybatis-plus:
  configuration:
    map-underscore-to-camel-case: true
    auto-mapping-behavior: full
  mapper-locations: classpath*:mapper*Mapper.xml

# 线程池
async:
  executor:
    thread:
      core-pool-size: 6
      max-pool-size: 12
      queue-capacity: 100000
      name-prefix: lottery-service-

4.3 代码生成

这边我们可以直接使用mybatis-plus的代码生成器帮助我们生成一些基础的业务代码,避免这些重复的体力活。

这边贴出相关代码,直接修改数据库连接信息、相关包名模块名即可。


public class MybatisPlusGeneratorConfig {
    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig GC = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("chen");
        gc.setOpen(false);
        //实体属性 swagger2 注解
        gc.setSwagger2(false);
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://127.0.0.1:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
//        pc.setModuleName(scanner("模块名"));
        pc.setParent("com.example.lottery");
        pc.setEntity("dal.model");
        pc.setMapper("dal.mapper");
        pc.setService("service");
        pc.setServiceImpl("service.impl");
        mpg.setPackageInfo(pc);


        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();

        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setSuperEntityClass("com.baomidou.mybatisplus.extension.activerecord.Model");
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);

        strategy.setEntityLombokModel(true);
        // 公共父类
//        strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
        // 写于父类中的公共字段
//        strategy.setSuperEntityColumns("id");
        strategy.setInclude(scanner("lottery,lottery_item,lottery_prize,lottery_record").split(","));
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix(pc.getModuleName() + "_");
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }

    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + ":");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "!");
    }
}

4.4 Redis 配置

我们如果在代码中使用 RedisTemplate 的话,需要添加相关配置,将其注入到Spring容器中。


@Configuration
public class RedisTemplateConfig {
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        // 使用Jackson2jsonRedisSerialize 替换默认序列化
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(DateTime.class, new JodaDateTimeJsonSerializer());
        simpleModule.aDDDeserializer(DateTime.class, new JodaDateTimeJsonDeserializer());
        objectMapper.reGISterModule(simpleModule);

        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        // 设置value的序列化规则和 key的序列化规则
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.seTKEySerializer(new StringRedisSerializer());

        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);

        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

}

class JodaDateTimeJsonSerializer extends JsonSerializer<DateTime> {
    @Override
    public void serialize(DateTime dateTime, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        jsonGenerator.writeString(dateTime.toString("yyyy-MM-dd HH:mm:ss"));
    }
}

class JodaDateTimeJsonDeserializer extends JsonDeserializer<DateTime> {
    @Override
    public DateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        String dateString = jsonParser.readValueAs(String.class);
        DateTimeFORMatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        return dateTimeFormatter.parseDateTime(dateString);
    }
}

4.5 常量管理

由于代码中会用到一些共有的常量,我们应该将其抽离出来。


public class LotteryConstants {

    
    public final static String DRAWING = "DRAWING";
    
    
    public final static String LOTTERY = "LOTTERY";
    
    
    public final static String LOTTERY_PRIZE = "LOTTERY_PRIZE";
    
    
    public final static String DEFAULT_LOTTERY_PRIZE = "DEFAULT_LOTTERY_PRIZE";

    public enum PrizeTypeEnum {
        THANK(-1), NORMAL(1), UNIQUE(2);
        private int value;

        private PrizeTypeEnum(int value) {
            this.value = value;
        }

        public int getValue() {
            return this.value;
        }
    }

    
    public final static String LOTTERY_ITEM = "LOTTERY_ITEM";
    
    
    public final static String DEFAULT_LOTTERY_ITEM = "DEFAULT_LOTTERY_ITEM";

}

public enum ReturnCodeEnum {

    SUCCESS("0000", "成功"),

    LOTTER_NOT_EXIST("9001", "指定抽奖活动不存在"),

    LOTTER_FINISH("9002", "活动已结束"),

    LOTTER_REPO_NOT_ENOUGHT("9003", "当前奖品库存不足"),

    LOTTER_ITEM_NOT_INITIAL("9004", "奖项数据未初始化"),

    LOTTER_DRAWING("9005", "上一次抽奖还未结束"),

    REQUEST_PARAM_NOT_VALID("9998", "请求参数不正确"),

    SYSTEM_ERROR("9999", "系统繁忙,请稍后重试");

    private String code;

    private String msg;

    private ReturnCodeEnum(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public String getCodeString() {
        return getCode() + "";
    }
}

对Redis中的key进行统一的管理。


public class RedisKeyManager {

    
    public static String getDrawingRedisKey(String accountIp) {
        return new StringBuilder(LotteryConstants.DRAWING).append(":").append(accountIp).toString();
    }

    
    public static String getLotteryRedisKey(Integer id) {
        return new StringBuilder(LotteryConstants.LOTTERY).append(":").append(id).toString();
    }

    
    public static String getLotteryPrizeRedisKey(Integer lotteryId) {
        return new StringBuilder(LotteryConstants.LOTTERY_PRIZE).append(":").append(lotteryId).toString();
    }

    public static String getLotteryPrizeRedisKey(Integer lotteryId, Integer prizeId) {
        return new StringBuilder(LotteryConstants.LOTTERY_PRIZE).append(":").append(lotteryId).append(":").append(prizeId).toString();
    }

    public static String getDefaultLotteryPrizeRedisKey(Integer lotteryId) {
        return new StringBuilder(LotteryConstants.DEFAULT_LOTTERY_PRIZE).append(":").append(lotteryId).toString();
    }

    public static String getLotteryItemRedisKey(Integer lotteryId) {
        return new StringBuilder(LotteryConstants.LOTTERY_ITEM).append(":").append(lotteryId).toString();
    }

    public static String getDefaultLotteryItemRedisKey(Integer lotteryId) {
        return new StringBuilder(LotteryConstants.DEFAULT_LOTTERY_ITEM).append(":").append(lotteryId).toString();
    }
}

4.6 业务代码

4.6.1 抽奖接口

我们首先编写抽奖接口,根据前台传的参数查询到具体的活动,然后进行相应的操作。(当然,前端直接是写死的/lottery/1)


@GetMapping("/{id}")
public ResultResp<LotteryItemVo> doDraw(@PathVariable("id") Integer id, httpservletRequest request) {
    String accountIp = CusAccessObjectUtil.getIpAddress(request);
    log.info("begin LotteryController.doDraw,access user {}, lotteryId,{}:", accountIp, id);
    ResultResp<LotteryItemVo> resultResp = new ResultResp<>();
    try {
        //判断当前用户上一次抽奖是否结束
        checkDrawParams(id, accountIp);

        //抽奖
        DoDrawDto dto = new DoDrawDto();
        dto.setAccountIp(accountIp);
        dto.setLotteryId(id);
        lotteryService.doDraw(dto);

        //返回结果设置
        resultResp.setCode(ReturnCodeEnum.SUCCESS.getCode());
        resultResp.setMsg(ReturnCodeEnum.SUCCESS.getMsg());
        //对象转换
        resultResp.setResult(lotteryConverter.dto2LotteryItemVo(dto));
    } catch (Exception e) {
        return ExceptionUtil.handlerException4biz(resultResp, e);
    } finally {
        //清除占位标记
        redisTemplate.delete(RedisKeyManager.getDrawingRedisKey(accountIp));
    }
    return resultResp;
}

private void checkDrawParams(Integer id, String accountIp) {
    if (null == id) {
        throw new RewardException(ReturnCodeEnum.REQUEST_PARAM_NOT_VALID.getCode(), ReturnCodeEnum.REQUEST_PARAM_NOT_VALID.getMsg());
    }
    //采用setNx命令,判断当前用户上一次抽奖是否结束
    Boolean result = redisTemplate.opsForValue().setIfAbsent(RedisKeyManager.getDrawingRedisKey(accountIp), "1", 60, TimeUnit.SECONDS);
    //如果为false,说明上一次抽奖还未结束
    if (!result) {
        throw new RewardException(ReturnCodeEnum.LOTTER_DRAWING.getCode(), ReturnCodeEnum.LOTTER_DRAWING.getMsg());
    }
}

为了避免用户重复点击抽奖,所以我们通过Redis来避免这种问题,用户每次抽奖的时候,通过setNx给用户排队并设置过期时间;如果用户点击多次抽奖,Redis设置值的时候发现该用户上次抽奖还未结束则抛出异常。

最后用户抽奖成功的话,记得清除该标记,从而用户能够继续抽奖。

4.6.2 初始化数据

从抽奖入口进来,校验成功以后则开始业务操作。


@Override
public void doDraw(DoDrawDto drawDto) throws Exception {
    RewardContext context = new RewardContext();
    LotteryItem lotteryItem = null;
    try {
        //JUC工具 需要等待线程结束之后才能运行
        CountDownLatch countDownLatch = new CountDownLatch(1);
        //判断活动有效性
        Lottery lottery = checkLottery(drawDto);
        //发布事件,用来加载指定活动的奖品信息
        applicationContext.publishEvent(new InitPrizeToRedisEvent(this, lottery.getId(), countDownLatch));
        //开始抽奖
        lotteryItem = doPlay(lottery);
        //记录奖品并扣减库存
        countDownLatch.await(); //等待奖品初始化完成
        String key = RedisKeyManager.getLotteryPrizeRedisKey(lottery.getId(), lotteryItem.getPrizeId());
        int prizeType = Integer.parseInt(redisTemplate.opsForHash().get(key, "prizeType").toString());
        context.setLottery(lottery);
        context.setLotteryItem(lotteryItem);
        context.setAccountIp(drawDto.getAccountIp());
        context.setKey(key);
        //调整库存及记录中奖信息
        AbstractRewardProcessor.rewardProcessorMap.get(prizeType).doReward(context);
    } catch (UnRewardException u) { //表示因为某些问题未中奖,返回一个默认奖项
        context.setKey(RedisKeyManager.getDefaultLotteryPrizeRedisKey(lotteryItem.getLotteryId()));
        lotteryItem = (LotteryItem) redisTemplate.opsForValue().get(RedisKeyManager.getDefaultLotteryItemRedisKey(lotteryItem.getLotteryId()));
        context.setLotteryItem(lotteryItem);
        AbstractRewardProcessor.rewardProcessorMap.get(LotteryConstants.PrizeTypeEnum.THANK.getValue()).doReward(context);
    }
    //拼接返回数据
    drawDto.setLevel(lotteryItem.getLevel());
    drawDto.setPrizeName(context.getPrizeName());
    drawDto.setPrizeId(context.getPrizeId());
}

首先我们通过CountDownLatch来保证商品初始化的顺序,关于CountDownLatch可以查看 JUC工具 该文章。

然后我们需要检验一下活动的有效性,确保活动未结束。

检验活动通过后则通过ApplicationEvent 事件实现奖品数据的加载,将其存入Redis中。或者通过ApplicationRunner在程序启动时获取相关数据。我们这使用的是事件机制。ApplicationRunner 的相关代码在下文我也顺便贴出。

事件机制


public class InitPrizeToRedisEvent extends ApplicationEvent {

    private Integer lotteryId;

    private CountDownLatch countDownLatch;

    public InitPrizeToRedisEvent(Object source, Integer lotteryId, CountDownLatch countDownLatch) {
        super(source);
        this.lotteryId = lotteryId;
        this.countDownLatch = countDownLatch;
    }

    public Integer getLotteryId() {
        return lotteryId;
    }

    public void setLotteryId(Integer lotteryId) {
        this.lotteryId = lotteryId;
    }

    public CountDownLatch getCountDownLatch() {
        return countDownLatch;
    }

    public void setCountDownLatch(CountDownLatch countDownLatch) {
        this.countDownLatch = countDownLatch;
    }
}

有了事件机制,我们还需要一个监听事件,用来初始化相关数据信息。具体业务逻辑大家可以参考下代码,有相关的注释信息,主要就是将数据库中的数据添加进redis中,需要注意的是,我们为了保证原子性,是通过HASH来存储数据的,这样之后库存扣减的时候就可以通过opsForHash来保证其原子性。

当初始化奖品信息之后,则通过countDown()方法表名执行完成,业务代码中线程阻塞的地方可以继续执行了。


@Slf4j
@Component
public class InitPrizeToRedisListener implements ApplicationListener<InitPrizeToRedisEvent> {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    LotteryPrizeMapper lotteryPrizeMapper;

    @Autowired
    LotteryItemMapper lotteryItemMapper;

    @Override
    public void onApplicationEvent(InitPrizeToRedisEvent initPrizeToRedisEvent) {
        log.info("begin InitPrizeToRedisListener," + initPrizeToRedisEvent);
        Boolean result = redisTemplate.opsForValue().setIfAbsent(RedisKeyManager.getLotteryPrizeRedisKey(initPrizeToRedisEvent.getLotteryId()), "1");
        //已经初始化到缓存中了,不需要再次缓存
        if (!result) {
            log.info("already initial");
            initPrizeToRedisEvent.getCountDownLatch().countDown();
            return;
        }
        QueryWrapper<LotteryItem> lotteryIteMQueryWrapper = new QueryWrapper<>();
        lotteryItemQueryWrapper.eq("lottery_id", initPrizeToRedisEvent.getLotteryId());
        List<LotteryItem> lotteryItems = lotteryItemMapper.selectList(lotteryItemQueryWrapper);

        //如果指定的奖品没有了,会生成一个默认的奖项
        LotteryItem defaultLotteryItem = lotteryItems.parallelStream().filter(o -> o.getDefaultItem().intValue() == 1).findFirst().orElse(null);

        Map<String, Object> lotteryItemMap = new HashMap<>(16);
        lotteryItemMap.put(RedisKeyManager.getLotteryItemRedisKey(initPrizeToRedisEvent.getLotteryId()), lotteryItems);
        lotteryItemMap.put(RedisKeyManager.getDefaultLotteryItemRedisKey(initPrizeToRedisEvent.getLotteryId()), defaultLotteryItem);
        redisTemplate.opsForValue().multiSet(lotteryItemMap);

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("lottery_id", initPrizeToRedisEvent.getLotteryId());
        List<LotteryPrize> lotteryPrizes = lotteryPrizeMapper.selectList(queryWrapper);

        //保存一个默认奖项
        AtomicReference<LotteryPrize> defaultPrize = new AtomicReference<>();
        lotteryPrizes.stream().forEach(lotteryPrize -> {
            if (lotteryPrize.getId().equals(defaultLotteryItem.getPrizeId())) {
                defaultPrize.set(lotteryPrize);
            }
            String key = RedisKeyManager.getLotteryPrizeRedisKey(initPrizeToRedisEvent.getLotteryId(), lotteryPrize.getId());
            setLotteryPrizeToRedis(key, lotteryPrize);
        });
        String key = RedisKeyManager.getDefaultLotteryPrizeRedisKey(initPrizeToRedisEvent.getLotteryId());
        setLotteryPrizeToRedis(key, defaultPrize.get());
        initPrizeToRedisEvent.getCountDownLatch().countDown(); //表示初始化完成
        log.info("finish InitPrizeToRedisListener," + initPrizeToRedisEvent);
    }

    private void setLotteryPrizeToRedis(String key, LotteryPrize lotteryPrize) {
        redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
        redisTemplate.opsForHash().put(key, "id", lotteryPrize.getId());
        redisTemplate.opsForHash().put(key, "lotteryId", lotteryPrize.getLotteryId());
        redisTemplate.opsForHash().put(key, "prizeName", lotteryPrize.getPrizeName());
        redisTemplate.opsForHash().put(key, "prizeType", lotteryPrize.getPrizeType());
        redisTemplate.opsForHash().put(key, "totalStock", lotteryPrize.getTotalStock());
        redisTemplate.opsForHash().put(key, "validStock", lotteryPrize.getValidStock());
    }
}

上面部分是通过事件的方法来初始化数据,下面我们说下ApplicationRunner的方式:

这种方式很简单,在项目启动的时候将数据加载进去即可。

我们只需要实现ApplicationRunner接口即可,然后在run方法中从数据库读取数据加载到Redis中。


@Slf4j
@Component
public class LoadDataApplicationRunner implements ApplicationRunner {


    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    LotteryMapper lotteryMapper;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("=========begin load lottery data to Redis===========");
        //加载当前抽奖活动信息
        Lottery lottery = lotteryMapper.selectById(1);

        log.info("=========finish load lottery data to Redis===========");
    }
}

4.6.3 抽奖

我们在使用事件进行数据初始化的时候,可以同时进行抽奖操作,但是注意的是这个时候需要使用countDownLatch.await();来阻塞当前线程,等待数据初始化完成。

在抽奖的过程中,我们首先尝试从Redis中获取相关数据,如果Redis中没有则从数据库中加载数据,如果数据库中也没查询到相关数据,则表明相关的数据没有配置完成。

获取数据之后,我们就该开始抽奖了。抽奖的核心在于随机性以及概率性,咱们总不能随便抽抽都能抽到一等奖吧?所以我们需要在表中设置每个奖项的概率性。如下所示:

image-20211229212035238

在我们抽奖的时候需要根据概率划分处相关区间。我们可以通过Debug的方式来查看一下具体怎么划分的:

奖项的概率越大,区间越大;大家看到的顺序是不同的,由于我们在上面通过Collections.shuffle(lotteryItems);将集合打乱了,所以这里看到的不是顺序展示的。

image-20211229212634205

在生成对应区间后,我们通过生成随机数,看随机数落在那个区间中,然后将对应的奖项返回。这就实现了我们的抽奖过程。


private LotteryItem doPlay(Lottery lottery) {
    LotteryItem lotteryItem = null;
    QueryWrapper<LotteryItem> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("lottery_id", lottery.getId());
    Object lotteryItemsObj = redisTemplate.opsForValue().get(RedisKeyManager.getLotteryItemRedisKey(lottery.getId()));
    List<LotteryItem> lotteryItems;
    //说明还未加载到缓存中,同步从数据库加载,并且异步将数据缓存
    if (lotteryItemsObj == null) {
        lotteryItems = lotteryItemMapper.selectList(queryWrapper);
    } else {
        lotteryItems = (List<LotteryItem>) lotteryItemsObj;
    }
    //奖项数据未配置
    if (lotteryItems.isEmpty()) {
        throw new BizException(ReturnCodeEnum.LOTTER_ITEM_NOT_INITIAL.getCode(), ReturnCodeEnum.LOTTER_ITEM_NOT_INITIAL.getMsg());
    }
    int lastScope = 0;
    Collections.shuffle(lotteryItems);
    Map<Integer, int[]> awardItemScope = new HashMap<>();
    //item.getPercent=0.05 = 5%
    for (LotteryItem item : lotteryItems) {
        int currentScope = lastScope + new BigDecimal(item.getPercent().floatValue()).multiply(new BigDecimal(mulriple)).intValue();
        awardItemScope.put(item.getId(), new int[]{lastScope + 1, currentScope});
        lastScope = currentScope;
    }
    int luckyNumber = new Random().nextInt(mulriple);
    int luckyPrizeId = 0;
    if (!awardItemScope.isEmpty()) {
        Set<Map.Entry<Integer, int[]>> set = awardItemScope.entrySet();
        for (Map.Entry<Integer, int[]> entry : set) {
            if (luckyNumber >= entry.getValue()[0] && luckyNumber <= entry.getValue()[1]) {
                luckyPrizeId = entry.getKey();
                break;
            }
        }
    }
    for (LotteryItem item : lotteryItems) {
        if (item.getId().intValue() == luckyPrizeId) {
            lotteryItem = item;
            break;
        }
    }
    return lotteryItem;
}

4.6.4 调整库存及记录

在调整库存的时候,我们需要考虑到每个奖品类型的不同,根据不同类型的奖品采取不同的措施。比如如果是一些价值高昂的奖品,我们需要通过分布式来确保安全性;或者比如有些商品我们需要发送相应的短信;所以我们需要采取一种具有扩展性的实现机制。

具体的实现机制可以看下方的类图,我首先定义一个奖品方法的接口(RewardProcessor),然后定义一个抽象类(AbstractRewardProcessor),抽象类中定义了模板方法,然后我们就可以根据不同的类型创建不同的处理器即可,这大大加强了我们的扩展性。

比如我们这边就创建了库存充足处理器及库存不足处理器。

image-20211229214246943

image-20211229214223549

接口:


public interface RewardProcessor<T> {

    void doReward(RewardContext context);

}

抽象类:


@Slf4j
public abstract class AbstractRewardProcessor implements RewardProcessor<RewardContext>, ApplicationContextAware {

    public static Map<Integer, RewardProcessor> rewardProcessorMap = new ConcurrentHashMap<Integer, RewardProcessor>();

    @Autowired
    protected RedisTemplate redisTemplate;

    private void beforeProcessor(RewardContext context) {
    }

    @Override
    public void doReward(RewardContext context) {
        beforeProcessor(context);
        processor(context);
        afterProcessor(context);
    }

    protected abstract void afterProcessor(RewardContext context);


    
    protected abstract void processor(RewardContext context);

    
    protected abstract int getAwardType();

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        rewardProcessorMap.put(LotteryConstants.PrizeTypeEnum.THANK.getValue(), (RewardProcessor) applicationContext.getBean(NoneStockRewardProcessor.class));
        rewardProcessorMap.put(LotteryConstants.PrizeTypeEnum.NORMAL.getValue(), (RewardProcessor) applicationContext.getBean(HasStockRewardProcessor.class));
    }
}

我们可以从抽象类中的doReward方法处开始查看,比如我们这边先查看库存充足处理器中的代码:

库存处理器执行的时候首相将Redis中对应的奖项库存减1,这时候是不需要加锁的,因为这个操作是原子性的。

当扣减后,我们根据返回的值判断商品库存是否充足,这个时候库存不足则提示未中奖或者返回一个默认商品。

最后我们还需要记得更新下数据库中的相关数据。


@Override
protected void processor(RewardContext context) {
    //扣减库存(redis的更新)
    Long result = redisTemplate.opsForHash().increment(context.getKey(), "validStock", -1);
    //当前奖品库存不足,提示未中奖,或者返回一个兜底的奖品
    if (result.intValue() < 0) {
        throw new UnRewardException(ReturnCodeEnum.LOTTER_REPO_NOT_ENOUGHT.getCode(), ReturnCodeEnum.LOTTER_REPO_NOT_ENOUGHT.getMsg());
    }
    List<Object> propertys = Arrays.asList("id", "prizeName");
    List<Object> prizes = redisTemplate.opsForHash().multiGet(context.getKey(), propertys);
    context.setPrizeId(Integer.parseInt(prizes.get(0).toString()));
    context.setPrizeName(prizes.get(1).toString());
    //更新库存(数据库的更新)
    lotteryPrizeMapper.updateValidStock(context.getPrizeId());
}

方法执行完成之后,我们需要执行afterProcessor方法:

这个地方我们是通过异步任务异步存入抽奖记录信息。


@Override
protected void afterProcessor(RewardContext context) {
    asyncLotteryRecordTask.saveLotteryRecord(context.getAccountIp(), context.getLotteryItem(), context.getPrizeName());
}

在这边我们可以发现是通过Async注解,指定一个线程池,开启一个异步执行的方法。


@Slf4j
@Component
public class AsyncLotteryRecordTask {

    @Autowired
    LotteryRecordMapper lotteryRecordMapper;

    @Async("lotteryServiceExecutor")
    public void saveLotteryRecord(String accountIp, LotteryItem lotteryItem, String prizeName) {
        log.info(Thread.currentThread().getName() + "---saveLotteryRecord");
        //存储中奖信息
        LotteryRecord record = new LotteryRecord();
        record.setAccountIp(accountIp);
        record.setItemId(lotteryItem.getId());
        record.setPrizeName(prizeName);
        record.setCreateTime(LocalDateTime.now());
        lotteryRecordMapper.insert(record);
    }
}

创建一个线程池:相关的配置信息是我们定义在YML文件中的数据。


@Configuration
@EnableAsync
@EnableConfigurationProperties(ThreadPoolExecutorProperties.class)
public class ThreadPoolExecutorConfig {

    @Bean(name = "lotteryServiceExecutor")
    public Executor lotteryServiceExecutor(ThreadPoolExecutorProperties poolExecutorProperties) {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(poolExecutorProperties.getCorePoolSize());
        executor.setMaxPoolSize(poolExecutorProperties.getMaxPoolSize());
        executor.setQueueCapacity(poolExecutorProperties.getQueueCapacity());
        executor.setThreadNamePrefix(poolExecutorProperties.getNamePrefix());
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return executor;
    }
}

@Data
@ConfigurationProperties(prefix = "async.executor.thread")
public class ThreadPoolExecutorProperties {
    private int corePoolSize;
    private int maxPoolSize;
    private int queueCapacity;
    private String namePrefix;
}

4.7 总结

以上便是整个项目的搭建,关于前端界面无非就是向后端发起请求,根据返回的奖品信息,将指针落在对应的转盘位置处,具体代码可以前往项目地址查看。希望大家可以动个小手点点赞,嘻嘻。

5. 项目地址

如果直接使用项目的话,记得修改数据库中活动的结束时间。

Redis

具体的实战项目在lottery工程中。

image-20211229221247136

到此这篇关于Redis 抽奖大转盘的实战示例的文章就介绍到这了,更多相关Redis 抽奖大转盘内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Redis 抽奖大转盘的实战示例

本文链接: https://www.lsjlt.com/news/161318.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Redis 抽奖大转盘的实战示例
    目录1. 项目介绍2. 项目演示3. 表结构4. 项目搭建4.1 依赖4.2 YML配置4.3 代码生成4.4 Redis 配置4.5 常量管理4.6 业务代码4.7 总结5. 项目...
    99+
    2022-11-12
  • VUE实现大转盘抽奖
    目录UI初始参考各值参考图方案分析-参数配置核心思路:关于旋转位置参数配置实现逻辑组件使用抽奖效果结语UI 老规矩,先看下静态UI,以便于有个图像概念 初始参考各值参考图 方案...
    99+
    2022-11-12
  • vue实现大转盘抽奖功能
    本文实例为大家分享了vue实现大转盘抽奖的具体代码,供大家参考,具体内容如下 效果图如下 中奖提示 代码如下 <template>   <div class="...
    99+
    2022-11-13
  • Android实现抽奖转盘实例代码
    本文详述了android抽奖程序的实现方法,程序为一个抽奖大转盘代码,里面定义了很多图形方法和动画。 实现主要功能的SlyderView.java源代码如下: import ...
    99+
    2022-06-06
    抽奖转盘 Android
  • 如何实现纯CSS3大转盘抽奖
    这篇文章给大家分享的是有关如何实现纯CSS3大转盘抽奖的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。HTML<section class="gb-whe...
    99+
    2022-10-19
  • vue如何实现大转盘抽奖功能
    这篇文章主要为大家展示了“vue如何实现大转盘抽奖功能”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“vue如何实现大转盘抽奖功能”这篇文章吧。效果图如下中奖提示代码如下<template&...
    99+
    2023-06-29
  • jquery实现九宫格大转盘抽奖功能
    本篇内容主要讲解“jquery实现九宫格大转盘抽奖功能”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“jquery实现九宫格大转盘抽奖功能”吧! 一、用...
    99+
    2022-10-19
  • 微信小程序怎么实现抽奖大转盘
    这篇“微信小程序怎么实现抽奖大转盘”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“微信小程序怎么实现抽奖大转盘”文章吧。界面就...
    99+
    2023-06-26
  • iOS实现新年抽奖转盘效果的思路
    临近春节,相信不少app都会加一个新的需求——新年抽奖 不多废话,先上GIF效果图 DEMO链接 跑马灯效果 抽奖效果 实现步骤: 一、跑马灯效果 其实很简单,就是通过以下两...
    99+
    2022-05-24
    iOS 抽奖转盘
  • 微信小程序转盘抽奖的实现方法
    本文实例为大家分享了微信小程序实现转盘抽奖的具体代码,供大家参考,具体内容如下 lucky-draw.wxss: .lucky_draw_zp{ width: 502rpx; hei...
    99+
    2022-11-13
  • Java实现抽奖算法的示例代码
    目录一、题目描述二、解题思路三、代码详解四、优化抽奖算法解题思路代码详解一、题目描述 题目: 小虚竹为了给粉丝送福利,决定在参与学习打卡活动的粉丝中抽一位幸运粉丝,送份小礼物。为了公...
    99+
    2022-11-13
  • PHP实现抽奖系统的示例代码
    目录一、随机抽奖二、概率抽奖三、内定抽奖一、随机抽奖 随机抽奖当然是最公平的抽奖,就是当用户抽奖时随机返回抽奖结果 这种结果完全随机,不受人为控制,中奖全凭借运气 先定义奖池拥有的奖...
    99+
    2022-11-13
  • html+css+js实现带有转盘的抽奖小程序
    html+css+js实现带有转盘的抽奖小程序 简介:html+css+js实现带有转盘的抽奖小程序。 效果展示 抽奖方式: 飞镖 飞镖制作成飞机的模样,没人的号码用黄色标签做成云朵模样黏贴在背景板上,年会背景板缓缓移动,飞扎中的名字得奖...
    99+
    2023-08-16
    javascript html css
  • Vue3实现九宫格抽奖的示例代码
    目录前言前期构思具体实现1、布局2、指定奖品3、抽奖最终效果前言 对象说晚饭吃什么太难选择,问我能不能做一个九宫格抽奖来决定我们晚上吃什么,emmm。 既然对象都开口了,不做也不行啊...
    99+
    2022-11-13
  • JavaScript实现九宫格抽奖功能的示例代码
    目录效果图实现流程主要代码效果图 话不多说,直接上效果: 实现流程 主要流程为: 1. 根据效果图,构建静态页面 2. 获取元素(自带的属性) 3. 绑定事件 4. 事件触发之后 ...
    99+
    2022-11-13
  • QT实现年会抽奖小软件的示例代码
    目录一、效果展示:二、软件代码介绍1、工程目录2、核心代码之主类代码部分3、核心代码之线程类代码部分一、效果展示: 1、操作说明 下拉选择主题,点击开始按钮,开始滚动,再次点击停止,...
    99+
    2022-11-13
  • C#模拟实现抽奖小程序的示例代码
    目录1.抽奖主界面2.操作步骤2.1 抽奖界面2.2 抽奖结果导出3.源码3.1 数据库连接3.2 抽奖程序1.抽奖主界面 2.操作步骤 S键开始; 0、1、2、3、4、5键分别对...
    99+
    2022-11-12
  • Unity实现老虎机滚动抽奖效果的示例代码
    直接看下效果图吧: 制作思路: 设计四张图片,五个点,每个图片同时向下一个点移动,到最后一个就回到0号点,以此循环。 场景搭建: 创建Image命名为Bg作为电视框背...
    99+
    2022-11-12
  • 示例解析php的大转盘概率算法
    随着电商、营销活动的火热开展,各种打折、赠送活动越来越多,其中大转盘是一种非常受欢迎的赠送方式。然而,如果大家没有进行详细的概率算法,就有可能会导致奖品的概率失衡,甚至被玩家利用漏洞。而对于php大转盘的概率算法,更是需要我们深入研究。首先...
    99+
    2023-05-14
  • 微信小程序实现幸运大转盘功能的示例代码
    目录一、项目展示二、抽奖页三、领奖页一、项目展示 幸运大转盘是一个简单的抽奖小程序 参与用户点击抽奖便可抽取轮盘的奖品 二、抽奖页 抽奖页是一个大轮盘和活动规则 页面形式简单 主要...
    99+
    2022-11-13
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作