Zuul filters

Zuul filters

Zuul filter Profile

spring cloud Zuul contains routing requests and two filter functions. Is responsible for routing function forwards the request to the specific micro-services, and the filter is responsible for processing the request to intervene is the basis of authority check, and other functions of the service aggregator.

In actual operation, the route mapping and forwards the request by several different filter completed. Each will enter zuul http request through a series of filters to give request response processing chain and returned to the client.

spring cloud zuul comprising four types of filters.

  • pre filter. Called before the request is routed. Zuul request before micro-services. For example, request authentication, select the service instance micro, record debugging information.
  • route filter. Responsible for forwarding the request to the micro-service. In this construct the original request, and sending the original request using the Apache HttpClient or Netflix Ribbon.
  • post filter. After the route is called and error filters. You can add standard HTTP Header In response, gather statistics and indicators, as well as the response sent to the client and so on.
  • error filter. It is called when an error occurs processing the request.

In addition to the default Filter, Zuul also allows us to create a custom filter type. For example, we can customize a STATIC type filter, it generates the response Zuul, instead of forwarding the request to a specific micro-services.

Lifecycle Zuul request are as follows:

mark

Reference Netflix Zuul wiki

Custom filters Zuul

Create a new module microservice-gateway-zuul-filter.

Add rely Zuul and Eureka client:

1
2
3
4
5
6
7
8
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

application.yml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
spring:
application:
name: microservice-gateway-zuul-filter

server:
port: 8809

eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
prefer-ip-address: true

zuul:
routes:
user1:
path: /user/**
serviceId: microservice-springcloud-user
stripPrefix: false

logging:
level:
com.netflix: debug

Define a pre types of filters, records the request URI.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class  extends ZuulFilter {
private final static Logger LOGGER = org.slf4j.LoggerFactory.getLogger(MyZuulPreFilter.class);

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


public int filterOrder () { return 0 ; // order filter, the smaller the number the first execution }




public Boolean shouldFilter () { return to true ; // whether the filter. }



// filter operation performed by specific public Object RUN () { . = RequestContext.getCurrentContext the HttpServletRequest Request () getRequest (); String requestUri Request.getRequestURI = (); logger.info ( "requesting the URI: {}" , requestUri ); return null ; } }








spring boot main categories:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@SpringBootApplication
@EnableZuulProxy
public class ZuulFilterApplication
{
public static void main( String[] args )
{
SpringApplication.run(ZuulFilterApplication.class,args);
}

@Bean
public MyZuulPreFilter myZuulPreFilter() {
return new MyZuulPreFilter();
}
}

test:

Start Eureka Server, user module and microservice-gateway-zuul-filter.

We access user module by Zuul's host and port / sample / 1.

mark

We look at the console.

mark

We simply record what URI request. More usage can refer netflix-core package ZuulFilter implemented.

mark

Details of the filter may use Zuul Reference: https://www.jianshu.com/p/ff863d532767

Original: Big Box  Zuul filters


Guess you like

Origin www.cnblogs.com/wangziqiang123/p/11652212.html