从数据库加载 netflix zuul 路由值
Posted
技术标签:
【中文标题】从数据库加载 netflix zuul 路由值【英文标题】:load netflix zuul routes values from database 【发布时间】:2018-12-07 10:02:37 【问题描述】:我正在学习 api 网关技术netflix zuul
。我弄清楚了所有事情,并且能够使用zuul
代理网址路由我的api。以下是我的代码:
主类
package com.example.springbootzuulgatwayproxy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
import com.example.springbootzuulgatwayproxy.filters.ErrorFilter;
import com.example.springbootzuulgatwayproxy.filters.PostFilter;
import com.example.springbootzuulgatwayproxy.filters.PreFilter;
import com.example.springbootzuulgatwayproxy.filters.RouteFilter;
@SpringBootApplication
@EnableZuulProxy
public class SpringBootZuulgatwayproxyApplication
public static void main(String[] args)
SpringApplication.run(SpringBootZuulgatwayproxyApplication.class, args);
@Bean
public PreFilter preFilter()
return new PreFilter();
@Bean
public PostFilter postFilter()
return new PostFilter();
@Bean
public ErrorFilter errorFilter()
return new ErrorFilter();
@Bean
public RouteFilter routeFilter()
return new RouteFilter();
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>spring-boot-zuulgatwayproxy</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-zuulgatwayproxy</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR2</spring-cloud.version>
</properties>
<dependencies>
<!-- <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency> -->
<!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId>
</dependency> -->
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId>
</dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>$spring-cloud.version</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.properties
zuul.routes.employee.url=http://localhost:8090
zuul.routes.employee2.url=http://localhost:8090
server.port=8080
然后有 4 个过滤器类(Pre、Route、Post 和 Error)都扩展 ZuulFilter
类。
一切正常。但现在的要求是,application.properties
文件中的所有zuul.routes.[entity].url
字段必须从数据库中加载,以及它们的url
值。我怎样才能达到这个要求。请指教。我已经尝试使用类似的键预填充/更新 application.properties 文件。能够更新/写入 application.properties 文件,但实际效果不会在运行时发生。我应该怎么做才能将属性文件中定义的那些路由键设置为从 mysql 数据库动态加载。
【问题讨论】:
【参考方案1】:更新application.properties
中的路由不会自动触发ZuulProperties
bean 重新加载。无论路由存储在哪里,您都需要一个类似于:
@Primary
@Bean(name = "zuulConfigProperties")
@RefreshScope
@ConfigurationProperties("zuul")
public ZuulProperties zuulProperties()
...
更新以包括路线配置:
zuul:
ignoredServices: "*"
routes:
zuulDemo1:
path: /zuul2/**
serviceId: demo-zuul-api1
# stripPrefix set to true if context path is set to /
stripPrefix: true
这在我写的一篇博客文章Routing requests and dynamically refreshing routes using Spring Cloud Zuul Server 中有所介绍,尽管它不会将路由存储在数据库中,而是存储在Bitbucket
支持的文件中,该文件具有向Zuul
发送POST 请求的WebHook服务器然后将消息发布到代理 (RabbitMQ
) 以供订阅者获取变更集。
【讨论】:
对不起,我无法理解您的架构,您正在使用第三方配置@ConfigurationProperties("zuul")
和方法 zuulProperties()
。但我想知道,如何在返回ZuulProperties
的特殊方法中加载这些属性(类似于上面提到的application.properties
)。
我可以用Route Filter
做些什么吗?你也可以用Map
代替db
来解释我【参考方案2】:
如果您想将路线值存储在数据库中,可以查看此博客。
https://programmer.group/spring-cloud-gateway-service-zuul-three-dynamic-routing.html
数据库结构
CREATE TABLE `sys_zuul_route` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'router Id',
`path` varchar(255) NOT NULL COMMENT 'Routing path',
`service_id` varchar(255) NOT NULL COMMENT 'Service name',
`url` varchar(255) DEFAULT NULL COMMENT 'url agent',
`strip_prefix` char(1) DEFAULT '1' COMMENT 'Forward without prefix',
`retryable` char(1) DEFAULT '1' COMMENT 'Do you want to retry?',
`enabled` char(1) DEFAULT '1' COMMENT 'Is it enabled?',
`sensitiveHeaders_list` varchar(255) DEFAULT NULL COMMENT 'Sensitive request header',
`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation time',
`update_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT 'Update time',
`del_flag` char(1) DEFAULT '0' COMMENT 'Remove identity (0-normal,1-Delete)',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='Dynamic routing configuration table'
【讨论】:
以上是关于从数据库加载 netflix zuul 路由值的主要内容,如果未能解决你的问题,请参考以下文章
如何使用来自 Netflix/Eureka 服务的发现信息在 Netflix/Zuul 和 Netflix/Ribbon 中启用自动路由?
SpringCloud - Spring Cloud Netflix 之 Zuul网关;路由