Spring-Cloud-Netflix-Zuul-Filter

Spring-cloud-netflix-Zuul-Filter

Spring cloud netflix routing-and-filtering

Pom.xml:

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependencies>
    

Annotations:

@EnableZuulProxy @SpringBootApplication

Filter Types:

Filter classes implement Four methods:

Main.java:

@EnableZuulProxy
@SpringBootApplication
public class RoutingAndFilteringGatewayApplication {

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

  @Bean
  public SimpleFilter simpleFilter() {
    return new SimpleFilter();
  }

}

SimpleFilter.Java:

package com.example.routingandfilteringgateway.filters.pre;

import javax.servlet.http.HttpServletRequest;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.ZuulFilter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SimpleFilter extends ZuulFilter {

  private static Logger log = LoggerFactory.getLogger(SimpleFilter.class);

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

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

  @Override
  public boolean shouldFilter() {
    return true;
  }

  @Override
  public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();
    HttpServletRequest request = ctx.getRequest();

    log.info(String.format("%s request to %s", request.getMethod(), request.getRequestURL().toString()));

    return null;
  }

}

Intergration with other microservices:


zuul.routes.doctor-service.path=/doctor-api/**
zuul.routes.doctor-service.url=http://localhost:8081/
zuul.routes.diagnosis-service.url=http://localhost:8082/getDiagnosis

ribbon.eureka.enabled=false
server.port=8080