知识点 | 核心内容 | 考试重点/易混淆点 | 难度系数 |
---|---|---|---|
Spring Cloud简介 | Spring Cloud的基本概念、定位、成长路径和家族组件 | Spring Cloud的定位和成长路径 | |
项目实践 | 通过课程查询项目学习Spring Cloud组件,包括模块间调用、断路器、网关等 | Spring Cloud核心思想和组件应用 | |
服务注册与发现 | 使用Eureka实现服务的自动注册与发现 | Eureka Server和Client的配合使用 | |
负载均衡 | 使用Ribbon实现不同的负载均衡算法 | 负载均衡算法的选择和应用 | |
断路器 | 使用Hystrix实现断路功能,保证服务稳定性 | 断路器的工作原理和实现 | |
服务网关 | 使用Zuul实现统一入口和安全管理 | 网关的作用和过滤器的使用 |
com.mukewang
)。spring-cloud-course
)。course-list
模块,用于存放业务代码。course-price
模块。知识点 | 核心内容 | 考试重点/易混淆点 | 难度系数 |
---|---|---|---|
项目设计 | 项目整体设计包括项目介绍、接口设计、数据流向和表设计 | 数据流向和表设计的具体实现 | |
接口设计 | 课程列表和价格模块的接口设计,特别是整合接口的远程调用和数据处理 | 远程调用的实现和数据整合的难点 | |
表设计 | course表和课程价格表的结构及关联关系 | 表之间的关联和valid字段的使用 | |
新建项目 | 使用Spring Initializr创建多模块项目,配置基本信息和依赖 | 项目初始化进度和文件夹结构的确认 | |
子模块创建 | 删除src文件夹,新建子模块并确认层级结构 | 子模块的继承关系和POM文件的自动更新 |
# Spring Cloud课程列表与价格模块开发笔记
## 一、课程列表模块开发
### 1.1 新建包
- **位置**: `src/main/java`
- **包名**: `com.Imock.course`
### 1.2 写主启动类
- **类名**: `CourseListApplication`
- **注解**: `@SpringBootApplication`
- **主函数**:
```java
public static void main(String[] args) {
SpringApplication.run(CourseListApplication.class, args);
}
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.1version>
dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
application.properties
server.port=8081
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/course_practice
spring.datasource.username=root
spring.datasource.password=12345678
spring.application.name=course-list
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n
CourseListController
@RestController
@GetMapping("/courses")
public List<Course> crossList() {
return courseListService.getCourseList();
}
CourseListService
public interface CourseListService {
List<Course> getCourseList();
}
CourseListServiceImpl
@Service
public class CourseListServiceImpl implements CourseListService {
@Autowired
private CourseMapper courseMapper;
@Override
public List<Course> getCourseList() {
return courseMapper.findValidCourses();
}
}
CourseMapper
@Mapper
@Repository
public interface CourseMapper {
@Select("SELECT * FROM course WHERE valid = 1")
List<Course> findValidCourses();
}
Course
public class Course implements Serializable {
private Integer id;
private Integer courseID;
private String courseName;
private Integer valid;
// Getters and Setters
}
Serializable
接口。com.Imock.course
course-price
CoursePriceController
@RestController
public class CoursePriceController {
@Autowired
private CoursePriceService coursePriceService;
@GetMapping("/price")
public Integer getCoursePrice(@RequestParam Integer courseID) {
CoursePrice coursePrice = coursePriceService.getCoursePrice(courseID);
return coursePrice.getPrice();
}
}
CoursePriceService
public interface CoursePriceService {
CoursePrice getCoursePrice(Integer courseID);
}
CoursePriceServiceImpl
@Service
public class CoursePriceServiceImpl implements CoursePriceService {
@Autowired
private CoursePriceMapper coursePriceMapper;
@Override
public CoursePrice getCoursePrice(Integer courseID) {
return coursePriceMapper.findCoursePrice(courseID);
}
}
CoursePriceMapper
@Mapper
@Repository
public interface CoursePriceMapper {
@Select("SELECT * FROM course_price WHERE course_id = #{courseID}")
CoursePrice findCoursePrice(@Param("courseID") Integer courseID);
}
CoursePrice
public class CoursePrice implements Serializable {
private Integer id;
private Integer courseID;
private Integer price;
// Getters and Setters
}
CoursePriceApplication
@SpringBootApplication
public class CoursePriceApplication {
public static void main(String[] args) {
SpringApplication.run(CoursePriceApplication.class, args);
}
}
eureka-server
,区别于Eureka Client。<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
dependency>
标签统一管理版本。application.properties
server.port=8000
spring.application.name=eureka-server
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
http://localhost:8000
,查看Eureka管理界面。<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
application.properties
eureka.client.service-url.defaultZone=http://localhost:8000/eureka/
application.properties
eureka.client.service-url.defaultZone=http://localhost:8000/eureka/
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
@EnableFeignClients
。CourseListClient
接口。application.properties
中设置。loadbalance.rule
。RoundRobinRule
。<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-hystrixartifactId>
dependency>
feign.hystrix.enabled=true
。@EnableCircuitBreaker
。CourseListClient
接口,提供默认课程列表。@Primary
注解解决组件冲突。GET /courses-and-price
。entity
包。CourseAndPrice
。CoursePriceService
中新增方法。getCoursesAndPrice
。CourseAndPrice
对象。以上内容总结了Spring Cloud中服务间调用、负载均衡与断路器整合的核心知识点,为后续的微服务架构开发提供了坚实的基础。
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-zuulartifactId>
dependency>
zuul.routes.course-list.path=/list/**
zuul.routes.course-list.service-id=course-list
@SpringBootApplication
@EnableZuulProxy
/imooc
)。zuul.prefix=/imooc
zuul.routes.course-list.path=/list/**
zuul.routes.course-list.service-id=course-list
@Component
ZuulFilter
filterType()
: 返回FilterConstants.PRE_TYPE
。filterOrder()
: 指定执行顺序(如5)。shouldFilter()
: 返回true
表示所有请求都经过此过滤器。run()
: 实现具体逻辑,如打印请求URL。filterType()
返回FilterConstants.POST_TYPE
。SEND_RESPONSE_FILTER_ORDER - 1
。知识点 | 核心内容 | 考试重点/易混淆点 | 难度系数 |
---|---|---|---|
网关作用 | 统一身份验证和安全处理 | 网关如何统一处理身份验证和安全问题 | |
网关集成 | 注册到Eureka,引入依赖,配置路由地址 | 路由地址配置的方法和个性化设置 | |
过滤器类型 | pre、post、error过滤器及其使用场景 | 区分pre和post的使用场景 | |
前置过滤器编写 | 使用@Component 注解,继承ZuulFilter ,实现抽象方法 |
过滤器类别指定和shouldFilter 方法的实现 |
|
后置过滤器编写 | 修改过滤器类型为post,调整执行顺序,实现具体逻辑 | 过滤器类别修改和返回状态码的获取 | |
过滤器执行顺序 | 数字越大执行越晚,pre先执行,post后执行 | 理解过滤器执行顺序的重要性 | |
实际测试 | 启动服务,访问接口,验证过滤器是否按预期工作 | 验证过滤器是否正确打印URL和状态码 |
以上内容总结了Spring Cloud Zuul网关的核心知识点,为后续的微服务架构开发提供了坚实的基础。