brief introduction
Zuul is an open source API gateway server provided by Netflix. Spring Cloud integrates and enhances Zuul. The service gateway Zuul aggregates all the micro-service interfaces and exposes them uniformly. The external client only needs to interact with the service gateway. Compared with internal services, it can prevent them from being directly accessed by external clients and expose sensitive information of services, thus playing a protective role. In addition, Zuul can also achieve identity authentication, data monitoring, dynamic routing and other functions.
Project introduction
- sc-parent, parent module (see Spring Cloud Learning Notes (1): Eureka Registry)
- sc-eureka, Registry (see Spring Cloud Learning Notes (1): Eureka Registry)
- sc-provider, provider (see Spring Cloud Learning Notes (1): Eureka Registry)
- sc-gateway, service gateway
Using Zuul to Build Service Gateway
1. Create the sub-module project sc-gateway under the parent module, pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.cf</groupId> <artifactId>sc-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>sc-gateway</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> </dependencies> </project>
2. Create the startup class gateway. Gateway Application:
package gateway; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication @EnableZuulProxy public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); } }
@ Enable Zuul Proxyh and @Enable Zuul Server:
@ EnableZuulProxy is a superset of @EnableZuulServer, and @EnableZuulProxy contains all filters imported by @EnableZuulServer.
@ EnableZuulProxy uses a reverse proxy and @EnableZuulServer does not use any proxy.
3. Create application.yml:
server: port: 8088 spring: application: name: sc-gateway eureka: client: serviceUrl: defaultZone: http://localhost:8080/eureka/ zuul: routes: sc-provider: /sp/** #Mapping service Id as sc-provider to / sp/** path
4. test
Start the registry sc-eureka, provider sc-provider, gateway sc-gateway in turn. The following are the results of Zuul's access to providers and direct access to providers:
Other common configurations
1. Ignore specified services
zuul: ignored-services: serviceId1,serviceId2 #Ignore service Id1, service Id2
2. Ignore all services and proxy only specified services
zuul: ignored-services: '*' #* To ignore all services, only proxy sc-provider services routes: sc-provider: /sp/**
3. Specify access path prefix, which can only be accessed with prefix after setting.
zuul: prefix: /yc routes: sc-provider: /sp/**
4. The url of the specified service
zuul: routes: sc-provider: path: /sp/** url: http://localhost:8081 # specifies the url of the service sc-provider, which is not obtained from the Eureka registry.
This configuration will not be performed as HystrixCommand, nor will Ribbon be used to balance the load of multiple url s. To achieve these goals, you can use a list of OfServers or specify service Id.
5. Specify sensitive headers to prevent leakage of sensitive headers
zuul: routes: sc-provider: path: /sp/** sensitiveHeaders: Cookie,Set-Cookie,Authorization #The value of global zuul.sensitiveHeaders will be overwritten url: http://localhost:8081
If you want to set a global sensitive header, you can set the value of zuul.sensitiveHeaders. Cookie, Set-Cookie and Authorization are the default values for sensitiveHeaders. If you don't want to set sensitive headers, you can set sensitiveHeaders to an empty list:
zuul: routes: sc-provider: path: /sp/** sensitiveHeaders: url: http://localhost:8081
6. Ignore Header
zuul: ignoredHeaders: Header1, Header2 #Header1 and Header2 will not be propagated to other microservices
Zuul's Routing Endpoint
When @EnableZuulProxy is used in conjunction with Spring Boot Actuator, Zuul exposes a route management endpoint/routes through which Zuul can view the route list information currently mapped by Zuul.
1. Modify pom.xml of sc-gateway and add Spring Boot Actuator dependencies:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
2. Modify the application.yml of sc-gateway to open access to / routes endpoints:
management: endpoints: web: exposure: include: 'routes'
3. Access / routes endpoints
Start the registry sc-eureka, provider sc-provider and gateway sc-gateway in turn, then visit http://localhost:8088/actuator/routes/details, and display the routing list information as follows:
Zuul filter
The core of Zuul is a series of filters that can perform a series of operations during HTTP request and response routing. Zuul provides a framework for dynamically reading, compiling and running these filters, which do not communicate directly with each other and share data through a unique Request Context for each request.
1.Zuul filter type
- PRE Filters: Execute the request before it is routed to a specific service.
- ROUTING Filters: Used to route requests to microservices.
- POST Filters: Execute after the request is routed to the microservice.
- ERROR Filters: Execute when errors occur at other stages.
2.Zuul filter characteristics
- Type: The type of Zuul filter that determines at which stage of the request the filter works.
- Execution Order: specifies the order in which filters are executed, and the smaller the value, the earlier they are executed.
- Criteria: Conditions required for Filter execution.
- Action: If the condition is met, the operation is performed.
3.Zuul Request Life Cycle Diagram
4. Custom Zuul Filter
New class gateway.filter.MyZuulFilter:
package gateway.filter; import javax.servlet.http.HttpServletRequest; import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.exception.ZuulException; public class MyZuulFilter extends ZuulFilter{ /** * Execution logic */ @Override public Object run() throws ZuulException { RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); if (request.getParameter("name") != null) { System.out.println("Hello," + request.getParameter("name")); } return null; } /** * Determine if you want to execute the filter */ @Override public boolean shouldFilter() { return true; } /** * Filter execution sequence */ @Override public int filterOrder() { return 1; } /** * Type of filter */ @Override public String filterType() { return "pre"; } }
Add configuration to the startup class Gateway Application:
@Bean public MyZuulFilter MyZuulFilter(){ return new MyZuulFilter(); }
Start registry sc-eureka, provider sc-provider, gateway sc-gateway in turn, then visit http://localhost:8088/sp/book/list?name=Xiaoming, MyZuulFilter filter will be executed, console output: Hello, Xiaoming.