无法从配置类加载netflix zuul路由值

时间:2018-07-01 18:21:28

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

我正在尝试使用netflix zuul api gateway技术。我能够使用application.properties文件路由我的网址。但是我无法使用ZuulProperties进行第三方配置。这是一个要求。我怎样才能做到这一点。我尝试了以下代码:

配置类代码

package com.example.springbootzuulgatwayproxy;

import java.util.HashMap;
import java.util.Map;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
public class AppConfig {

    @Primary
    @Bean(name = "zuulConfigProperties")
    @RefreshScope
    @ConfigurationProperties("zuul")
    public ZuulProperties zuulProperties() {
        ZuulProperties.ZuulRoute route = new ZuulProperties.ZuulRoute("http://localhost:8090");// ZuulRoute is static inner class of ZuulProperties
        Map<String,ZuulRoute> map = new HashMap<String,ZuulRoute>();
        map.put("zuul.routes.employee.url", route);
        ZuulProperties props = new ZuulProperties();
        props.setRoutes(map);
        return props;
    }
}

在这里,我期望ZuulProperties从我的配置类内部加载zuul.route属性。我要错了吗?就像我说过的那样,我可以使用applicatin.properties轻松地做到这一点。但这是我坚持的要求。坦率地说,我希望从database加载这些属性。但是后来我遇到了这段代码,看起来很有希望。但是我无法做到这一点。

主班

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 org.springframework.context.annotation.ComponentScan;
import com.example.springbootzuulgatwayproxy.filters.ErrorFilter;
import com.example.springbootzuulgatwayproxy.filters.PostFilter;
import com.example.springbootzuulgatwayproxy.filters.PreFilter;
import com.example.springbootzuulgatwayproxy.filters.RouteFilter;
@ComponentScan
@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();
    }
}

当我点击http://localhost:8080/employee/getEmployeeDetails/{employee_name}网址时,邮递员给了我404找不到错误。

0 个答案:

没有答案