15.今日佳人
今日佳人
一、服务提供者环境搭建(tanhua-dubbo-mongo)
1.1编写配置信息application.yml
server:
port: 8082
spring:
application:
name: tanhua-dubbo-mongo #服务名称
cloud: #nacos地址
nacos:
discovery:
server-addr: 192.168.136.160:8848
data: #MongoDB地址
mongodb:
uri: mongodb://192.168.136.160:27017/tanhua
dubbo: #dubbo配置
protocol:
name: dubbo
port: 20882
registry:
address: spring-cloud://localhost
scan:
base-packages: com.tanhua.dubbo.api #dubbo中包扫描
1.2编写启动类
package com.tanhua.dubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DubboMongoApplication {
public static void main(String[] args) {
SpringApplication.run(DubboMongoApplication.class,args);
}
}
1.3编写实体类(配置注解 @Document,@Id)
package com.tanhua.model.mongo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.mapping.Document;
/**
* 今日佳人
*/
@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; //日期
}
1.4RecommendUserApi
package com.tanhua.dubbo.api;
import com.tanhua.model.mongo.RecommendUser;
//查询今日佳人接口
public interface RecommendUserApi {
RecommendUser queryUserIdMaxScore(Long toUserId);
}
1.5RecommendUserApiImpl
(这个实现类的名字要和dubbo包扫描一致,不然报org.apache.dubbo.rpc.RpcException: No provider available from registry localhost:9090 for service)消费者的配置文件中也要订阅此服务:不然也会报这个错
dubbo:
cloud:
subscribed-services: 'tanhua-dubbo-db,tanhua-dubbo-mongo'
package com.tanhua.dubbo.api;
import com.tanhua.model.mongo.RecommendUser;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
/**
* 查找今日佳人实现类
*/
@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {
@Autowired
private MongoTemplate mongoTemplate;
@Override
public RecommendUser queryUserIdMaxScore(Long toUserId) {
//1.构造查询条件,查询与toUserId这个操作用户缘分最高的用户
Criteria criteria =Criteria.where("toUserId").is(toUserId);
//2.创建query对象
Query query = Query.query(criteria);
query.with(Sort.by(Sort.Order.desc("score")))
.limit(1);
RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
return recommendUser;
}
}
二、服务消费者
2.1TanHuaController
package com.tanhua.server.controller;
import com.tanhua.model.vo.TodayBest;
import com.tanhua.server.service.TanHuaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/tanhua")
public class TanHuaController {
@Autowired
private TanHuaService tanHuaService;
/**
* 查询今日佳人
* 请求路径:/tanhua/todayBest
* 请求方式:get
* 响应数据:TodayBest对象
*/
@GetMapping("/todayBest")
public ResponseEntity queryTodayBest(){
TodayBest vo = tanHuaService.queryToDayBest();
return ResponseEntity.ok(vo);
}
}
2.2TanHuaService
package com.tanhua.server.service;
import com.tanhua.dubbo.api.RecommendUserApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.RecommendUser;
import com.tanhua.model.vo.TodayBest;
import com.tanhua.server.interceptor.ThreadLocalUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
@Service
public class TanHuaService {
@DubboReference
private RecommendUserApi recommendUserApi;
@DubboReference
private UserInfoApi userInfoApi;
/**
*
*查询今日佳人
*/
public TodayBest queryToDayBest() {
//1.获取用户id
Long userId = ThreadLocalUtils.getUserId();
//2.根据用户id查询今日佳人
RecommendUser recommendUser = recommendUserApi.queryUserIdMaxScore(userId);
if(recommendUser == null){
recommendUser = new RecommendUser();
recommendUser.setUserId(1L);
recommendUser.setScore(90D);
}
//3.根据查询到的今日佳人的用户id查询用户详情
Long toDayBestId = recommendUser.getUserId();//得到今日佳人的用户id
UserInfo userInfo = userInfoApi.selectUserInfo(toDayBestId);//得到今日佳人详情
TodayBest todayBest = TodayBest.init(userInfo, recommendUser);
return todayBest;
}
}