Zuul includes two main functions: Request Routing and filtering. The routing function is responsible for forwarding external requests to specific micro service instances, which is the basis for realizing the unified entrance of external access. The filter function is responsible for intervening in the request processing process, which is the basis for realizing request verification, service aggregation and other functions.
Zuul and Eureka integrate, register zuul as an application under Eureka service governance, and obtain messages of other micro services from Eureka. In the future, access to micro services is obtained through zuul jump.
Note: Zuul service will eventually register with Eureka and provide three functions: proxy + routing + filtering.
Zuul is a microservice gateway in spring cloud. Gateway: it is the front portal in the overall network system. The request first passes through the gateway to route the path and locate the specific service node.
Zuul is a microservice gateway. First, it is a microservice. It will also register and discover services in Eureka registry. It is also a gateway. Requests should be routed through zuul.
Zuul gateway is not necessary. It is recommended.
Zuul is generally recommended when there are a large number of micro services (more than 10), when there are strict requirements on service management, and when there are strict requirements on micro service permissions.
2. Role of Zuul gatewayThe gateway has the following functions:
- Unified entrance: not all services are provided with a unique entrance. The gateway acts as an external and internal isolation to ensure the security of background services.
- Authentication verification: identify the authority of each request and reject requests that do not meet the requirements.
- Dynamic routing: dynamically route requests to different back-end clusters.
- Reduce the coupling between client and server: services can be developed independently and mapped through the gateway layer.
Create a new cloud-gateway-zuul9527 module, modify pom.xml, and add Zuul, Eureka and Hystrix dependencies here.
cloud-gateway-zuul9527 module pom.xml file.
<?xml version="1.0" encoding="UTF-8"?> <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"> <!-- Import parent module name --> <parent> <artifactId>cloud2020</artifactId> <groupId>com.king.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <!-- Sub module name --> <artifactId>cloud-gateway-zuul9527</artifactId> <dependencies> <!-- introduce zuul Routing gateway dependency --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> <version>2.0.0.RELEASE</version> </dependency> <!-- introduce hystrix Fuse dependency --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <!-- quote eureka-client Register service client dependencies --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- actuator monitor --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- Reference parent spring boot Dependence of --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Configure hot deployment --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <!-- Reference parent lombok rely on --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
application.yml file of cloud-gateway-zuul9527 module.
# Configure service port number server: port: 9527 # Configure application information spring: application: name: cloud-gateway-zuul # Configure app name # Configure eureka eureka: client: service-url: defaultZone: http://Eureka 7001. Com: 7001 / Eureka / # settled service address instance: instance-id: gateway-zuul-9527 # Specify the service instance ID prefer-ip-address: true # The access path can display the IP address
The main startup class of cloud-gateway-zuul9527 module.
package com.king.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; /** * @EnableZuulServer: Ordinary ZuulServer only supports basic route and filter functions * @EnableZuulProxy: Enhanced version of ordinary ZuulServer + service discovery and fusing, with reverse proxy function */ @EnableZuulProxy @SpringBootApplication public class ZuulMain9527 { public static void main(String[] args) { SpringApplication.run(ZuulMain9527.class, args); } }
Start the cloud-eureka-server7001 registry template, cloud provider-hystrix-payment8001 service producer template, and cloud-gateway-zuul9527 routing gateway module.
Access via browser http://eureka7001.com:7001/ On the page, you can see that both CLOUD-GATEWAY-ZUUL and cloud-provider-hierarchy-payment have been registered in the registration center.
Then visit the cloud provider hystrix payment8001 service producer template http://localhost:8001/payment/hystrix/ok/1 At this time, you can get the correct results. At this time, you can access directly.
Then try to access the cloud provider hystrix payment8001 service producer template through the cloud-gateway-zuul9527 routing gateway module http://localhost:8001/payment/hystrix/ok/1 API method, access path http://localhost:9527/cloud-provider-hystrix-payment/payment/hystrix/ok/1. You can also get the correct results.
The service name here is directly exposed. Sometimes, we don't want to directly expose the service name. Is there a way to solve this problem?
Find the cloud-gateway-zuul9527 module, add the following content in application.yml, and make a routing mapping rule for the service name.
# Configure service port number server: port: 9527 # Configure application information spring: application: name: cloud-gateway-zuul # Configure app name # Configure eureka eureka: client: service-url: defaultZone: http://Eureka 7001. Com: 7001 / Eureka / # settled service address instance: instance-id: gateway-zuul-9527 # Specify the service instance ID prefer-ip-address: true # The access path can display the IP address zuul: routes: serviceTempName.serviceId: cloud-provider-hystrix-payment # Real service name / application name registered into Eureka serviceTempName.path: /provider8001/** # Indicates that all requests to access / provider8001 / * * through the browser are actually sent to the cloud provider hystrix payment service name ignored-services: cloud-provider-hystrix-payment # Hide the original service name. After hiding, it can only be accessed through '/ provider8001'. If there are multiple original names of micro services that need to be hidden, just change the value of ignored services to '*'. prefix: /king # Access prefix
The serviceTempName configured in this section is taken casually, which is equivalent to temporarily giving a name to the current service. The value of serviceId is the real service name registered in Eureka, and path is the virtual path. It means that all requests to access / provider8001 / * * through the browser are actually sent to the service name cloud provider hierarchy payment.
Prefix can add an access prefix to the request passing through Zuul, that is, the service name needs to be prefixed to correctly access resources. We must add a / king before provider8001 to access it normally. If not, it must be an error.
We send a request http://localhost:9527/cloud -Provider hystrix payment / payment / hystrix / OK / 1 reports Whitelabel Error Page 404 because the mapping rules are reconfigured to access http://localhost:9527/king/provider8001/payment/hystrix/ok/1 The results can be obtained normally and the mapping rules take effect.
The function of ignored services here is to hide the original service name. If it is not hidden, both provider 8001 and cloud provider hystrix payment can be accessed, which does not completely solve the previous problems. Therefore, when mapping service names, the original names are usually hidden. If there are multiple microservices whose original names need to be hidden, just change the value of ignored services to "*".
5. View routing informationAdd the following information to application.yml to expose the routes and filters information of the actor for easy viewing.
# By default, the actor will only expose health and info, and let the actor expose the routes and filters endpoints management: endpoints: web: exposure: include: routes, filters
Browser access http://localhost:9527/actuator/routes You can view the routing information and access http://localhost:9527/actuator/filters You can view the filter information.
6. FilterCreate MyZuulFilter, inherit ZuulFilter, implement the corresponding method, and add MyZuulFilter to the container.
package com.king.springcloud.filter; import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.exception.ZuulException; import org.springframework.stereotype.Component; import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.ROUTE_TYPE; @Component public class MyZuulFilter extends ZuulFilter { /** * ERROR_TYPE:Execute when an error occurs * POST_TYPE:Execute after ROUTE or ERROR * PRE_TYPE:Execute before agent execution * ROUTE_TYPE:Execute when the agent executes */ @Override public String filterType() { return ROUTE_TYPE; } /** * If multiple zuulfilters of the same type are defined, the execution order is determined according to the return value of the filterOrder() method * The smaller the return value, the higher the priority and the earlier the execution */ @Override public int filterOrder() { return 0; } /** * true:Turn on the filter * false:Do not turn on the filter */ @Override public boolean shouldFilter() { return true; } /** * What does the filter do */ @Override public Object run() throws ZuulException { System.out.println("MyZuulFilterConfig.run"); return null; } }
Access via browser http://localhost:9527/king/provider8001/payment/hystrix/ok/1 , you see output on the console, indicating that the run() method has been executed.