Zuul在Web項目中的使用見上文《SpringBoot中使用Zuul》,下麵例子為Zuul在Spring Cloud的使用。 ...
Zuul在Web項目中的使用見上文《SpringBoot中使用Zuul》,下麵例子為Zuul在Spring Cloud的使用。
開發工具:IntelliJ IDEA 2019.2.3
一、伺服器端
1、創建項目
IDEA中創建一個新的SpringBoot項目,名稱為“zuul-eureka-server”,SpringBoot版本選擇2.1.10,在選擇Dependencies(依賴)的界面勾選Spring Cloud Discovert -> Eureka Server。
pom.xml完整內容如下:
<?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>View Code
2、修改配置application.yml
修改埠號為8761;取消將自己信息註冊到Eureka伺服器,不從Eureka伺服器抓取註冊信息。
server: port: 8761 eureka: client: register-with-eureka: false fetch-registry: false
3、修改啟動類代碼
增加註解@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); } }View Code
二、服務提供者
1、創建項目
IDEA中創建一個新的SpringBoot項目,除了名稱為“zuul-provider”,其它步驟和上面創建伺服器端一樣。
2、修改配置application.yml
server: port: 8000 spring: application: name: zuul-provider eureka: instance: hostname: localhost client: serviceUrl: defaultZone: http://localhost:8761/eureka/
3、添加一個實體類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、修改啟動類代碼
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; } }
三、服務調用者
1、創建項目
IDEA中創建一個新的SpringBoot項目,名稱為“zuul-invoker”,SpringBoot版本選擇2.1.10,在選擇Dependencies(依賴)的界面勾選Spring Cloud Discovery -> Eureka Server,Spring Cloud Routing -> OpenFeign。
pom.xml完整內容如下:
<?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>View Code
2、修改配置application.yml
server: port: 9000 spring: application: name: zuul-invoker eureka: instance: hostname: localhost client: serviceUrl: defaultZone: http://localhost:8761/eureka/
3、添加一個實體類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、添加一個客戶端介面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; //聲明調用的服務名稱 @FeignClient("zuul-provider") public interface UserClient { @RequestMapping(method = RequestMethod.GET, value = "/user/{name}") User getUser(@PathVariable("name") String name); }
5、修改啟動類代碼CloudInvokerApplication.java
加上註解@EnableEurekaClient和@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、添加控制器 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; } }
四、網關Zuul
1、創建項目
IDEA中創建一個新的SpringBoot項目,名稱為“zuul-gateway”,SpringBoot版本選擇2.1.10,在選擇Dependencies(依賴)的界面勾選Spring Cloud Discovery -> Eureka Server,Spring Cloud Routing -> Zuul。
pom.xml完整內容如下:
<?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>View Code
2、修改啟動類代碼
增加註解@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、修改配置application.yml
把網關項目註冊到Eureka伺服器中;
聲明所有的/user/**請求會被轉發到Id為zuul-invoker的服務進行處理。
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
五、測試
1、啟動伺服器端。
瀏覽器訪問http://localhost:8761/,正常
2、啟動服務提供者
瀏覽器訪問 http://localhost:8000/user/小明
頁面輸出:{"name":"小明","age":30}
3、啟動服務調用者。
瀏覽器訪問http://localhost:9000/invokeUser/小明
頁面輸出:{"name":"小明","age":30}
4、啟動網關
http://localhost:8080/user/invokeUser/小明
頁面輸出:{"name":"小明","age":30}
由此可見,成功轉發並處理。