zuul学习一:spring cloud zuul的快速入门
ZuulRoute 是 ZuulProperties 的一个内部类

前提:通过这段代码来实现动态路由刷新
for (RespRouteDto respRouteDto : respRouteDtos) {
routes.put(respRouteDto.getId(), new ZuulProperties.ZuulRoute(respRouteDto.getPath(), respRouteDto.getLocation()));
}
进入 ZuulRoute
public ZuulRoute(String path, String location) {
this.id = this.extractId(path);
this.path = path;
this.setLocation(location);
}
setLocation()
public void setLocation(String location) {
if (location == null || !location.startsWith("http:") && !location.startsWith("https:")) {
this.serviceId = location;
} else {
this.url = location;
}
}
发现上面的代码 serviceId 的值为location
总结: 其实上面的代码是通过编码的形式配置 路径 和 服务实例名 的映射关系
ext:通过配置文件的方式配置映射关系
zuul:
routes:
user-service:
path: /users/**
serviceId: user-service
当访问路径 /user/** 时,即访问 user-service 的服务接口
本文深入探讨了Spring Cloud Zuul的路由配置机制,详细解释了如何通过编码和配置文件方式映射路径到服务实例,重点讲解了ZuulRoute类的使用及动态路由刷新的实现。
984

被折叠的 条评论
为什么被折叠?



