7-Spring cloud routing gateway zuul
1 Introduction
- This article is a continuation of the previous article, so some clusters or services are not introduced below. For example, the Eureka cluster is still started on the basis of the previous ones. The introduction of the previous articles is as follows:
- 1-Eureka service registration and discovery and Eureka cluster construction (practical) .
- 2-Spring cloud's Eureka quickly eliminates invalidation services
- 3-Building Ribbon load balancing in Spring cloud - practical operation on the server (Part 1)
- 4-Building Ribbon load balancing in Spring cloud - practical operation on the server (Part 2)
- 5-Usage of Feign in Spring cloud - practical operation on the server .
- 6-Hystrix fault-tolerant processing of Spring cloud .
2. About zuul
2.1 Basic principles of zuul
-
Gateway (Zuul) - the essential core is the filter (this sentence is actually enough)
These filters can perform the following functions:
- Authentication and security: Identify the authentication requirements for each resource and reject those requests that do not meet the requirements.
- Audit and Monitor: Track meaningful data and statistics at the edge, resulting in an accurate view of production.
- dynamic routing: Dynamically route requests to different backend clusters.
- Stress test: Gradually increase traffic to your execution cluster to understand performance.
- load distribution: Allocate corresponding capacity to each load type and discard requests exceeding the limit.
- Static response handling: Partial responses are built directly at the edge location to avoid forwarding them to the internal cluster.
- Multi-region elasticity: request routing across AWS Regions, aiming at diversifying the use of ELB (Elastic Load Balancing) and bringing the edge of the system closer to the system users.
- After implementing the request routing function, the interface provided by our microservice application can be accessed by the client through the unified API gateway entrance. However, when each client user requests the interface provided for the server application, their access rights are often limited, and the system does not open all microservice interfaces to them.
- After completing the service routing, we still need some security measures to open the service to the outside world to protect the client from only accessing the resources it should access. So we need to use Zuul's filters to implement security control of our external services.
-
To define a filter in the service gateway, you only need to inherit the ZuulFilter abstract class and implement the four abstract functions it defines to intercept and filter requests.
-
For more explanation, please refer to the following article:
Microservice: Gateway (zuul) - basic principles, configuration, authentication, IP restriction, and current limiting .
2.2 Why use zuul
- You can refer to the following article
Getting Started with Zuul (Full Version) .
3. Build zuul
3.1 Project structure
- as follows:
3.2 Basic configuration
3.2.1 pom file
-
as follows:
<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.liu.susu</groupId> <artifactId>dog-cloud-parent</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>dog-gateway-zuul-8090</artifactId> <packaging>jar</packaging> <name>dog-gateway-zuul-8090</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>com.liu.susu</groupId> <artifactId>dog-api</artifactId> <version>${project.version}</version> </dependency> <!--zuul相关的--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!--actuator监控--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--容错hystrix--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <!--下面这几个,版本同${spring-boot.version}--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
3.2.2 yml file
-
as follows:
server: port: 8090 # #网关的端口号 spring: application: name: dog-gateway-zuul # #网关在注册中心的唯一名称 eureka: client: # 客户端注册进eureka服务列表内 register-with-eureka: true # false表示不向注册中心注册自己 service-url: defaultZone: http://62.234.14.112:2886/eureka/,http://58.87.88.142:2886/eureka,http://154.8.150.175:2886/eureka/ instance: instance-id: dog-gateway-zuul-8090 # prefer-ip-address: true
3.3.3 Startup class
-
as follows:
@SpringBootApplication @EnableEurekaClient @EnableZuulProxy
3.3 Test to see the effect
3.3.1 Demonstration
- First, start the Eureka cluster and service provider, and then start zuul. My Eureka cluster and service provider have not stopped on the server, so start the local zuul directly, as follows:
- Then make sure that direct access to the service provider is available, as follows:
- Then access it through zuul,Zuul also comes with load balancing,as follows:
// http://zuul_host:zuul_port/微服务在Eureka 注册中心上的服务提供者(对应的application.name)/接口映射地址 // 本地的话,如下3种访问均可 http://localhost:8090/dog-provider/dog/getDogByNum/1 http://127.0.0.1:8090/dog-provider/dog/getDogByNum/1 http://192.168.1.101:8090/dog-provider/dog/getDogByNum/1
3.3.1 Architecture diagram
- Simple structure, as follows
4. zuul routing access mapping rules
4.1 Mapping the service name of the service provider
-
The configuration is as follows:
zuul: routes: zuulDog.serviceId: dog-provider # dog-provider 是服务提供者的服务名 zuulDog.path: /myDog/** # ignored-services: dog-provider #禁止通过此服务名访问,即 http://localhost:8090/dog-provider/dog/getDogByNum/1 不再可访问 ignored-services: "*" # 忽略多个微服务的情况
-
The access effect is as follows:
// http://localhost:8090/dog-provider/dog/getDogByNum/1 没映射前的访问,暴漏了服务提供者名 http://localhost:8090/myDog/dog/getDogByNum/1 //myDog 随便映射
4.2 Access prefix
- as follows:
zuul:
routes:
zuulDog.serviceId: dog-provider # dog-provider 是服务提供者的服务名
zuulDog.path: /myDog/**
# ignored-services: dog-provider #禁止通过此服务名访问,即 http://localhost:8090/dog-provider/dog/getDogByNum/1 不再可访问
ignored-services: "*" # 忽略多个微服务的情况
prefix: /susu # 加前缀