5、今日佳人
在用户登录成功后,就会进入首页,首页中有今日佳人、推荐好友、探花、搜附近等功能。
今日佳人,会推荐缘分值最大的用户,进行展现出来。缘分值的计算是由用户的行为进行打分,如:点击、点赞、评论、学历、婚姻状态等信息组合而成的。
实现:我们先不考虑推荐的逻辑,假设现在已经有推荐的结果,我们只需要从结果中查询到缘分值最高的用户就可以了。至于推荐的逻辑以及实现,我们将后面的课程中讲解。
流程:
5.1、表结构设计
1. #表结构 recommend_user 2. { 3. "userId":1001, #推荐的用户id 4. "toUserId":1002, #用户id 5. "score":90, #推荐得分 6. "date":"2019/1/1" #日期 7. }
在MongoDB中只存储用户的id数据,其他的数据需要通过接口查询。
5.2、服务提供者
5.2.0、导入依赖
找到tanhua-domain模块的pom.xml
打开mongo的依赖
5.2.1、实体类
1. @AllArgsConstructor 2. @NoArgsConstructor 3. @Data 4. @Document(collection = "recommend_user") 5. public class RecommendUser implements java.io.Serializable { 6. private ObjectId id; //主键id 7. private Long userId; //推荐的用户id 8. private Long toUserId; //用户id 9. private Double score =0d; //推荐得分 10. private String date; //日期 11. }
5.2.2、RecommendUserApi接口
1. public interface RecommendUserApi { 2. 3. RecommendUser queryWithMaxScore(Long toUserId); 4. }
5.2.3、RecommendUserApiImpl
1. @DubboService 2. public class RecommendUserApiImpl implements RecommendUserApi { 3. 4. @Autowired 5. private MongoTemplate mongoTemplate; 6. 7. //查询今日佳人 8. public RecommendUser queryWithMaxScore(Long toUserId) { 9. 10. //根据toUserId查询,根据评分score排序,获取第一条 11. //构建Criteria 12. Criteria criteria = Criteria.where("toUserId").is(toUserId); 13. //构建Query对象 14. Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score"))) 15. .limit(1); 16. //调用mongoTemplate查询 17. 18. return mongoTemplate.findOne(query,RecommendUser.class); 19. } 20. }
5.2.4、application配置
1. server: 2. port: 18082 3. spring: 4. application: 5. name: tanhua-dubbo-mongo 6. cloud: 7. nacos: 8. discovery: 9. server-addr: 192.168.136.160:8848 10. data: 11. mongodb: 12. uri: mongodb://192.168.136.160:27017/tanhua 13. dubbo: 14. protocol: 15. name: dubbo 16. port: 20882 17. registry: 18. address: spring-cloud://localhost 19. scan: 20. base-packages: com.tanhua.dubbo.api #dubbo中包扫描
5.2.5 启动类
1. package com.tanhua.dubbo; 2. 3. import com.baomidou.mybatisplus.annotation.DbType; 4. import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; 5. import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; 6. import org.mybatis.spring.annotation.MapperScan; 7. import org.springframework.boot.SpringApplication; 8. import org.springframework.boot.autoconfigure.SpringBootApplication; 9. import org.springframework.context.annotation.Bean; 10. 11. @SpringBootApplication 12. public class DubboMongoApplication { 13. 14. public static void main(String[] args) { 15. SpringApplication.run(DubboMongoApplication.class,args); 16. } 17. }
5.3、代码实现
5.3.1、接口说明
地址:http://192.168.136.160:3000/project/19/interface/api/100
5.3.2、TanhuaController
1. @RestController 2. @RequestMapping("/tanhua") 3. public class TanhuaController { 4. 5. @Autowired 6. private TanhuaService tanhuaService; 7. 8. //今日佳人 9. @GetMapping("/todayBest") 10. public ResponseEntity todayBest() { 11. TodayBest vo = tanhuaService.todayBest(); 12. return ResponseEntity.ok(vo); 13. } 14. }
5.3.3、TanhuaService
1. @Service 2. public class TanhuaService { 3. 4. @DubboReference 5. private RecommendUserApi recommendUserApi; 6. 7. @DubboReference 8. private UserInfoApi userInfoApi; 9. 10. @DubboReference 11. private QuestionApi questionApi; 12. 13. @Autowired 14. private HuanXinTemplate template; 15. 16. //查询今日佳人数据 17. public TodayBest todayBest() { 18. //1、获取用户id 19. Long userId = UserHolder.getUserId(); 20. //2、调用API查询 21. RecommendUser recommendUser = recommendUserApi.queryWithMaxScore(userId); 22. if(recommendUser == null) { 23. recommendUser = new RecommendUser(); 24. recommendUser.setUserId(1l); 25. recommendUser.setScore(99d); 26. } 27. //3、将RecommendUser转化为TodayBest对象 28. UserInfo userInfo = userInfoApi.findById(recommendUser.getUserId()); 29. TodayBest vo = TodayBest.init(userInfo, recommendUser); 30. //4、返回 31. return vo; 32. } 33. }
5.3.4、vo对象
1. package com.tanhua.domain.vo; 2. 3. import com.tanhua.domain.db.UserInfo; 4. import com.tanhua.domain.mongo.RecommendUser; 5. import lombok.AllArgsConstructor; 6. import lombok.Data; 7. import lombok.NoArgsConstructor; 8. import org.springframework.beans.BeanUtils; 9. 10. /** 11. * 今日佳人 12. */ 13. @Data 14. @NoArgsConstructor 15. @AllArgsConstructor 16. public class TodayBest { 17. 18. private Long id; //用户id 19. private String avatar; 20. private String nickname; 21. private String gender; //性别 man woman 22. private Integer age; 23. private String[] tags; 24. private Long fateValue; //缘分值 25. 26. /** 27. * 在vo对象中,补充一个工具方法,封装转化过程 28. */ 29. public static TodayBest init(UserInfo userInfo, RecommendUser recommendUser) { 30. TodayBest vo = new TodayBest(); 31. BeanUtils.copyProperties(userInfo,vo); 32. if(userInfo.getTags() != null) { 33. vo.setTags(userInfo.getTags().split(",")); 34. } 35. vo.setFateValue(recommendUser.getScore().longValue()); 36. return vo; 37. } 38. }
5.3.5、解决MongoDB启动bug
在项目中,添加了mongo的依赖的话,springboot就会自动去连接本地的mongo,由于他连接不上会导致出错。
解决有2种方案:
- 排除掉mongo的依赖
- springboot中添加排除自动配置的注解
1. package com.tanhua.server; 2. 3. import org.springframework.boot.SpringApplication; 4. import org.springframework.boot.autoconfigure.SpringBootApplication; 5. import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration; 6. import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; 7. 8. @SpringBootApplication(exclude = { 9. MongoAutoConfiguration.class, 10. MongoDataAutoConfiguration.class 11. }) //排除mongo的自动配置 12. public class TanhuaServerApplication { 13. 14. public static void main(String[] args) { 15. SpringApplication.run(TanhuaServerApplication.class,args); 16. } 17. }