前言
zuul 是在spring cloud netflix平臺(tái)上提供動(dòng)態(tài)路由,監(jiān)控,彈性,安全等邊緣服務(wù)的框架,是netflix基于jvm的路由器和服務(wù)器端負(fù)載均衡器,相當(dāng)于是設(shè)備和 netflix 流應(yīng)用的 web 網(wǎng)站后端所有請(qǐng)求的前門。本文基于上篇(springcloud系列——ribbon 負(fù)載均衡)實(shí)現(xiàn)zuul動(dòng)態(tài)路由
github地址:https://github.com/netflix/zuul
代碼編寫(xiě)
首先我們?cè)趕pringcloud下面新建一個(gè)springboot項(xiàng)目:zuul-server,pom繼承parent,并且在eureka上面注冊(cè)(還不會(huì)服務(wù)注冊(cè)與發(fā)現(xiàn)的,請(qǐng)戳:springcloud系列——eureka 服務(wù)注冊(cè)與發(fā)現(xiàn))
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 # 續(xù)約更新時(shí)間間隔(默認(rèn) 30 秒) eureka.instance.lease-renewal-interval-in-seconds= 10 # 續(xù)約到期時(shí)間(默認(rèn) 90 秒) eureka.instance.lease-expiration-duration-in-seconds= 10 #zuul代理配置 zuul.routes.服務(wù)名.path,服務(wù)名要與注冊(cè)的一致 #應(yīng)用名映射 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過(guò)濾器
更多的檢查規(guī)則后續(xù)慢慢健全
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過(guò)濾器,實(shí)現(xiàn)了路由檢查 */ public class accessfilter extends zuulfilter { /** * 通過(guò)int值來(lái)定義過(guò)濾器的執(zhí)行順序 */ @override public int filterorder() { // predecoration之前運(yùn)行 return pre_decoration_filter_order - 1 ; } /** * 過(guò)濾器的類型,在zuul中定義了四種不同生命周期的過(guò)濾器類型: * 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; } /** * 過(guò)濾器的具體邏輯 */ @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" ); //有權(quán)限令牌 if (!stringutils.isempty(accesstoken)) { ctx.setsendzuulresponse( true ); ctx.setresponsestatuscode( 200 ); //可以設(shè)置一些值 ctx.set( "issuccess" , true ); return null ; } else { ctx.setsendzuulresponse( false ); ctx.setresponsestatuscode( 401 ); ctx.setresponsebody( "{\"result\":\"accesstoken is not correct!\"}" ); //可以設(shè)置一些值 ctx.set( "issuccess" , false ); return null ; } } /** * 返回一個(gè)boolean類型來(lái)判斷該過(guò)濾器是否要執(zhí)行 */ @override public boolean shouldfilter() { return true ; } } |
啟動(dòng)類
添加@enablezuulproxy注解并使用自定義過(guò)濾器
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(); } } |
效果演示
啟動(dòng)所有項(xiàng)目,我們?cè)趀ureka上注冊(cè)了四個(gè)服務(wù),相比上篇(springcloud系列——ribbon 負(fù)載均衡)多了一個(gè)zuul
瀏覽器訪問(wèn) http://localhost:10010/myspringboot/feign/ribbon、http://localhost:10010/myspringboot/feign/ribbon?accesstoken=123456
http://localhost:10010/ 這個(gè)端口對(duì)外暴露,相對(duì)于總?cè)肟冢竺娼硬煌穆窂接桑瑉uul路由到對(duì)應(yīng)的服務(wù)上
1、沒(méi)有accesstoken是,無(wú)法通過(guò)檢查
2、攜帶accesstoken時(shí),可正常路由,并且feign調(diào)用、ribbon負(fù)載均衡
后記
我們?yōu)槭裁匆褂脄uul呢?
1、請(qǐng)求校驗(yàn)、路由轉(zhuǎn)發(fā),接口校驗(yàn)與業(yè)務(wù)邏輯分離
2、隱藏諸多服務(wù)路徑,只暴露統(tǒng)一入口,安全
更多zuul配置,請(qǐng)看官方文檔
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://www.cnblogs.com/huanzi-qch/p/10142395.html