一、zuul簡介web
1.一、zuul是什麼 spring
zuul 是netflix開源的一個API Gateway 服務器, 本質上是一個web servlet應用。sql
Zuul 在雲平臺上提供動態路由,監控,彈性,安全等邊緣服務的框架。Zuul 至關因而設備和 Netflix 流應用的 Web 網站後端全部請求的前門。apache
Zuul的主要功能是路由轉發和過濾器。路由功能是微服務的一部分,好比/api/user轉發到到user服務,/api/shop轉發到到shop服務。zuul默認和Ribbon結合實現了負載均衡的功能。後端
1.二、zuul能作什麼api
zuul的核心是一系列的filters, 其做用能夠類比Servlet框架的Filter,或者AOP。瀏覽器
Zuul能夠經過加載動態過濾機制,從而實現如下各項功能:安全
二、建立service-zuul工程服務器
2.一、建立service-zuul工程,pom.xml文件以下app
<?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.lishun</groupId> <artifactId>service-zuul</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>service-zuul</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.lishun</groupId> <artifactId>cloud</artifactId> <version>1.0-SNAPSHOT</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
2.二、在其入口applicaton類加上註解@EnableZuulProxy,開啓zuul的功能:
@EnableZuulProxy @EnableEurekaClient @SpringBootApplication public class ServiceZuulApplication { public static void main(String[] args) { SpringApplication.run(ServiceZuulApplication.class, args); } }
2.三、加上配置文件application.yml加上如下的配置代碼:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8769
spring:
application:
name: service-zuul
zuul:
routes:
api-a:
path: /api-a/**
serviceId: service-ribbon
api-b:
path: /api-b/**
serviceId: service-feign
首先指定服務註冊中心的地址爲http://localhost:8761/eureka/,服務的端口爲8769,服務名爲service-zuul;
以/api-a/ 開頭的請求都轉發給service-ribbon服務;以/api-b/開頭的請求都轉發給service-feign服務;
2.四、啓動5個服務,瀏覽器訪問 http://localhost:8769/api-a/hi?name=lis 頁面顯示
hi lis ,i am from port:8762
瀏覽器訪問 http://localhost:8769/api-b/hi?name=lis 頁面顯示
hi lis ,i am from port:8762
這說明zuul起到了路由的做用
三、服務過濾
前面提到,Zuul經過加載動態過濾機制,不只能夠實現路由功能,還能實現驗證,檢查,監控等功能。
下面使用zuul來實現安全驗證功能
@Component public class MyFilter extends ZuulFilter { private static Logger log = LoggerFactory.getLogger(MyFilter.class); @Override public String filterType() { return "pre"; } @Override public int filterOrder() { return 0; } @Override public boolean shouldFilter() { return true; } @Override public Object run() throws ZuulException { RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); log.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString())); Object accessToken = request.getParameter("token"); if(accessToken == null) { log.warn("token is empty"); ctx.setSendZuulResponse(false); ctx.setResponseStatusCode(401); try { ctx.getResponse().getWriter().write("token is empty"); }catch (Exception e){} return null; } log.info("ok"); return null; } }
filterType:返回一個字符串表明過濾器的類型,在zuul中定義了四種不一樣生命週期的過濾器類型,具體以下:
這時訪問:http://localhost:8769/api-a/hi?name=lis ;網頁顯示:
token is empty
訪問 http://localhost:8769/api-a/hi?name=lis&token=22 ; 網頁顯示:
hi lis ,i am from port:8762