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