Zuul provides the function of service gateway, which can realize load balancing, reverse proxy, dynamic routing, request forwarding and other functions.
Most of Zuul's functions are implemented through filters. In addition to the standard four filter types, it also supports custom filters.
Using the @ EnableZuulProxy annotation, the Spring container initializes the Zuul related configuration, including a Spring Boot Bean: ServletRegistrationBean, which is mainly used to register the Servlet. In the service method of Servlet, various Zuul filters are executed. The following figure shows the life cycle of HTTP requests in ZuulServlet.
Integrating Zuul in Spring Boot Web project:
I. create a hello source service project
1. Create project
Development tool: IntelliJ idea February 3, 2019
Create a new spring boot project named "Hello server" in IDEA, select 2.1.10 for spring boot version, and check Web - > spring web in the dependency interface.
2. Modify startup code
Add a hello service
package com.example.helloserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class HelloServerApplication { public static void main(String[] args) { SpringApplication.run(HelloServerApplication.class, args); } @GetMapping("/hello/{name}") public String hello(@PathVariable String name){ return "hello " + name; } }
3. Modify the configuration application.yml and specify the port number 8090
server: port: 8090
II. Test routing function
1. Create project
Create a new spring boot project named zuul router in IDEA. Select 2.1.10 for the spring boot version. Check Web - > spring web, spring cloud Routing - > zuul in the dependency interface.
It mainly adds two dependencies: spring boot starter web and spring cloud starter Netflix zuul.
The complete content of pom.xml is as follows:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.10.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>zuul-router</artifactId> <version>0.0.1-SNAPSHOT</version> <name>zuul-router</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR4</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2. Modify startup code
Add annotation @ EnableZuulProxy
package com.example.zuulrouter; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication @EnableZuulProxy public class ZuulRouterApplication { public static void main(String[] args) { SpringApplication.run(ZuulRouterApplication.class, args); } }
3. Modify the configuration application.yml
zuul: routes: test: url: http://localhost:8090
After adding the above configuration, all requests sent to http://localhost:8080/test will be forwarded to port 8090.
Visit http://localhost:8080/test/hello/lc in the browser, and the page output is hello lc
The above routing configuration omits path, and routeId "test" is used as the path by default.
Revised to:
zuul: routes: test: path: /a/** url: http://localhost:8090 b: url: https://www.cnblogs.com/gdjlc
Now the browser accesses http://localhost:8080/a/hello/lc, and the page output is hello lc
Visit http://localhost:8080/b, and the content of https://www.cnblogs.com/gdjlc will be displayed on the page