一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - SpringCloud Zuul實(shí)現(xiàn)動(dòng)態(tài)路由

SpringCloud Zuul實(shí)現(xiàn)動(dòng)態(tài)路由

2021-07-01 14:49huanzi-qch Java教程

這篇文章主要介紹了SpringCloud Zuul實(shí)現(xiàn)動(dòng)態(tài)路由,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

前言

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

官方文檔:https://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/2.1.0.rc2/single/spring-cloud-netflix.html

代碼編寫(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)

SpringCloud Zuul實(shí)現(xiàn)動(dòng)態(tài)路由

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

SpringCloud Zuul實(shí)現(xiàn)動(dòng)態(tài)路由

瀏覽器訪問(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ù)載均衡

SpringCloud Zuul實(shí)現(xiàn)動(dòng)態(tài)路由

后記

我們?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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美成人午夜片一一在线观看 | 亚洲欧美日韩另类精品一区二区三区 | 青草国内精品视频在线观看 | 风间由美一区二区av101 | 五月天色综合 | 97色伦亚洲自偷 | 免费港剧在线观看港剧 | 无码AV精品一区二区三区 | 末发育xxxxx仙踪林 | 色哟约| 草嫩社区| 色综合久久中文字幕网 | 欧美日韩一区二区三区免费不卡 | 大肚孕妇的高h辣文 | 日韩毛片在线 | 日韩一区三区 | 天生奶水1v1高h | 亚洲 日本 天堂 国产 在线 | 91久久国产露脸精品 | 九九精品视频一区二区三区 | bb18lv黑料正能量 | 亚洲四虎影院 | 国产成人成人一区二区 | 国产suv精品一区二区四区三区 | 国产在线拍 | 国产日本免费 | 91香蕉国产视频 | 91精品国产91热久久久久福利 | 大肥臀风间由美 中文字幕 大东北chinesexxxx露脸 | 欧美激情影音先锋 | 99热99在线 | 护士xxxx | 操到翻白眼 | 好大水好多好爽好硬好深视频 | 男生同性视频twink在线 | 偷偷狠狠的日日高清完整视频 | 亚洲欧美在线免费观看 | 色婷婷久久综合中文久久一本 | 免费观看欧美性一级 | 亚洲日本在线观看网址 | 欧美人妖草草xxoo |