up:: 利用Hystrix实现断路器
说明:
想把我们从另一个服务所拿到的这个信息进行整合,也就是开发出一个课程列表加价格接口。这个接口呢?是融合双方信息。 工作中经常要这么做。
controller层
@GetMapping("coursesAndPrice")
public List<CourseAndPrice> getCoursesAndPrice() {
List<CourseAndPrice> coursesAndPrice = coursePriceService.getCoursesAndPrice();
return coursesAndPrice;
}
service层
@Override
public List<CourseAndPrice> getCoursesAndPrice() {
List<CourseAndPrice> courseAndPriceList = new ArrayList<>();
List<Course> courses = courseListClient.courseList();
for (int i = 0; i < courses.size(); i++) {
Course course = courses.get(i);
if (course != null) {
CoursePrice coursePrice = getCoursePrice(course.getCourseId());
CourseAndPrice courseAndPrice = new CourseAndPrice();
courseAndPrice.setPrice(coursePrice.getPrice());
courseAndPrice.setName(course.getCourseName());
courseAndPrice.setId(course.getId());
courseAndPrice.setCourseId(course.getCourseId());
courseAndPriceList.add(courseAndPrice);
}
}
return courseAndPriceList;
}
反向生成接口
List<CourseAndPrice> getCoursesAndPrice();
注入实体对象
@Autowired
CourseListClient courseListClient;
entity实体类
package com.imooc.course.entity;
import java.io.Serializable;
/**
* 描述: 课程与价格的融合类
*/
public class CourseAndPrice implements Serializable {
Integer id;
Integer courseId;
String name;
Integer price;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getCourseId() {
return courseId;
}
public void setCourseId(Integer courseId) {
this.courseId = courseId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
}
说明: 没啥好讲的,做过很多遍了。。。