Zuul是Netflix提供的一個開源的API網關服務器,SpringCloud對Zuul進行了整合和加強。服務網關Zuul聚合了全部微服務接口,並統一對外暴露,外部客戶端只需與服務網關交互便可。相對於內部服務而言,可以防止其被外部客戶端直接訪問而暴露服務的敏感信息,起到了保護做用。除此以外,Zuul還能夠實現身份認證、數據監控、動態路由等功能。html
<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.cf</groupId> <artifactId>sc-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>sc-gateway</artifactId> <dependencies> <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> </dependencies> </project>
package gateway; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication @EnableZuulProxy public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); } }
@EnableZuulProxy是@EnableZuulServer的超集,@EnableZuulProxy包含@EnableZuulServer導入的全部過濾器。java
@EnableZuulProxy使用反向代理,@EnableZuulServer不使用任何代理。web
server: port: 8088 spring: application: name: sc-gateway eureka: client: serviceUrl: defaultZone: http://localhost:8080/eureka/ zuul: routes: sc-provider: /sp/** #將serviceId爲sc-provider的服務映射到/sp/**路徑
依次啓動註冊中心sc-eureka、提供者sc-provider、網關sc-gateway,如下是經過Zuul訪問提供者和直接訪問提供者的結果:
spring
zuul: ignored-services: serviceId1,serviceId2 #忽略服務serviceId1,serviceId2
zuul: ignored-services: '*' #*爲忽略全部服務,只代理sc-provider服務 routes: sc-provider: /sp/**
zuul: prefix: /yc routes: sc-provider: /sp/**
zuul: routes: sc-provider: path: /sp/** url: http://localhost:8081 #指定服務sc-provider的url,不從Eureka註冊中心獲取。
這種配置方式不會做爲HystrixCommand執行,也不會使用Ribbon來平衡多個url的負載。要實現這些目標,可使用靜態服務器列表(listOfServers)或者指定serviceId。apache
zuul: routes: sc-provider: path: /sp/** sensitiveHeaders: Cookie,Set-Cookie,Authorization #將會覆蓋全局zuul.sensitiveHeaders的值 url: http://localhost:8081
若是要設置全局的敏感Header能夠設置zuul.sensitiveHeaders的值。Cookie,Set-Cookie,Authorization爲sensitiveHeaders的默認值,若是不想設置敏感header,能夠把sensitiveHeaders設置成空列表:服務器
zuul: routes: sc-provider: path: /sp/** sensitiveHeaders: url: http://localhost:8081
zuul: ignoredHeaders: Header1, Header2 #Header1和Header2將不會傳播到其餘的微服務中
當@EnableZuulProxy和Spring Boot Actuator配合使用時,Zuul會暴露一個路由管理端點/routes,經過這個路由端點能夠查看到Zuul當前映射的路由列表信息。app
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
management: endpoints: web: exposure: include: 'routes'
依次啓動註冊中心sc-eureka、提供者sc-provider、網關sc-gateway,而後訪問http://localhost:8088/actuator/routes/details,顯示路由列表信息以下:框架
Zuul的核心是一系列過濾器,它們可以在HTTP請求和響應路由期間執行一系列操做。Zuul提供了一個框架來動態讀取、編譯和運行這些過濾器,過濾器之間不直接通訊,它們經過對每一個請求唯一的RequestContext共享數據。maven
新建類gateway.filter.MyZuulFilter:ide
package gateway.filter; import javax.servlet.http.HttpServletRequest; import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.exception.ZuulException; public class MyZuulFilter extends ZuulFilter{ /** * 具體執行邏輯 */ @Override public Object run() throws ZuulException { RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); if (request.getParameter("name") != null) { System.out.println("你好," + request.getParameter("name")); } return null; } /** * 判斷你該過濾器是否要執行 */ @Override public boolean shouldFilter() { return true; } /** * 過濾器執行順序 */ @Override public int filterOrder() { return 1; } /** * 過濾器類型 */ @Override public String filterType() { return "pre"; } }
啓動類GatewayApplication中添加配置:
@Bean public MyZuulFilter MyZuulFilter(){ return new MyZuulFilter(); }
依次啓動註冊中心sc-eureka、提供者sc-provider、網關sc-gateway,而後訪問http://localhost:8088/sp/book/list?name=小明,MyZuulFilter過濾器將會執行,控制檯輸出:你好,小明。