up:: 进行EurekaClient改造
说明:
SpringCloud服务与服务之间他们的调用了默认是通过 http 请求来完成的。 http 请求了其实你要是去写的话,相对而言还是比较繁杂的,你要去指定他一些参数,最后还要把这结果进行个解析,但是我们制作一使用微服的架构其实并不是希望带来更多的繁琐,而是希望能够让程序更加高效,也就是说我们的理想状态是说能够和以前一样,就仿佛是调用一个本地方法一样的去调用远程服务,那正是因为有这需求,我们Feign就诞生了。
集成Feign
引入依赖
我们的course_list是通过course_price进行调用的,所以应该给price添加依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
配置
这里的配置主要是一些负载均衡的相关配置,需要放到下一节讲解
注解
新建一个客户端
package com.imooc.course.client;
import com.imooc.course.entity.Course;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
/**
* 描述: 课程列表的Feign客户端
*/
@FeignClient("course-list")
public interface CourseListClient {
@GetMapping("/courses")
List<Course> courseList();
}
说明:通过alter+enter引入course实体
然后还要再次alter+enter直接导包,上面的步骤只是导入了模块course-list
查看下pom里添加了什么嘞?
开发controller层
@GetMapping("/coursesInPrice")
public List<Course> getCourseListInPrice(Integer courseId) {
List<Course> courses = courseListClient.courseList();
return courses;
}
说明: 注入变量
@Autowired
CourseListClient courseListClient;
测试
我们访问的是8082端口,即价格服务,但是却把课程服务内容返回了,说明服务间调用完成了!!!