前言
zuul 是在spring cloud netflix平臺上提供動態路由,監控,彈性,安全等邊緣服務的框架,是netflix基于jvm的路由器和服務器端負載均衡器,相當于是設備和 netflix 流應用的 web 網站后端所有請求的前門。本文基于上篇(springcloud系列——ribbon 負載均衡)實現zuul動態路由
github地址:https://github.com/netflix/zuul
代碼編寫
首先我們在springcloud下面新建一個springboot項目:zuul-server,pom繼承parent,并且在eureka上面注冊(還不會服務注冊與發現的,請戳:springcloud系列——eureka 服務注冊與發現)
maven引入zuul
1
2
3
4
5
|
<!-- zuul --> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-netflix-zuul</artifactid> </dependency> |
配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
server.port= 10010 spring.application.name=zuul-server eureka.client.serviceurl.defaultzone=http: //localhost:1111/eureka/ #健康檢查(需要spring-boot-starter-actuator依賴) eureka.client.healthcheck.enabled= true # 續約更新時間間隔(默認 30 秒) eureka.instance.lease-renewal-interval-in-seconds= 10 # 續約到期時間(默認 90 秒) eureka.instance.lease-expiration-duration-in-seconds= 10 #zuul代理配置 zuul.routes.服務名.path,服務名要與注冊的一致 #應用名映射 zuul.routes.myspringboot.path=/myspringboot/** zuul.routes.myspringboot.service-id=myspringboot #url映射 #zuul.routes.myspringboot.path=/myspringboot/** #zuul.routes.myspringboot-url.url=http: //localhost:10087/ |
自定義zuul過濾器
更多的檢查規則后續慢慢健全
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
/** * zuul過濾器,實現了路由檢查 */ public class accessfilter extends zuulfilter { /** * 通過int值來定義過濾器的執行順序 */ @override public int filterorder() { // predecoration之前運行 return pre_decoration_filter_order - 1 ; } /** * 過濾器的類型,在zuul中定義了四種不同生命周期的過濾器類型: * public static final string error_type = "error"; * public static final string post_type = "post"; * public static final string pre_type = "pre"; * public static final string route_type = "route"; */ @override public string filtertype() { return pre_type; } /** * 過濾器的具體邏輯 */ @override public object run() { requestcontext ctx = requestcontext.getcurrentcontext(); httpservletrequest request = ctx.getrequest(); system.out.println(string.format( "%s accessfilter request to %s" , request.getmethod(),request.getrequesturl().tostring())); string accesstoken = request.getparameter( "accesstoken" ); //有權限令牌 if (!stringutils.isempty(accesstoken)) { ctx.setsendzuulresponse( true ); ctx.setresponsestatuscode( 200 ); //可以設置一些值 ctx.set( "issuccess" , true ); return null ; } else { ctx.setsendzuulresponse( false ); ctx.setresponsestatuscode( 401 ); ctx.setresponsebody( "{\"result\":\"accesstoken is not correct!\"}" ); //可以設置一些值 ctx.set( "issuccess" , false ); return null ; } } /** * 返回一個boolean類型來判斷該過濾器是否要執行 */ @override public boolean shouldfilter() { return true ; } } |
啟動類
添加@enablezuulproxy注解并使用自定義過濾器
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@enablezuulproxy @springbootapplication public class zuulserverapplication { public static void main(string[] args) { springapplication.run(zuulserverapplication. class , args); } @bean public accessfilter accessfilter() { return new accessfilter(); } } |
效果演示
啟動所有項目,我們在eureka上注冊了四個服務,相比上篇(springcloud系列——ribbon 負載均衡)多了一個zuul
瀏覽器訪問 http://localhost:10010/myspringboot/feign/ribbon、http://localhost:10010/myspringboot/feign/ribbon?accesstoken=123456
http://localhost:10010/ 這個端口對外暴露,相對于總入口,后面接不同的路徑由,zuul路由到對應的服務上
1、沒有accesstoken是,無法通過檢查
2、攜帶accesstoken時,可正常路由,并且feign調用、ribbon負載均衡
后記
我們為什么要使用zuul呢?
1、請求校驗、路由轉發,接口校驗與業務邏輯分離
2、隱藏諸多服務路徑,只暴露統一入口,安全
更多zuul配置,請看官方文檔
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/huanzi-qch/p/10142395.html