从数据库加载netflix zuul路由值

时间:2018-06-28 10:44:27

标签: java spring netflix-zuul spring-cloud-netflix api-gateway

我正在学习有关API网关技术netflix zuul的知识。我了解到所有情况,并能够使用zuul代理url路由我的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类。

一切正常。但现在的要求是,zuul.routes.[entity].url文件中的所有application.properties字段都必须连同其url值一起从数据库中加载。我怎样才能达到这一要求。请指教。我尝试使用类似的密钥预先填充/更新application.properties文件。能够更新/写入application.properties文件,但实际效果在运行时不会发生。为了将那些在属性文件中定义的路由键设置为从MySQL数据库动态加载,我该怎么做。

2 个答案:

答案 0 :(得分:1)

application.properties中更新路由不会自动触发重新加载ZuulProperties bean。无论路由存储在哪里,您都需要一个类似于以下内容的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中对此进行了介绍,尽管该文章没有将路由存储在DB中,但存储在Bitbucket支持的文件中,该文件具有WebHook,可将POST请求发送到Zuul服务器,然后服务器将消息发布到代理(RabbitMQ),供订户获取更改集。

答案 1 :(得分:0)

如果您想将路由值存储在数据库中,您可以查看此博客。

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'