Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to route to an external url using zuul proxy filter

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

enter image description here

like image 466
user3274140 Avatar asked Oct 16 '22 19:10

user3274140


1 Answers

 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

like image 148
jayant mishra Avatar answered Oct 21 '22 00:10

jayant mishra