Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从数据库加载netflix zuul路由值_Java_Spring_Netflix Zuul_Spring Cloud Netflix_Api Gateway - Fatal编程技术网

Java 从数据库加载netflix zuul路由值

Java 从数据库加载netflix zuul路由值,java,spring,netflix-zuul,spring-cloud-netflix,api-gateway,Java,Spring,Netflix Zuul,Spring Cloud Netflix,Api Gateway,我正在学习api网关技术netflix zuul。我把所有的事情都安排好了,并且能够使用zuulproxy-url路由我的api。下面是我的代码: 主类 package com.example.springbootzuulgatwayproxy; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; impor

我正在学习api网关技术
netflix zuul
。我把所有的事情都安排好了,并且能够使用
zuul
proxy-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();
    }
}
zuul.routes.employee.url=http://localhost:8090
zuul.routes.employee2.url=http://localhost:8090
server.port=8080
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>
然后有4个过滤器类(Pre、Route、Post和Error),它们都扩展了
zuumfilter
class


一切正常。但现在的要求是,必须从数据库加载
application.properties
文件中的所有
zuul.routes.[entity].url
字段及其
url
值。我怎样才能达到这个要求。请给我一些建议。我已尝试使用类似的密钥预填充/更新application.properties文件。能够更新/写入application.properties文件,但实际效果不会在运行时发生。如何设置属性文件中定义的路由键以从mysql数据库动态加载。

应用程序中更新路由。属性不会自动触发
ZuulProperties
bean重新加载。无论路由存储在何处,您都需要一个类似以下内容的bean:


@初级的
@Bean(name=“zuulConfigProperties”)
@刷新范围
@配置属性(“zuul”)
公共ZuulProperties ZuulProperties(){
...
}

更新以包括路由配置:

zuul:
  ignoredServices: "*"
  routes:
    zuulDemo1:
      path: /zuul2/**
      serviceId: demo-zuul-api1
# stripPrefix set to true if context path is set to /
      stripPrefix: true

这在我写的一篇博文中有介绍,尽管它不在DB中存储路由,而是存储在一个由
Bitbucket
支持的文件中,该文件有一个WebHook,它向
Zuul
服务器发送post请求,然后服务器将消息发布到代理(
RabbitMQ
)订阅者可以获得变更集。

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

数据库结构

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'

很抱歉,我无法理解您的体系结构,您正在使用第三方配置
@ConfigurationProperties(“zuul”)
方法
zuulProperties()
。但是我想知道,在这个返回
ZuulProperties
的特殊方法中,如何加载这些属性(类似于上面提到的
application.properties
)。我可以使用
路由过滤器
做些什么吗?你也可以用
Map
代替
db