如何使用zuul代理筛选器路由到外部URL

时间:2018-04-30 05:48:52

标签: spring-boot netflix-zuul spring-rest

我有一个外部网址,我想通过zuul过滤器传递一些请求标头来启动应用程序。 任何人都可以帮我这个。 在我的自定义预滤波器中,我写了这个:

public String Keys(double input){

    if (input > 0){
        System.out.println("positive");
    }
    else if (input < 0) {
        System.out.println("negative");
    }
    else if (input == 0) {
        System.out.println("zero");
    }
    return "";
}

public HashMap<String, Integer> increaseValues(ArrayList<Double> inputs){
    HashMap<String, Integer> hashMap = new HashMap<>();
    hashMap.put("positive", 0);
    hashMap.put("negative", 0);
    hashMap.put("zero", 0);

//What I tried before adding the Keys method.
//This updates the value but the loop won't continue if another input in the 
//arraylist is true.

for (int i = 0; i < inputs.size(); i++){
        double input = inputs.get(i);

        if (input > 0){
           hashMap.put("positive", 1);
        } else if (input < 0){
          hashMap.put("negative", 1);
        } else if (input == 0){
          hashMap.put("zero", 1); }
    return hashMap;
}

public void main(String[] args){
    ArrayList<Double> inputs = new ArrayList<>();
    inputs.add(-4.56);
    inputs.add(-4.66);
    inputs.add(0.0);
    inputs.add(6.0);
    inputs.add(-6.99);
    inputs.add(6.97);   
}

app.properties:

 @Component
public class CustomFilters extends ZuulFilter {

    public static final Logger logger = LoggerFactory.getLogger(CustomFilters.class);


    @Override
    public String filterType() {
        return "route";
    }

    @Override
    public int filterOrder() {
        return 1;
    }

    @Override
    public boolean shouldFilter() {

        return true;
    }

    @Override
    public Object run() {
        logger.info("executing run ");
        RequestContext ctx = RequestContext.getCurrentContext();
        ctx.addZuulRequestHeader("x-forwarded-host", "<external url>");
        ctx.addZuulRequestHeader("x-forwarded-proto", "https");
        ctx.addZuulRequestHeader("x-forwarded-port", "8800");
        ctx.addZuulRequestHeader("key", "id);

        return null;
    }
}

示例应用程序: 这给了我一个休息网址,通过它我可以重定向到我的属性文件中定义的外部网址。

ribbon.eureka.enabled=false
server.port=8080
zuul.routes.books.sensitive-headers=
zuul.routes.books.path = /books/
zuul.routes.books.url = <ext url>

app.properties:

public class SampleApplication {

    @RequestMapping(value = "/check")
      public String available() {
        return "available!";
      }

    public static void main(String[] args) {
        SpringApplication.run(SampleApplication.class, args);
    }

}

发布截图

enter image description here

1 个答案:

答案 0 :(得分:2)

 zuul:
  routes:
    users:
      path: /myusers/**
      url: http://example.com/users_service

这些简单的url-routes不会作为HystrixCommand执行,也不会使用Ribbon对多个URL进行负载均衡。要实现这些目标,您可以使用静态服务器列表指定serviceId,如下所示:

zuul:
  routes:
    echo:
      path: /myusers/**
      serviceId: myusers-service
      stripPrefix: true

hystrix:
  command:
    myusers-service:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: ...

myusers-service:
  ribbon:
    NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList
    ListOfServers: http://example1.com,http://example2.com
    ConnectTimeout: 1000
    ReadTimeout: 3000
    MaxTotalHttpConnections: 500
    MaxConnectionsPerHost: 100

在您的过滤器中,请确保此过滤器仅应用于uri example1.com或example2.com的请求,并通过inslemting过滤方法