目录
-
Zuul配置
-
在mysql中创建路由信息表
-
定义CustomRouteLocator类
-
增加CustomZuulConfig类,主要是为了配置CustomRouteLocator
-
RefreshRouteService类,用于实现数据库路由信息的刷新
-
当然也要提供RefreshController,提供从浏览器访问的刷新功能
-
问题
-
后记
Zuul 是 Netflix 开源的微服务网关,Spring Cloud 对 Zuul 进行了整合和增强。在 SpringCloud 体系中,Zuul 担任着网关的角色,对发送到服务端的请求进行一些预处理,比如安全验证、动态路由、负载分配等。
还是那句话,由于水平有限,难免有不当或者错误之处,请大家指正,谢谢。
Zuul配置
一般的,我们如果使用Spring Cloud Zuul 进行路由配置,类似于下面的样子:
-
zuul: -
routes: -
users: -
path: /myusers/** -
stripPrefix: false
当我们要新增或者改变一个网关路由时,我们不得不停止网关服务,修改配置文件,保存再重新启动网关服务,这样才能让我们新的设置生效。
设想一样,如果是在生产环境,为了一个小小的路由变更,这样的停止再重启恐怕谁也受不了吧。接下来,看看我们怎么能做到动态配置网关路由,让网关路由配置在服务不需要重启的情况生效。(废话一堆啊)
在mysql中创建路由信息表,对于类如下:
-
public static class ZuulRouteVO { -
-
/** -
* The ID of the route (the same as its map key by default). -
*/ -
private String id; -
-
/** -
* The path (pattern) for the route, e.g. /foo/**. -
*/ -
private String path; -
-
/** -
* The service ID (if any) to map to this route. You can specify a physical URL or -
* a service, but not both. -
*/ -
private String serviceId; -
-
/** -
* A full physical URL to map to the route. An alternative is to use a service ID -
* and service discovery to find the physical address. -
*/ -
private String url; -
-
/** -
* Flag to determine whether the prefix for this route (the path, minus pattern -
* patcher) should be stripped before forwarding. -
*/ -
private boolean stripPrefix = true; -
-
/** -
* Flag to indicate that this route should be retryable (if supported). Generally -
* retry requires a service ID and ribbon. -
*/ -
private Boolean retryable; -
-
private Boolean enabled; -
-
public String getId() { -
return id; -
} -
-
public void setId(String id) { -
this.id = id; -
} -
-
public String getPath() { -
return path; -
} -
-
public void setPath(String path) { -
this.path = path;

本文介绍了如何在Spring Cloud中实现Zuul路由的动态配置,避免服务重启。详细讲述了创建路由信息表、定义CustomRouteLocator类、配置CustomZuulConfig、实现数据库路由刷新以及提供刷新控制器的过程。同时,文章提到了ZuulRefreshListener导致的30秒自动刷新问题,并给出了解决方案。
最低0.47元/天 解锁文章
382

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



