Spring Cloud Zuul路由动态配置

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

目录

  • Zuul配置

  • 在mysql中创建路由信息表

  • 定义CustomRouteLocator类

  • 增加CustomZuulConfig类,主要是为了配置CustomRouteLocator

  • RefreshRouteService类,用于实现数据库路由信息的刷新

  • 当然也要提供RefreshController,提供从浏览器访问的刷新功能

  • 问题

  • 后记

Zuul 是 Netflix 开源的微服务网关,Spring Cloud 对 Zuul 进行了整合和增强。在 SpringCloud 体系中,Zuul 担任着网关的角色,对发送到服务端的请求进行一些预处理,比如安全验证、动态路由、负载分配等。

还是那句话,由于水平有限,难免有不当或者错误之处,请大家指正,谢谢。

Zuul配置

一般的,我们如果使用Spring Cloud Zuul 进行路由配置,类似于下面的样子:

 
  1. zuul:

  2.  routes:

  3.    users:

  4.      path: /myusers/**

  5.      stripPrefix: false

当我们要新增或者改变一个网关路由时,我们不得不停止网关服务,修改配置文件,保存再重新启动网关服务,这样才能让我们新的设置生效。

设想一样,如果是在生产环境,为了一个小小的路由变更,这样的停止再重启恐怕谁也受不了吧。接下来,看看我们怎么能做到动态配置网关路由,让网关路由配置在服务不需要重启的情况生效。(废话一堆啊)

在mysql中创建路由信息表,对于类如下:

 
  1. public static class ZuulRouteVO {

  2.  

  3.        /**

  4.         * The ID of the route (the same as its map key by default).

  5.         */

  6.        private String id;

  7.  

  8.        /**

  9.         * The path (pattern) for the route, e.g. /foo/**.

  10.         */

  11.        private String path;

  12.  

  13.        /**

  14.         * The service ID (if any) to map to this route. You can specify a physical URL or

  15.         * a service, but not both.

  16.         */

  17.        private String serviceId;

  18.  

  19.        /**

  20.         * A full physical URL to map to the route. An alternative is to use a service ID

  21.         * and service discovery to find the physical address.

  22.         */

  23.        private String url;

  24.  

  25.        /**

  26.         * Flag to determine whether the prefix for this route (the path, minus pattern

  27.         * patcher) should be stripped before forwarding.

  28.         */

  29.        private boolean stripPrefix = true;

  30.  

  31.        /**

  32.         * Flag to indicate that this route should be retryable (if supported). Generally

  33.         * retry requires a service ID and ribbon.

  34.         */

  35.        private Boolean retryable;

  36.  

  37.        private Boolean enabled;

  38.  

  39.        public String getId() {

  40.            return id;

  41.        }

  42.  

  43.        public void setId(String id) {

  44.            this.id = id;

  45.        }

  46.  

  47.        public String getPath() {

  48.            return path;

  49.        }

  50.  

  51.        public void setPath(String path) {

  52.            this.path = path;

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值