The use of Zuul in Web projects is shown above< Using Zuul in SpringBoot >, the following example is Zuul's use in Spring Cloud.
Development tool: IntelliJ idea February 3, 2019
I. server side
1. Create project
Create a new spring boot project named "zuul Eureka server" in IDEA, select 2.1.10 for spring boot version, and check spring cloud discovery - > Eureka server in the dependency interface.
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-eureka-server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>zuul-eureka-server</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.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</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 the configuration application.yml
Modify the port number to 8761; cancel the registration of your own information to Eureka server, and do not retrieve the registration information from Eureka server.
server: port: 8761 eureka: client: register-with-eureka: false fetch-registry: false
3. Modify startup code
Add annotation @ EnableEurekaServer
package com.example.zuuleurekaserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class ZuulEurekaServerApplication { public static void main(String[] args) { SpringApplication.run(ZuulEurekaServerApplication.class, args); } }
II. Service provider
1. Create project
Create a new spring boot project in IDEA, with the exception of the name "zuul provider", and the other steps are the same as above.
2. Modify the configuration application.yml
server: port: 8000 spring: application: name: zuul-provider eureka: instance: hostname: localhost client: serviceUrl: defaultZone: http://localhost:8761/eureka/
3. Add an entity class User.java
package com.example.zuulprovider; public class User { String name; Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
4. Modify startup code
package com.example.zuulprovider; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @EnableEurekaClient @RestController public class ZuulProviderApplication { public static void main(String[] args) { SpringApplication.run(ZuulProviderApplication.class, args); } @RequestMapping(value="/user/{name}", produces = MediaType.APPLICATION_JSON_VALUE) public User user(@PathVariable String name) { User u = new User(); u.setName(name); u.setAge(30); return u; } }
III. service caller
1. Create project
Create a new spring boot project named "zuul invoker" in IDEA. Select 2.1.10 for the spring boot version. In the dependency interface, check spring cloud discovery - > Eureka server, spring cloud Routing - > openfeign.
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-invoker</artifactId> <version>0.0.1-SNAPSHOT</version> <name>zuul-invoker</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.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</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 the configuration application.yml
server: port: 9000 spring: application: name: zuul-invoker eureka: instance: hostname: localhost client: serviceUrl: defaultZone: http://localhost:8761/eureka/
3. Add an entity class User.java
package com.example.zuulinvoker; public class User { String name; Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
4. Add a client interface UserClient.java
package com.example.zuulinvoker; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; //Declare the service name of the call @FeignClient("zuul-provider") public interface UserClient { @RequestMapping(method = RequestMethod.GET, value = "/user/{name}") User getUser(@PathVariable("name") String name); }
5. Modify the startup code CloudInvokerApplication.java
Annotate @ EnableEurekaClient and @ EnableFeignClients
package com.example.zuulinvoker; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableEurekaClient @EnableFeignClients public class ZuulInvokerApplication { public static void main(String[] args) { SpringApplication.run(ZuulInvokerApplication.class, args); } }
6. Add controller InvokerController.java
package com.example.zuulinvoker; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class InvokerController { @Autowired private UserClient userClient; @RequestMapping(value = "/invokeUser/{name}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public User invokeUser(@PathVariable String name){ User user = userClient.getUser(name); return user; } }
IV. gateway Zuul
1. Create project
Create a new spring boot project named zuul gateway in IDEA. Select 2.1.10 for the spring boot version. Check spring cloud discovery - > Eureka server, spring cloud Routing - > zuul in the dependency interface.
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-gateway</artifactId> <version>0.0.1-SNAPSHOT</version> <name>zuul-gateway</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.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</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.zuulgateway; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication @EnableZuulProxy public class ZuulGatewayApplication { public static void main(String[] args) { SpringApplication.run(ZuulGatewayApplication.class, args); } }
3. Modify the configuration application.yml
Register the gateway project to Eureka server;
Declare that all / user / * * requests will be forwarded to the service with Id zuul invoker for processing.
server: port: 8080 spring: application: name: zuul-gateway eureka: instance: hostname: localhost client: serviceUrl: defaultZone: http://localhost:8761/eureka/ zuul: routes: user: path: /user/** serviceId: zuul-invoker
Five, test
1. Start the server.
Browser access http://localhost:8761 /, normal
2. Start service provider
Browser visit http://localhost:8000/user / Xiaoming
Page output: {"name": "Xiaoming", "age":30}
3. Start the service caller.
Browser visit http://localhost:9000/invokeUser / Xiaoming
Page output: {"name": "Xiaoming", "age":30}
4. Start the gateway
http://localhost:8080/user/invokeUser / Xiaoming
Page output: {"name": "Xiaoming", "age":30}
Thus, it can be seen that it was successfully forwarded and processed.