最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2019年01月11日 09:32:21   作者:huanzi-qch  
這篇文章主要介紹了SpringCloud Zuul實(shí)現(xiàn)動(dòng)態(tài)路由,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(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

代碼編寫

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

 <!-- Zuul -->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
    </dependency>

配置文件

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ī)則后續(xù)慢慢健全

/**
 * Zuul過濾器,實(shí)現(xiàn)了路由檢查
 */
public class AccessFilter extends ZuulFilter {

  /**
   * 通過int值來定義過濾器的執(zhí)行順序
   */
  @Override
  public int filterOrder() {
    // PreDecoration之前運(yùn)行
    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");
    //有權(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類型來判斷該過濾器是否要執(zhí)行
   */
  @Override
  public boolean shouldFilter() {
    return true;
  }
}

啟動(dòng)類

添加@EnableZuulProxy注解并使用自定義過濾器

@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

瀏覽器訪問 http://localhost:10010/myspringboot/feign/ribbon、http://localhost:10010/myspringboot/feign/ribbon?accessToken=123456

http://localhost:10010/ 這個(gè)端口對(duì)外暴露,相對(duì)于總?cè)肟?,后面接不同的路徑由,Zuul路由到對(duì)應(yīng)的服務(wù)上

1、沒有accessToken是,無法通過檢查

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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

威远县| 鹤峰县| 珲春市| 呼和浩特市| 香港 | 崇州市| 彭泽县| 嘉禾县| 象山县| 武汉市| 安庆市| 巴楚县| 临猗县| 石首市| 宽甸| 大竹县| 涡阳县| 荥经县| 夏河县| 江永县| 大洼县| 东海县| 哈密市| 东方市| 浙江省| 南澳县| 衡阳市| 余庆县| 霸州市| 河曲县| 阳西县| 乌海市| 鸡西市| 老河口市| 宝兴县| 永兴县| 呼伦贝尔市| 汉川市| 洛川县| 德令哈市| 江达县|