请查看使用 RestTemplate 的控制器(添加评论)的以下代码:
@GetMapping("/{courseid}")
public Course getCourseDetails(@PathVariable Long courseid) {
// Get Course info (ID, Name, Description) from pre-populated Array List
CourseInfo courseInfo = getCourseInfo(courseid);
// Get Price info of a course from another microservice using RESTTemplate
Price price = restTemplate.getForObject("http://localhost:8002/price/"+courseid, Price.class);
// Get enrollment info of a course from another microservice using RESTTemplate
Enrollment enrollment = restTemplate.getForObject("http://localhost:8003/enrollment/"+courseid, Enrollment.class);
//Consolidate everything in to Course object and send it as response
return new Course(courseInfo.getCourseID(), courseInfo.getCourseName(), courseInfo.getCourseDesc(), price.getDiscountedPrice(),
enrollment.getEnrollmentOpen());
}
现在我正在尝试使用反应式编程来实现相同的目标。我现在使用来自 Web-Flux 的 WebClient 和 Mono。但是,我很困惑如何组合结果?看看下面的代码(只是使用 Mono Everywhere。其余代码保持不变)
@GetMapping("/{courseid}")
public Mono<Course> getCourseDetails(@PathVariable Long courseid) {
// Get Course info (ID, Name, Description) from pre-populated Array List
CourseInfo courseInfo = getCourseInfo(courseid);
// Get Price info of a course from another microservice using RESTTemplate
Mono<Price> price = webClient.get().uri("http://localhost:8002/price/{courseid}/",courseid).retrieve().bodyToMono(Price.class);
// Get enrollment info of a course from another microservice using RESTTemplate
Mono<Enrollment> inventory = webClient.get().uri("http://localhost:8003/enrollment/{courseid}/",courseid).retrieve().bodyToMono(Enrollment.class);
//Question : How do we Consolidate everything and form a Mono<Course> object and send it as response?
}
问题 1:我们如何合并所有内容并形成 Mono 对象并将其作为响应发送?
问题2:语句“CourseInfo courseInfo = getCourseInfo(courseid);”是否 导致阻塞操作?
谢谢!