职贝云数AI新零售门户

标题: Redis完成微博好友功能微服务(关注,取关,共同关注) [打印本页]

作者: ymuUw7L16pYA    时间: 2023-1-4 00:18
标题: Redis完成微博好友功能微服务(关注,取关,共同关注)
需求分析

好友功能是目前社交场景的必备功能之一,普通好友相关的功能包含有:关注/取关、我(他)的关注、我(他)的粉丝、共同关注、我关注的人也关注他等这样一些功能。

(, 下载次数: 1)
相似于这样的功能我们假如采用数据库做的话只是单纯得到用户的一些粉丝或者关注列表的话是很简单也很容易完成, 但是假如我想要查出两个甚至少个用户共同关注了哪些人或者想要查询两个或者多个用户的共同粉丝的话就会很费事, 效率也不会很高。但是假如你用redis去做的话就会相当的简单而且效率很高。缘由是redis本人本身带有专门针对于这种集合的交集、并集、差集的一些操作。

(, 下载次数: 1)
设计思绪

总体思绪我们采用MySQL + Redis的方式结合完成。MySQL次要是保存落地数据,而应用Redis的Sets数据类型停止集合操作。Sets拥有去重(我们不能多次关注同一用户)功能。一个用户我们存贮两个集合:一个是保存用户关注的人 另一个是保存关注用户的人。
数据库表设计

这个数据库表的结构比较简单,次要记录了用户id、用户关注的id和关注形态。
  1. CREATE TABLE `t_follow` (
  2.   `id` int(11) NOT NULL AUTO_INCREMENT,
  3.   `user_id` int(11) DEFAULT NULL COMMENT '当前登录用户的id',
  4.   `follow_user_id` int(11) DEFAULT NULL COMMENT '当前登录用户关注的用户的id',
  5.   `is_valid` tinyint(1) DEFAULT NULL COMMENT '关注形态,0-没有关注,1-关注了',
  6.   `create_date` datetime DEFAULT NULL,
  7.   `update_date` datetime DEFAULT NULL,
  8.   PRIMARY KEY (`id`)
  9. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='用户和用户关注表';
复制代码
新建好友功能微服务

添加依赖和配置

pom依赖如下:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.     <parent>
  6.         <artifactId>redis-seckill</artifactId>
  7.         <groupId>com.zjq</groupId>
  8.         <version>1.0-SNAPSHOT</version>
  9.     </parent>
  10.     <modelVersion>4.0.0</modelVersion>
  11.     <artifactId>ms-follow</artifactId>
  12.     <dependencies>
  13.         <!-- eureka client -->
  14.         <dependency>
  15.             <groupId>org.springframework.cloud</groupId>
  16.             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  17.         </dependency>
  18.         <!-- spring web -->
  19.         <dependency>
  20.             <groupId>org.springframework.boot</groupId>
  21.             <artifactId>spring-boot-starter-web</artifactId>
  22.         </dependency>
  23.         <!-- mysql -->
  24.         <dependency>
  25.             <groupId>mysql</groupId>
  26.             <artifactId>mysql-connector-java</artifactId>
  27.         </dependency>
  28.         <!-- spring data redis -->
  29.         <dependency>
  30.             <groupId>org.springframework.boot</groupId>
  31.             <artifactId>spring-boot-starter-data-redis</artifactId>
  32.         </dependency>
  33.         <!-- mybatis -->
  34.         <dependency>
  35.             <groupId>org.mybatis.spring.boot</groupId>
  36.             <artifactId>mybatis-spring-boot-starter</artifactId>
  37.         </dependency>
  38.         <!-- commons 公共项目 -->
  39.         <dependency>
  40.             <groupId>com.zjq</groupId>
  41.             <artifactId>commons</artifactId>
  42.             <version>1.0-SNAPSHOT</version>
  43.         </dependency>
  44.         <!-- swagger -->
  45.         <dependency>
  46.             <groupId>com.battcn</groupId>
  47.             <artifactId>swagger-spring-boot-starter</artifactId>
  48.         </dependency>
  49.     </dependencies>
  50. </project>
复制代码
springboot配置如下:
  1. server:
  2.   port: 7004 # 端口
  3. spring:
  4.   application:
  5.     name: ms-follow # 运用名
  6.   # 数据库
  7.   datasource:
  8.     driver-class-name: com.mysql.cj.jdbc.Driver
  9.     username: root
  10.     password: root
  11.     url: jdbc:mysql://127.0.0.1:3306/seckill?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useUnicode=true&useSSL=false
  12.   # Redis
  13.   redis:
  14.     port: 6379
  15.     host: localhost
  16.     timeout: 3000
  17.     password: 123456
  18.     database: 2
  19.   # Swagger
  20.   swagger:
  21.     base-package: com.zjq.follow
  22.     title: 好用功能微服务API接口文档
  23. # 配置 Eureka Server 注册中心
  24. eureka:
  25.   instance:
  26.     prefer-ip-address: true
  27.     instance-id: ${spring.cloud.client.ip-address}:${server.port}
  28.   client:
  29.     service-url:
  30.       defaultZone: http://localhost:7000/eureka/
  31. service:
  32.   name:
  33.     ms-oauth-server: http://ms-oauth2-server/
  34.     ms-diners-server: http://ms-users/
  35. mybatis:
  36.   configuration:
  37.     map-underscore-to-camel-case: true # 开启驼峰映射
  38. logging:
  39.   pattern:
  40.     console: '%d{HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n'
复制代码
添加配置类

redis配置类:
  1. package com.zjq.seckill.config;
  2. import com.fasterxml.jackson.annotation.JsonAutoDetect;
  3. import com.fasterxml.jackson.annotation.PropertyAccessor;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.core.io.ClassPathResource;
  8. import org.springframework.data.redis.connection.RedisConnectionFactory;
  9. import org.springframework.data.redis.core.RedisTemplate;
  10. import org.springframework.data.redis.core.script.DefaultRedisScript;
  11. import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
  12. import org.springframework.data.redis.serializer.StringRedisSerializer;
  13. /**
  14. * RedisTemplate配置类
  15. * @author zjq
  16. */
  17. @Configuration
  18. public class RedisTemplateConfiguration {
  19.     /**
  20.      * redisTemplate 序列化运用的jdkSerializeable, 存储二进制字节码, 所以自定义序列化类
  21.      *
  22.      * @param redisConnectionFactory
  23.      * @return
  24.      */
  25.     @Bean
  26.     public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
  27.         RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
  28.         redisTemplate.setConnectionFactory(redisConnectionFactory);
  29.         // 运用Jackson2JsonRedisSerialize 交换默许序列化
  30.         Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
  31.         ObjectMapper objectMapper = new ObjectMapper();
  32.         objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  33.         jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
  34.         // 设置key和value的序列化规则
  35.         redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
  36.         redisTemplate.setKeySerializer(new StringRedisSerializer());
  37.         redisTemplate.setHashKeySerializer(new StringRedisSerializer());
  38.         redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
  39.         redisTemplate.afterPropertiesSet();
  40.         return redisTemplate;
  41.     }
  42.    
  43. }
复制代码
REST配置类:

(, 下载次数: 1)
关注/取关完成

业务逻辑


(, 下载次数: 1)
Mapper完成

Mapper比较简单次要是查询关注信息、添加关注信息、取关或者再次关注。

(, 下载次数: 1)
Service层完成
  1. package com.zjq.seckill.service;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import com.zjq.commons.constant.ApiConstant;
  4. import com.zjq.commons.constant.RedisKeyConstant;
  5. import com.zjq.commons.exception.ParameterException;
  6. import com.zjq.commons.model.domain.ResultInfo;
  7. import com.zjq.commons.model.pojo.Follow;
  8. import com.zjq.commons.model.vo.SignInUserInfo;
  9. import com.zjq.commons.utils.AssertUtil;
  10. import com.zjq.commons.utils.ResultInfoUtil;
  11. import com.zjq.seckill.mapper.FollowMapper;
  12. import org.springframework.beans.factory.annotation.Value;
  13. import org.springframework.data.redis.core.RedisTemplate;
  14. import org.springframework.stereotype.Service;
  15. import org.springframework.web.client.RestTemplate;
  16. import javax.annotation.Resource;
  17. import java.util.LinkedHashMap;
  18. /**
  19. * 关注/取关业务逻辑层
  20. * @author zjq
  21. */
  22. @Service
  23. public class FollowService {
  24.     @Value("${service.name.ms-oauth-server}")
  25.     private String oauthServerName;
  26.     @Value("${service.name.ms-diners-server}")
  27.     private String dinersServerName;
  28.     @Resource
  29.     private RestTemplate restTemplate;
  30.     @Resource
  31.     private FollowMapper followMapper;
  32.     @Resource
  33.     private RedisTemplate redisTemplate;
  34.     /**
  35.      * 关注/取关
  36.      *
  37.      * @param followUserId 关注的食客ID
  38.      * @param isFollowed    能否关注 1=关注 0=取关
  39.      * @param accessToken   登录用户token
  40.      * @param path          访问地址
  41.      * @return
  42.      */
  43.     public ResultInfo follow(Integer followUserId, int isFollowed,
  44.                              String accessToken, String path) {
  45.         // 能否选择了关注对象
  46.         AssertUtil.isTrue(followUserId == null || followUserId < 1,
  47.                 "请选择要关注的人");
  48.         // 获取登录用户信息 (封装方法)
  49.         SignInUserInfo dinerInfo = loadSignInDinerInfo(accessToken);
  50.         // 获取当前登录用户与需求关注用户的关注信息
  51.         Follow follow = followMapper.selectFollow(dinerInfo.getId(), followUserId);
  52.         // 假如没有关注信息,且要停止关注操作 -- 添加关注
  53.         if (follow == null && isFollowed == 1) {
  54.             // 添加关注信息
  55.             int count = followMapper.save(dinerInfo.getId(), followUserId);
  56.             // 添加关注列表到 Redis
  57.             if (count == 1) {
  58.                 addToRedisSet(dinerInfo.getId(), followUserId);
  59.             }
  60.             return ResultInfoUtil.build(ApiConstant.SUCCESS_CODE,
  61.                     "关注成功", path, "关注成功");
  62.         }
  63.         // 假如有关注信息,且目前处于关注形态,且要停止取关操作 -- 取关关注
  64.         if (follow != null && follow.getIsValid() == 1 && isFollowed == 0) {
  65.             // 取关
  66.             int count = followMapper.update(follow.getId(), isFollowed);
  67.             // 移除 Redis 关注列表
  68.             if (count == 1) {
  69.                 removeFromRedisSet(dinerInfo.getId(), followUserId);
  70.             }
  71.             return ResultInfoUtil.build(ApiConstant.SUCCESS_CODE,
  72.                     "成功取关", path, "成功取关");
  73.         }
  74.         // 假如有关注信息,且目前处于取关形态,且要停止关注操作 -- 重新关注
  75.         if (follow != null && follow.getIsValid() == 0 && isFollowed == 1) {
  76.             // 重新关注
  77.             int count = followMapper.update(follow.getId(), isFollowed);
  78.             // 添加关注列表到 Redis
  79.             if (count == 1) {
  80.                 addToRedisSet(dinerInfo.getId(), followUserId);
  81.             }
  82.             return ResultInfoUtil.build(ApiConstant.SUCCESS_CODE,
  83.                     "关注成功", path, "关注成功");
  84.         }
  85.         return ResultInfoUtil.buildSuccess(path, "操作成功");
  86.     }
  87.     /**
  88.      * 添加关注列表到 Redis
  89.      *
  90.      * @param dinerId
  91.      * @param followUserId
  92.      */
  93.     private void addToRedisSet(Integer dinerId, Integer followUserId) {
  94.         redisTemplate.opsForSet().add(RedisKeyConstant.following.getKey() + dinerId, followUserId);
  95.         redisTemplate.opsForSet().add(RedisKeyConstant.followers.getKey() + followUserId, dinerId);
  96.     }
  97.     /**
  98.      * 移除 Redis 关注列表
  99.      *
  100.      * @param dinerId
  101.      * @param followUserId
  102.      */
  103.     private void removeFromRedisSet(Integer dinerId, Integer followUserId) {
  104.         redisTemplate.opsForSet().remove(RedisKeyConstant.following.getKey() + dinerId, followUserId);
  105.         redisTemplate.opsForSet().remove(RedisKeyConstant.followers.getKey() + followUserId, dinerId);
  106.     }
  107.     /**
  108.      * 获取登录用户信息
  109.      *
  110.      * @param accessToken
  111.      * @return
  112.      */
  113.     private SignInUserInfo loadSignInDinerInfo(String accessToken) {
  114.         // 必须登录
  115.         AssertUtil.mustLogin(accessToken);
  116.         String url = oauthServerName + "user/me?access_token={accessToken}";
  117.         ResultInfo resultInfo = restTemplate.getForObject(url, ResultInfo.class, accessToken);
  118.         if (resultInfo.getCode() != ApiConstant.SUCCESS_CODE) {
  119.             throw new ParameterException(resultInfo.getMessage());
  120.         }
  121.         SignInUserInfo dinerInfo = BeanUtil.fillBeanWithMap((LinkedHashMap) resultInfo.getData(),
  122.                 new SignInUserInfo(), false);
  123.         return dinerInfo;
  124.     }
  125. }
复制代码
Controller完成
  1. package com.zjq.seckill.controller;
  2. import com.zjq.commons.model.domain.ResultInfo;
  3. import com.zjq.seckill.service.FollowService;
  4. import org.springframework.web.bind.annotation.*;
  5. import javax.annotation.Resource;
  6. import javax.servlet.http.HttpServletRequest;
  7. /**
  8. * 关注/取关控制层
  9. * @author zjq
  10. */
  11. @RestController
  12. public class FollowController {
  13.     @Resource
  14.     private FollowService followService;
  15.     @Resource
  16.     private HttpServletRequest request;
  17.     /**
  18.      * 关注/取关
  19.      *
  20.      * @param followUserId 关注的用户ID
  21.      * @param isFollowed    能否关注 1=关注 0=取消
  22.      * @param access_token  登录用户token
  23.      * @return
  24.      */
  25.     @PostMapping("/{followUserId}")
  26.     public ResultInfo follow(@PathVariable Integer followUserId,
  27.                              @RequestParam int isFollowed,
  28.                              String access_token) {
  29.         ResultInfo resultInfo = followService.follow(followUserId,
  30.                 isFollowed, access_token, request.getServletPath());
  31.         return resultInfo;
  32.     }
  33. }
复制代码
网关配置路由规则
  1. spring:
  2.   application:
  3.     name: ms-gateway
  4.   cloud:
  5.     gateway:
  6.       discovery:
  7.         locator:
  8.           enabled: true # 开启配置注册中心停止路由功能
  9.           lower-case-service-id: true # 将服务称号转小写
  10.       routes:
  11.          
  12.          # 好友功能微服务
  13.         - id: ms-follow
  14.           uri: lb://ms-follow
  15.           predicates:
  16.             - Path=/follow/**
  17.           filters:
  18.             - StripPrefix=1
复制代码
测实验证

依次启动,注册中心、网关、认证中心、好友功能微服务。
测试id为5的用户,关注id为1的用户。

(, 下载次数: 1)
查看redis可以看到有两个集合,一个粉丝集合,一个关注集合。

(, 下载次数: 1)
查看数据库,id为5的用户关注了id为1的用户

(, 下载次数: 1)
让id等于7的用户关注id等于1的用户,redis和数据库存储信息如下:

(, 下载次数: 1)
共同关注列表

Controller添加方法
  1.     /**
  2.      * 共同关注列表
  3.      *
  4.      * @param userId
  5.      * @param access_token
  6.      * @return
  7.      */
  8.     @GetMapping("commons/{userId}")
  9.     public ResultInfo findCommonsFriends(@PathVariable Integer userId,
  10.                                          String access_token) {
  11.         return followService.findCommonsFriends(userId, access_token, request.getServletPath());
  12.     }
复制代码
Service添加方法
  1.     /**
  2.      * 共同关注列表
  3.      *
  4.      * @param userId
  5.      * @param accessToken
  6.      * @param path
  7.      * @return
  8.      */
  9.     @Transactional(rollbackFor = Exception.class)
  10.     public ResultInfo findCommonsFriends(Integer userId, String accessToken, String path) {
  11.         // 能否选择了查看对象
  12.         AssertUtil.isTrue(userId == null || userId < 1,
  13.                 "请选择要查看的人");
  14.         // 获取登录用户信息
  15.         SignInUserInfo userInfo = loadSignInuserInfo(accessToken);
  16.         // 获取登录用户的关注信息
  17.         String loginuserKey = RedisKeyConstant.following.getKey() + userInfo.getId();
  18.         // 获取登录用户查看对象的关注信息
  19.         String userKey = RedisKeyConstant.following.getKey() + userId;
  20.         // 计算交集
  21.         Set<Integer> userIds = redisTemplate.opsForSet().intersect(loginuserKey, userKey);
  22.         // 没有
  23.         if (userIds == null || userIds.isEmpty()) {
  24.             return ResultInfoUtil.buildSuccess(path, new ArrayList<ShortUserInfo>());
  25.         }
  26.         // 调用食客服务根据 ids 查询食客信息
  27.         ResultInfo resultInfo = restTemplate.getForObject(usersServerName + "findByIds?access_token={accessToken}&ids={ids}",
  28.                 ResultInfo.class, accessToken, StrUtil.join(",", userIds));
  29.         if (resultInfo.getCode() != ApiConstant.SUCCESS_CODE) {
  30.             resultInfo.setPath(path);
  31.             return resultInfo;
  32.         }
  33.         // 处理结果集
  34.         List<LinkedHashMap> dinnerInfoMaps = (ArrayList) resultInfo.getData();
  35.         List<ShortUserInfo> userInfos = dinnerInfoMaps.stream()
  36.                 .map(user -> BeanUtil.fillBeanWithMap(user, new ShortUserInfo(), true))
  37.                 .collect(Collectors.toList());
  38.         return ResultInfoUtil.buildSuccess(path, userInfos);
  39.     }
复制代码
用户服务新增根据ids查询用户集合

Controller:
  1.     /**
  2.      * 根据 ids 查询用户信息
  3.      *
  4.      * @param ids
  5.      * @return
  6.      */
  7.     @GetMapping("findByIds")
  8.     public ResultInfo<List<ShortUserInfo>> findByIds(String ids) {
  9.         List<ShortUserInfo> dinerInfos = userService.findByIds(ids);
  10.         return ResultInfoUtil.buildSuccess(request.getServletPath(), dinerInfos);
  11.     }
复制代码
Service:
  1.     /**
  2.      * 根据 ids 查询食客信息
  3.      *
  4.      * @param ids 主键 id,多个以逗号分隔,逗号之间不用空格
  5.      * @return
  6.      */
  7.     public List<ShortUserInfo> findByIds(String ids) {
  8.         AssertUtil.isNotEmpty(ids);
  9.         String[] idArr = ids.split(",");
  10.         List<ShortUserInfo> dinerInfos = usersMapper.findByIds(idArr);
  11.         return dinerInfos;
  12.     }
复制代码
Mapper:
  1.     /**
  2.      * 根据 ID 集合查询多个食客信息
  3.      * @param ids
  4.      * @return
  5.      */
  6.     @Select("<script> " +
  7.             " select id, nickname, avatar_url from t_diners " +
  8.             " where is_valid = 1 and id in " +
  9.             " <foreach item=\"id\" collection=\"ids\" open=\"(\" separator=\",\" close=\")\"> " +
  10.             "   #{id} " +
  11.             " </foreach> " +
  12.             " </script>")
  13.     List<ShortUserInfo> findByIds(@Param("ids") String[] ids);
复制代码
下面测试曾经让id5和7的用户关注了id为1的用户,我们继续让id5的用户关注id为3的用户,让id5、6、7的用户关注了id为2的用户:
redis和数据库信息如下:

(, 下载次数: 1)

(, 下载次数: 1)

(, 下载次数: 1)

(, 下载次数: 1)
测实验证

查询当前登录用户id为5和id为7的共同关注信息:

(, 下载次数: 1)
查询当前登录用户id为6和id为7的共同关注信息:

(, 下载次数: 1)
可以看出来5和7共同关注了1和2,6和7只共同关注了2,符合预期。
本文内容到此结束了
如有播种欢迎点赞
您的鼓励是我最大的动力
如有错误❌疑问 欢迎各位指出





欢迎光临 职贝云数AI新零售门户 (https://www.taojin168.com/cloud/) Powered by Discuz! X3.5