一 Zuul簡介html
zuul 是netflix開源的一個API Gateway 服務器, 本質上是一個web servlet應用。java
Zuul 在雲平臺上提供動態路由,監控,彈性,安全等邊緣服務的框架。Zuul 至關因而設備和 Netflix 流應用的 Web 網站後端全部請求的前門。git
zuul的例子能夠參考 netflix 在github上的 simple webapp,能夠按照netflix 在github wiki 上文檔說明來進行使用。github
簡單說來就是把一個請求地址轉發到另外的地址上面去,在轉發以前能夠進行權限驗證,合法檢查等操做。web
下圖展現了Zuul與Eureka配合使用時的架構spring
接到上篇"SpringCloud之聲明式服務調用 Feign(三)"apache
繼續在springcloud工程中添加模塊eureka-zuul,也是經過start.spring.io提供的模板建立後端
新的目錄api
生成的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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xuan</groupId> <artifactId>eureka-zuul</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>eureka-zuul</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Finchley.RELEASE</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-eureka-client</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>
修改啓動文件EurekaZuulApplication.java,增長@EnableZuulProxy註解。
package com.xuan.eurekazuul; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication @EnableZuulProxy public class EurekaZuulApplication { public static void main(String[] args) { SpringApplication.run(EurekaZuulApplication.class, args); } }
修改配置文件」application.properties「,找到註冊中心和定義自身的服務名,端口,轉發的路徑,
spring.application.name=eureka-zuul
server.port=8765
zuul.routes.api.path=/xuan/**
zuul.routes.api.serviceId=eureka-client
eureka.client.serviceUrl.defaultZone=http://localhost:8080/eureka/
分別啓動模塊了:
1.EurekaServerApplication
2.EurekaClientApplication,EurekaClientApplication1,EurekaClientApplication2
3.EurekaZuulApplication
啓動後打開http://localhost:8080/顯示如圖:
訪問Zuul模塊提供的接口http://localhost:8765/xuan/hello,刷新一次也會訪問到不一樣的提供者上面去,緣由是zuul作負載均衡。