如何使用 zuul 代理过滤器路由到外部 url [英] how to route to an external url using zuul proxy filter

查看:80
本文介绍了如何使用 zuul 代理过滤器路由到外部 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i have an external url and i want to pass some request header through zuul filter to launch the application. Can anyone please help me on this. In my custom prefilter i have written this:

 @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;
    }
}

app.properties:

app.properties:

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

示例应用:这给了我一个 rest url,我通过它重定向到上面在我的属性文件中定义的外部 url.

Sample Application: This is giving me a rest url through which i am redirecting to external url defined above in my properties file.

public class SampleApplication {

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

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

}

app.properties:

app.properties:

spring.application.name=book
server.port=8090

问题截图

推荐答案

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

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

These simple url-routes do not get executed as a HystrixCommand, nor do they load-balance multiple URLs with Ribbon. To achieve those goals, you can specify a serviceId with a static list of servers, as follows:

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 的请求时出现

in your filter make sure that this filter should come into picture only for request with uri example1.com or example2.com by impelemting should filter method

这篇关于如何使用 zuul 代理过滤器路由到外部 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆