Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create custom zuul filters in spring cloud

I want to write some of my own custom zuul filters for a spring cloud microservice i am writing. Once i have the filter written how do I integrate it so the underlying netflix zuul framework can take advantage of it.

like image 522
EvilJinious1 Avatar asked Feb 12 '15 01:02

EvilJinious1


People also ask

How do you implement filters in spring Cloud gateway?

Implementing Spring Cloud Gateway Filters using Java Configuration. In the FirstController we extract the request header we have added in the pre filter and print it. In the SecondController we extract the request header we have added in the pre filter and print it. Run the application.

What type of filters can a Zuul service provide?

There are four types of standard filters in Zuul: pre for pre-routing filtering, route for routing to an origin, post for post-routing filters, and error for error handling. Zuul also supports a static type for static responses.

What are Zuul filters?

Zuul provides a framework to dynamically read, compile, and run these Filters. Filters do not communicate with each other directly - instead they share state through a RequestContext which is unique to each request. Filters are currently written in Groovy, although Zuul supports any JVM-based language.


1 Answers

Create a @Bean that extends ZuulFilter. See java configuration examples here. As long as the bean is in the same context as the @EnableZuulProxy app, it will automatically get picked up.

@Bean
public MyFilter myFilter() {
    return new MyFilter();
}

See examples of filters here.

public class MyFilter extends ZuulFilter {
    //...
}

There are three types of filters: pre, route and post and each set of filters is executed in that order (ie all pre's first, routes 2nd and post's 3rd).

like image 135
spencergibb Avatar answered Oct 11 '22 01:10

spencergibb