国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - SpringCloud Zuul實現動態路由

SpringCloud Zuul實現動態路由

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

這篇文章主要介紹了SpringCloud Zuul實現動態路由,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

前言

zuul 是在spring cloud netflix平臺上提供動態路由,監控,彈性,安全等邊緣服務的框架,是netflix基于jvm的路由器和服務器端負載均衡器,相當于是設備和 netflix 流應用的 web 網站后端所有請求的前門。本文基于上篇(springcloud系列——ribbon 負載均衡)實現zuul動態路由

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

代碼編寫

首先我們在springcloud下面新建一個springboot項目:zuul-server,pom繼承parent,并且在eureka上面注冊(還不會服務注冊與發現的,請戳:springcloud系列——eureka 服務注冊與發現

SpringCloud Zuul實現動態路由

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

SpringCloud 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負載均衡

SpringCloud Zuul實現動態路由

后記

我們為什么要使用zuul呢?

1、請求校驗、路由轉發,接口校驗與業務邏輯分離

2、隱藏諸多服務路徑,只暴露統一入口,安全

更多zuul配置,請看官方文檔

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://www.cnblogs.com/huanzi-qch/p/10142395.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成人av免费 | 日韩一区二区三区视频 | 91精品国产乱码久久久久久 | 亚洲精品国产精品国自产在线 | 午夜精品久久久久久久久久久久 | 亚洲欧美精品 | 全部免费毛片在线播放 | 成人欧美一区二区三区在线播放 | 一区二区三区自拍 | 国产精品一区二 | 欧美精品亚洲精品 | 麻豆91视频 | 成人在线欧美 | 亚洲欧美日韩国产综合 | 免费一级片 | 日韩福利片 | 日日干天天干 | 精品国产污网站污在线观看15 | 香蕉综合久久 | 成人在线免费观看 | 一级大片免费观看 | 亚洲福利电影网 | 成人免费黄色毛片 | 亚洲精品午夜视频 | 91xxx在线观看| 欧洲视频一区 | 激情久久久| 97超碰在线播放 | 日韩av一区二区在线观看 | 国产精品久久精品 | 国产福利在线 | 美女超碰| 欧美狠狠操 | 狠狠av| 欧美精品在线视频 | 久久成人一区二区 | 成人免费毛片高清视频 | www.欧美精品| 欧美亚洲综合久久 | 日韩一二三区视频 | 免费网站看v片在线a |