網關在微服務里的角色
在微服務架構體系里,網關是非常重要的一個環節,它主要實現了一些功能的統一處理,包括了:
- 統一授權
- 統一異常處理
- 路由導向
- 跨域處理
- 限流
實踐一下
1 添加依賴
1
2
3
4
5
6
|
dependencies { implementation( 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client' ) implementation( 'org.springframework.cloud:spring-cloud-starter-netflix-zuul' ) testimplementation( 'org.springframework.boot:spring-boot-starter-test' ) implementation( 'com.marcosbarbero.cloud:spring-cloud-zuul-ratelimit:1.3.2.release' ) } |
2 添加yml
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
|
server: port: 8300 spring: application: name: microservice-gateway-zuul eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultzone: http: //localhost:6761/eureka instance: ip-address: true zuul: routes: users: path: /lind/** #以lind開頭的路徑被重定向到lind服務 serviceid: lind add-host-header: true #顯示真實的http頭 retryable: false #關閉hystrix的重試功能 ratelimit: enabled: true # repository: redis behind-proxy: true policies: users: limit: 5 #限流,每分鐘請求 5 次 refresh-interval: 60 type: - user - origin - url # url類型的限流就是通過請求路徑區分 # origin是通過客戶端ip地址區分 # user是通過授權用戶進行區分,也包括匿名用戶 |
3 添加實現代碼
http攔截器,獲取用戶id,為子服務進行傳遞
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
|
public class prerequestlogfilter extends zuulfilter { private static final logger logger = loggerfactory.getlogger(prerequestlogfilter. class ); private final ratelimiter ratelimiter = ratelimiter.create( 1000.0 ); @override public object run() { try { requestcontext currentcontext = requestcontext.getcurrentcontext(); httpservletresponse response = currentcontext.getresponse(); httpservletrequest reqeust = currentcontext.getrequest(); currentcontext.addzuulrequestheader( "userid" , "123" ); //向子系統http頭寫數據 currentcontext.addzuulrequestheader( "username" , "test" ); prerequestlogfilter.logger.info( string.format( "send %s request to %s" , reqeust.getmethod(), reqeust.getrequesturl().tostring())); if (!ratelimiter.tryacquire()) { httpstatus httpstatus = httpstatus.too_many_requests; response.setcontenttype(mediatype.text_plain_value); response.setstatus(httpstatus.value()); response.getwriter().append(httpstatus.getreasonphrase()); currentcontext.setsendzuulresponse( false ); throw new zuulexception( httpstatus.getreasonphrase(), httpstatus.value(), httpstatus.getreasonphrase() ); } } catch (java.lang.exception e) { reflectionutils.rethrowruntimeexception(e); } return null ; } @override public boolean shouldfilter() { // 判斷是否需要過濾 return true ; } @override public string filtertype() { return filterconstants.pre_type; } @override public int filterorder() { return ordered.highest_precedence; } } |
在主程中注入這個過濾器
1
2
3
4
|
@bean public prerequestlogfilter prerequestlogfilter() { return new prerequestlogfilter(); } |
4 使用它
在url上通過localhost:8300/users/home
將進行lind服務里的home控制器下,并在http頭上寫入了userid和username這個鍵值對!
總結
以上所述是小編給大家介紹的springboot zuul實現網關,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://www.cnblogs.com/lori/p/9811526.html