Controller實(shí)現(xiàn)package com.zjq.seckill.controller;import com.zjq.commons.model.domain.ResultInfo;import com.zjq.seckill.service.FollowService;import org.springframework.web.bind.annotation.*;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;/** * 關(guān)注/取關(guān)控制層 * @author zjq */@RestControllerpublic class FollowController {@Resourceprivate FollowService followService;@Resourceprivate HttpServletRequest request;/*** 關(guān)注/取關(guān)** @param followUserId 關(guān)注的用戶ID* @param isFollowed是否關(guān)注 1=關(guān)注 0=取消* @param access_token登錄用戶token* @return*/@PostMapping("/{followUserId}")public ResultInfo follow(@PathVariable Integer followUserId,@RequestParam int isFollowed,String access_token) {ResultInfo resultInfo = followService.follow(followUserId,isFollowed, access_token, request.getServletPath());return resultInfo;}}網(wǎng)關(guān)配置路由規(guī)則spring:application:name: ms-gatewaycloud:gateway:discovery:locator:enabled: true # 開(kāi)啟配置注冊(cè)中心進(jìn)行路由功能lower-case-service-id: true # 將服務(wù)名稱轉(zhuǎn)小寫(xiě)routes:# 好友功能微服務(wù)- id: ms-followuri: lb://ms-followpredicates:- Path=/follow/**filters:- StripPrefix=1測(cè)試驗(yàn)證依次啟動(dòng),注冊(cè)中心、網(wǎng)關(guān)、認(rèn)證中心、好友功能微服務(wù) 。
測(cè)試id為5的用戶,關(guān)注id為1的用戶 。


【微博不能關(guān)注怎么回事20566 微博不能關(guān)注人了是怎么回事】查看redis可以看到有兩個(gè)集合,一個(gè)粉絲集合 , 一個(gè)關(guān)注集合 。

查看數(shù)據(jù)庫(kù),id為5的用戶關(guān)注了id為1的用戶


共同關(guān)注列表從Redis中讀取登錄用戶的關(guān)注列表與查看用戶的關(guān)注列表,然后進(jìn)行交集操作 , 獲取共同關(guān)注的用戶id
然后通過(guò)用戶服務(wù)傳入用戶id數(shù)據(jù)獲取用戶基本信息
Controller添加方法
/*** 共同關(guān)注列表** @param userId* @param access_token* @return*/@GetMapping("commons/{userId}")public ResultInfo findCommonsFriends(@PathVariable Integer userId,String access_token) {return followService.findCommonsFriends(userId, access_token, request.getServletPath());}Service添加方法/*** 共同關(guān)注列表** @param userId* @param accessToken* @param path* @return*/@Transactional(rollbackFor = Exception.class)public ResultInfo findCommonsFriends(Integer userId, String accessToken, String path) {// 是否選擇了查看對(duì)象AssertUtil.isTrue(userId == null || userId < 1,"請(qǐng)選擇要查看的人");// 獲取登錄用戶信息SignInUserInfo userInfo = loadSignInuserInfo(accessToken);// 獲取登錄用戶的關(guān)注信息String loginuserKey = RedisKeyConstant.following.getKey() + userInfo.getId();// 獲取登錄用戶查看對(duì)象的關(guān)注信息String userKey = RedisKeyConstant.following.getKey() + userId;// 計(jì)算交集Set<Integer> userIds = redisTemplate.opsForSet().intersect(loginuserKey, userKey);// 沒(méi)有if (userIds == null || userIds.isEmpty()) {return ResultInfoUtil.buildSuccess(path, new ArrayList<ShortUserInfo>());}// 調(diào)用食客服務(wù)根據(jù) ids 查詢食客信息ResultInfo resultInfo = restTemplate.getForObject(usersServerName + "findByIds?access_token={accessToken}&ids={ids}",ResultInfo.class, accessToken, StrUtil.join(",", userIds));if (resultInfo.getCode() != ApiConstant.SUCCESS_CODE) {resultInfo.setPath(path);return resultInfo;}// 處理結(jié)果集List<LinkedHashMap> dinnerInfoMaps = (ArrayList) resultInfo.getData();List<ShortUserInfo> userInfos = dinnerInfoMaps.stream().map(user -> BeanUtil.fillBeanWithMap(user, new ShortUserInfo(), true)).collect(Collectors.toList());return ResultInfoUtil.buildSuccess(path, userInfos);}
推薦閱讀
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- unny是什么檔次的產(chǎn)品 unny是什么
- 紅酒打開(kāi)多久就不能喝了變質(zhì)
- 龍井茶為什么不能二泡
- 紅酒開(kāi)瓶后能放多久不能喝有什么用
- 減肥哪些碳水不能吃 減肥不能吃的碳水列表
- 羊奶好還是牛奶好,羊奶為什么不能長(zhǎng)期喝
- 微信不能轉(zhuǎn)賬是怎么回事 微信自動(dòng)支付的在哪里取消
- 為什么家里不能養(yǎng)水仙花的原因 為什么家里不能養(yǎng)水仙花
- 離線緩存的視頻怎么保存到本地 緩存的視頻怎么保存到本地
- 駝乳粉什么時(shí)間喝最好,駝乳粉什么人不能用
