超簡單的SpringClound入門教程(四.路由閘道器zuul)
一個微服務的專案架構是要有要的基礎構件的,包括服務註冊與發現、服務消費、負載均衡、斷路器、智慧路由、配置管理等;這樣便能構件出一個簡單的微服務專案;
整理出一個簡單的微服務架構圖;
大家可以看在圖中Zuul就充當著路由轉發的角色
不止如此,Zuul還具備很多功能:
Authentication / Insights / Stress Testing / Canary Testing / Dynamic Routing / Service Migration / Load Shedding /Security / Static Response handling / Active / Active traffic management等;
一.專案準備;
我們繼續沿用上一篇文章的工程;在此基礎上我們建立一個新的Model;
二.建立service-zuuul
同樣的,建立一個SpringBoot專案的Model;命名service-zuul;
pomy依賴如下;
<?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.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.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> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</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>Dalston.RC1</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> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
properties配置檔案如下;
#服務埠 server.port=8886 #註冊服務中心地址 eureka.client.service-url.defaultZone=http://localhost:8880/eureka/ #註冊服務端name spring.application.name=service-zuul #/api-a的服務呼叫ribbon zuul.routes.api-a.path=/api-a/** zuul.routes.api-a.serviceId= service-ribbon #/api-b的服務呼叫feign zuul.routes.api-b.path=/api-b/** zuul.routes.api-b.serviceId= service-feign
我們在啟動類上貼@EnableZuulProxy,表示開啟Zuul功能;
@EnableZuulProxy //開啟zuul的功能: @EnableEurekaClient @SpringBootApplication public class ZuulApplication { public static void main(String[] args) { SpringApplication.run(ZuulApplication.class, args); } }
這時候我們啟動Eureka,service-hello(兩個例項),service-ribbon,service-fegin,最後啟動service-zuul;
我們訪問 http://localhost:8886/api-a/hello 和 http://localhost:8886/api-b/hello
會看到如下結果:
這就說明了我們的zuul專案起到了路由作用,
三,Zuul的過濾功能;
下面我們來實踐Zuul的過濾功能;通過其過濾功能我們可以做一些安全驗證,資料加解密,登入驗證等等需求;
我們只需自定義個類;並繼承ZuulFilter即可;
程式碼如下:
/** * 用zuul做過濾 */ @Component public class MyFilter extends ZuulFilter { private static Logger log = LoggerFactory.getLogger(MyFilter.class); //filterType:返回一個字串代表過濾器的型別 @Override public String filterType() { return "pre"; //pre:路由之前 routing:路由之時 post: 路由之後 error:傳送錯誤呼叫 } //filterOrder:過濾的順序 @Override public int filterOrder() { return 0; } //這裡可以寫邏輯判斷,是否要過濾,本文true,永遠過濾 @Override public boolean shouldFilter() { return true; } //過濾器的具體邏輯。可以查sql,nosql去判斷該請求到底有沒有許可權訪問。 @Override public Object run() { 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; } }
此時我們重啟service-zuul;訪問連線 http://localhost:8886/api-a/hello
就會通過過濾功能驗證,token為空,達到token的驗證效果;
我們再帶上token來訪問; http://localhost:8886/api-a/hello?token=888
此時便通過驗證,訪問服務;
官方文件: