how to route to an external url using zuul proxy filter

北城以北 提交于 2021-01-22 08:21:09

问题


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:

ribbon.eureka.enabled=false
server.port=8080
zuul.routes.books.sensitive-headers=
zuul.routes.books.path = /books/
zuul.routes.books.url = <ext 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:

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

Issue screenshot


回答1:


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

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

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



来源:https://stackoverflow.com/questions/50094625/how-to-route-to-an-external-url-using-zuul-proxy-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!