業余草 SpringCloud教程 | 第五篇: 路由網關(zuul)(Finchley版本)
在微服務架構中,需要幾個基礎的服務治理組件,包括服務註冊與發現、服務消費、負載均衡、斷路器、智能路由、配置管理等,由這幾個基礎組件相互協作,共同組建了一個簡單的微服務系統。一個簡答的微服務系統如下圖:
註意:A服務和B服務是可以相互調用的,作圖的時候忘記了。並且配置服務也是註冊到服務註冊中心的。
在Spring Cloud微服務系統中,一種常見的負載均衡方式是,客戶端的請求首先經過負載均衡(zuul、Ngnix),再到達服務網關(zuul集群),然後再到具體的服。,服務統一註冊到高可用的服務註冊中心集群,服務的所有的配置文件由配置服務管理(下一篇文章講述),配置服務的配置文件放在git倉庫,方便開發人員隨時改配置。
一、Zuul簡介
Zuul的主要功能是路由轉發和過濾器。路由功能是微服務的一部分,比如/api/user轉發到到user服務,/api/shop轉發到到shop服務。zuul默認和Ribbon結合實現了負載均衡的功能。
zuul有以下功能:
- Authentication
- Insights
- Stress Testing
- Canary Testing
- Dynamic Routing
- Service Migration
- Load Shedding
- Security
- Static Response handling
- Active/Active traffic management
二、準備工作
繼續使用上一節的工程。在原有的工程上,創建一個新的工程。
三、創建service-zuul工程
其pom.xml文件如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4<modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.forezp</groupId> 7 <artifactId>service-zuul</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>service-zuul</name> 12 <description>Demo project for Spring Boot</description> 13 14 <parent> 15 <groupId>com.forezp</groupId> 16 <artifactId>sc-f-chapter5</artifactId> 17 <version>0.0.1-SNAPSHOT</version> 18 </parent> 19 20 <dependencies> 21 <dependency> 22 <groupId>org.springframework.cloud</groupId> 23 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 24 </dependency> 25 <dependency> 26 <groupId>org.springframework.boot</groupId> 27 <artifactId>spring-boot-starter-web</artifactId> 28 </dependency> 29 <dependency> 30 <groupId>org.springframework.cloud</groupId> 31 <artifactId>spring-cloud-starter-netflix-zuul</artifactId> 32 </dependency> 33 </dependencies> 34 </project>
在其入口applicaton類加上註解@EnableZuulProxy,開啟zuul的功能:
1 @SpringBootApplication 2 @EnableZuulProxy 3 @EnableEurekaClient 4 @EnableDiscoveryClient 5 public class ServiceZuulApplication { 6 7 public static void main(String[] args) { 8 SpringApplication.run( ServiceZuulApplication.class, args ); 9 } 10 }
加上配置文件application.yml加上以下的配置代碼:
1 eureka: 2 client: 3 serviceUrl: 4 defaultZone: http://localhost:8761/eureka/ 5 server: 6 port: 8769 7 spring: 8 application: 9 name: service-zuul 10 zuul: 11 routes: 12 api-a: 13 path: /api-a/** 14 serviceId: service-ribbon 15 api-b: 16 path: /api-b/** 17 serviceId: service-feign
首先指定服務註冊中心的地址為http://localhost:8761/eureka/,服務的端口為8769,服務名為service-zuul;以/api-a/ 開頭的請求都轉發給service-ribbon服務;以/api-b/開頭的請求都轉發給service-feign服務;
依次運行這五個工程;打開瀏覽器訪問:http://localhost:8769/api-a/hi?name=forezp ;瀏覽器顯示:
hi forezp,i am from port:8762
打開瀏覽器訪問:http://localhost:8769/api-b/hi?name=forezp ;瀏覽器顯示:
hi forezp,i am from port:8762
這說明zuul起到了路由的作用
四、服務過濾
zuul不僅只是路由,並且還能過濾,做一些安全驗證。繼續改造工程;
1 @Component 2 public class MyFilter extends ZuulFilter { 3 4 private static Logger log = LoggerFactory.getLogger(MyFilter.class); 5 @Override 6 public String filterType() { 7 return "pre"; 8 } 9 10 @Override 11 public int filterOrder() { 12 return 0; 13 } 14 15 @Override 16 public boolean shouldFilter() { 17 return true; 18 } 19 20 @Override 21 public Object run() { 22 RequestContext ctx = RequestContext.getCurrentContext(); 23 HttpServletRequest request = ctx.getRequest(); 24 log.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString())); 25 Object accessToken = request.getParameter("token"); 26 if(accessToken == null) { 27 log.warn("token is empty"); 28 ctx.setSendZuulResponse(false); 29 ctx.setResponseStatusCode(401); 30 try { 31 ctx.getResponse().getWriter().write("token is empty"); 32 }catch (Exception e){} 33 34 return null; 35 } 36 log.info("ok"); 37 return null; 38 } 39 }
- filterType:返回一個字符串代表過濾器的類型,在zuul中定義了四種不同生命周期的過濾器類型,具體如下:
- pre:路由之前
- routing:路由之時
- post: 路由之後
- error:發送錯誤調用
- filterOrder:過濾的順序
- shouldFilter:這裏可以寫邏輯判斷,是否要過濾,本文true,永遠過濾。
- run:過濾器的具體邏輯。可用很復雜,包括查sql,nosql去判斷該請求到底有沒有權限訪問。
這時訪問:http://localhost:8769/api-a/hi?name=forezp ;網頁顯示:
token is empty
訪問 http://localhost:8769/api-a/hi?name=forezp&token=22 ;
網頁顯示:
hi forezp,i am from port:8762
本文源碼下載:
https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-chapter5
五、參考資料:
http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html
感謝您的關註!可加QQ1群:135430763,QQ2群:454796847,QQ3群:187424846。QQ群進群密碼:xttblog,想加微信群的朋友,可以微信搜索:xmtxtt,備註:“xttblog”,添加助理微信拉你進群。備註錯誤不會同意好友申請。再次感謝您的關註!後續有精彩內容會第一時間發給您!原創文章投稿請發送至[email protected]郵箱。商務合作可添加助理微信進行溝通!
業余草 SpringCloud教程 | 第五篇: 路由網關(zuul)(Finchley版本)