2024年用户积分和积分排行榜功能微服务实现_积分排行榜软件架构(2),字节跳动8年老大数据开发面试官经验谈

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

+ [积分排行榜TopN(关系型数据库)](#TopN_428)
+ - [构造数据](#_432)
	- [用户积分排名对象](#_486)
	- [积分控制层](#_508)
	- [积分业务逻辑层](#_522)
	- [数据交互Mapper层](#Mapper_556)

需求分析

在这里插入图片描述
在这里插入图片描述

什么是积分

在互联网应用中经常会有积分的概念,会员积分是一种成长手段,就像游戏中的等级一样,通过积分叠加,让用户深刻感受到自己的价值在提升。
积分的诞生远早于互联网产品,积分从本质上讲是衡量用户消费或贡献行为的标尺,是维护忠诚度的一个重要手段。

  • 在积分运营中,积分一般作为商家向会员或顾客发行的虚拟货币而存在。用户通过特定行为获取积分,再通过积分兑换奖品、优惠券、特权等商品来消耗积分。
  • 在电商体系中,要获取更多积分往往需要产生更多消费额,而获得的积分又可以在消费时抵扣部分现金,这方面主要的代表是京东的京豆。
  • 在兴趣社区中,用户发表越多精华帖子、参与越多互动,就能获得越多积分,积分不仅与社区内的虚拟身份等级挂钩,也能用来解锁某些特权或兑换社区周边。
积分的获取

积分获取的设计主要包括两部分:一是确定哪些行为可以获取积分;二是确定积分兑换比例。
以增长黑客模型(AARRR)作为目标基础,奖励的行为可以一般分为四个维度:打开、活跃、消费、传播。

  • 打开。很多APP之所以设置签到领积分的功能,主要是因为打开是所有用户行为的基础。此外有些APP还会设置连续签到还能获得递增的积分奖励,很多游戏就采用每日签到领奖品的方式保持用户的打开习惯。比签到门槛更低的是每日登录,只要打开app,无需点击任何按钮就能获得积分。
  • 活跃。只有打开还不够,为了让新用户快速了解产品,形成用户粘性,活跃维度的积分奖励一般远高于打开。一般社区类产品,一般会将UGC相关行为设置很高的积分奖励,尤其是精华UGC内容。
  • 消费。一般电商类产品会尤其注重消费指标,消费获得的积分一般都可以方便折算出回馈比例,给消费者下次消费时的抵扣现金,用来鼓励用户复购。
  • 传播。为了提升产品自传播,鼓励用户分享平台内容,内容类产品往往会设置转发积分,让用户更有动力去分享平台优质内容,进而达到拉新效果。这方便比较有代表性的是趣头条的邀请好友模式。
为啥需要积分服务

企业一直以来的发展方式,大都离不开推陈出新、吸引新客户、拓展销售渠道,而随着企业地不断发展,企业往往会面临新品推广困难、拉新乏力、客户不活跃、复购率低等难以解决的痛点。而搭建积分商城,却可以帮助企业解决这些问题。

  1. 吸引新用户

企业可以设置多种积分获取途径,增加客户手上的积分数量,并在积分商城平台上设置多种不同类型的积分兑换方式,吸引新客户持续访问,并以新用户专享福利等方式,刺激用户完成首单转化(目的),拉动复购

  1. 激活老客户

对那些活跃度不高的老客户,可以采取积分赠送、签到送积分、发放优惠券等形式来进行唤醒,达到再次消费的目的。积分助力产生会员等级,等级为顾客带来不同权益,同时可以给消费者带来荣誉感和尊享感,满足消费者心理需求;

  1. 提升留存

企业可以借助积分这种载体,来强化客户权益,来让忠实的客户享受到一定的高折扣优惠以及积分兑换权益,对提升客户留存非常有帮助。

本文主要讲解一下两个功能设计与实现:

  1. 添加积分:添加用户积分(签到1天送10积分,连续签到2天送20积分,3天送30积分,4天以上均送50积分)
  2. 积分排行榜设计

数据库表


CREATE TABLE `t\_user\_points` (
  `id` int(11) NOT NULL AUTO\_INCREMENT,
  `fk\_user\_id` int(11) DEFAULT NULL COMMENT '用户id',
  `points` int(11) DEFAULT NULL COMMENT '积分',
  `types` int(11) DEFAULT NULL COMMENT '积分类型:0=签到,1=关注好友,2=添加评论,3=点赞商户',
  `is\_valid` int(11) DEFAULT NULL COMMENT '是否有效 1=有效,0=无效',
  `create\_date` datetime DEFAULT NULL COMMENT '创建时间',
  `update\_date` datetime DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4  COLLATE=utf8mb4_general_ci ROW_FORMAT=COMPACT;

创建 ms-points 积分微服务

pom文件引入相关依赖:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>redis-seckill</artifactId>
        <groupId>com.zjq</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>ms-points</artifactId>

    <dependencies>
        <!-- eureka client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- spring web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- spring data redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <!-- commons 公共项目 -->
        <dependency>
            <groupId>com.zjq</groupId>
            <artifactId>commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!-- test 单元测试 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

yml配置文件:

server:
  port: 7006 # 端口

spring:
  application:
    name: ms-points # 应用名
  # 数据库
  # 数据库
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root
    url: jdbc:mysql://127.0.0.1:3306/seckill?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useUnicode=true&useSSL=false
  # Redis
  redis:
    port: 6379
    host: localhost
    timeout: 3000
    password: 123456
    database: 2
  # Swagger
  swagger:
    base-package: com.zjq.points
    title: 积分功能微服务API接口文档

# 配置 Eureka Server 注册中心
eureka:
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
  client:
    service-url:
      defaultZone: http://localhost:7000/eureka/

service:
  name:
    ms-oauth-server: http://ms-oauth2-server/
    ms-users-server: http://ms-users/

mybatis:
  configuration:
    map-underscore-to-camel-case: true # 开启驼峰映射

logging:
  pattern:
    console: '%d{HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n'


REST请求配置类和Redis配置类和全局异常处理:
RestTemplate 配置类:

package com.zjq.points.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;

/\*\*
 \* RestTemplate 配置类
 \* @author zjq
 \*
 \*/
@Configuration
public class RestTemplateConfiguration {

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setSupportedMediaTypes(Collections.singletonList(MediaType.TEXT\_PLAIN));
        restTemplate.getMessageConverters().add(converter);
        return restTemplate;
    }

}


RedisTemplate配置类:

package com.zjq.points.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/\*\*
 \* RedisTemplate配置类
 \*
 \* @author zjq
 \*/
@Configuration
public class RedisTemplateConfiguration {

    /\*\*
 \* redisTemplate 序列化使用的jdkSerializeable, 存储二进制字节码, 所以自定义序列化类
 \*
 \* @param redisConnectionFactory
 \* @return
 \*/
    @Bean
    public RedisTemplate<Object, Object> 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);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

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

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

        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }


}


新增用户积分功能

用户积分实体
/\*\*
 \* 用户积分实体
 \* @author zjq
 \*/
@Getter
@Setter
public class UserPoints extends BaseModel {

    @ApiModelProperty("关联userId")
    private Integer fkUserId;
    @ApiModelProperty("积分")
    private Integer points;
    @ApiModelProperty(name = "类型",example = "0=签到,1=关注好友,2=添加Feed,3=添加商户评论")
    private Integer types;

}

积分控制层
    /\*\*
 \* 添加积分
 \*
 \* @param userId 用户ID
 \* @param points 积分
 \* @param types 类型 0=签到,1=关注好友,2=添加Feed,3=添加商户评论
 \* @return
 \*/
    @PostMapping
    public ResultInfo<Integer> addPoints(@RequestParam(required = false) Integer userId,
                                         @RequestParam(required = false) Integer points,
                                         @RequestParam(required = false) Integer types) {
        userPointsService.addPoints(userId, points, types);
        return ResultInfoUtil.buildSuccess(request.getServletPath(), points);
    }

积分业务逻辑层
    /\*\*
 \* 添加积分
 \*
 \* @param userId 用户ID
 \* @param points 积分
 \* @param types 类型 0=签到,1=关注好友,2=添加Feed,3=添加商户评论
 \*/
    @Transactional(rollbackFor = Exception.class)
    public void addPoints(Integer userId, Integer points, Integer types) {
        // 基本参数校验
        AssertUtil.isTrue(userId == null || userId < 1, "用户不能为空");
        AssertUtil.isTrue(points == null || points < 1, "积分不能为空");
        AssertUtil.isTrue(types == null, "请选择对应的积分类型");

        // 插入数据库
        UserPoints userPoints = new UserPoints();
        userPoints.setFkUserId(userId);
        userPoints.setPoints(points);
        userPoints.setTypes(types);
        userPointsMapper.save(userPoints);
    }

数据交互mapper层
    /\*\*
 \* 添加积分
 \* @param userPoints 用户积分实体
 \*/
    @Insert("insert into t\_user\_points (fk\_user\_id, points, types, is\_valid, create\_date, update\_date) " +
            " values (#{fkUserId}, #{points}, #{types}, 1, now(), now())")
    void save(UserPoints userPoints);

网关 ms-gateway 服务添加积分微服务路由
spring:
  application:
    name: ms-gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true # 开启配置注册中心进行路由功能
          lower-case-service-id: true # 将服务名称转小写
      routes:
        # 积分服务路由
        - id: ms-points
          uri: lb://ms-points
          predicates:
            - Path=/points/\*\*
          filters:
            - StripPrefix=1

用户服务中添加用户积分逻辑

在用户服务中添加 ms-points 服务的地址:


service:
  name:
    # oauth2 服务地址
    ms-oauth-server: http://ms-oauth2-server/
    # 积分服务地址
    ms-points-server: http://ms-points/

积分类型枚举 :

/\*\*
 \* 积分类型
 \* @author zjq
 \*/
@Getter
public enum PointTypesConstant {

    sign(0),
    follow(1),
    feed(2),
    review(3)
    ;

    private int type;

    PointTypesConstant(int key) {
        this.type = key;
    }

}

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

sign(0),
follow(1),
feed(2),
review(3)
;

private int type;

PointTypesConstant(int key) {
    this.type = key;
}

}




[外链图片转存中...(img-7zc9Bwd6-1714922717596)]
[外链图片转存中...(img-CmsH53st-1714922717596)]

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值