真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

[Springcloud一步步實(shí)現(xiàn)廣告系統(tǒng)]3.網(wǎng)關(guān)路由

Zuul(Router and Filter)

WIKI: 傳送門

站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到中原網(wǎng)站設(shè)計(jì)與中原網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都做網(wǎng)站、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、空間域名、網(wǎng)站空間、企業(yè)郵箱。業(yè)務(wù)覆蓋中原地區(qū)。

[Spring cloud 一步步實(shí)現(xiàn)廣告系統(tǒng)] 3. 網(wǎng)關(guān)路由

作用
  1. 認(rèn)證,鑒權(quán)(Authentication/Security)
  2. 預(yù)判(Insights)
  3. 壓力測(cè)試(Stress Testing)
  4. 灰度/金絲雀測(cè)試(Canary Testing)
  5. 動(dòng)態(tài)路由(Dynamic Routing)
  6. 服務(wù)遷移(Service Migration)
  7. 降低負(fù)載(Load Shedding)
  8. 靜態(tài)響應(yīng)處理(Static Response handling)
  9. 主動(dòng)/主動(dòng)交換管理(Active/Active traffic management)

關(guān)鍵配置:

The configuration property zuul.host.maxTotalConnections and zuul.host.maxPerRouteConnections, which default to 200 and 20 respectively.

創(chuàng)建mscx-ad-zuul

三步曲創(chuàng)建法:

添加依賴
    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-zuul
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
    
加注解
@SpringCloudApplication
@EnableZuulProxy //啟用網(wǎng)關(guān)
public class GatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}
改配置
spring:
  application:
    name: ad-gateway-zuul
server:
  port: 1111
eureka:
  client:
    service-url:
      defaultZone: http://server1:7777/eureka/,http://server2:8888/eureka/,http://server3:9999/eureka/
  instance:
    hostname: ad-gateway-zuul
zuul:
  ignored-services: '*' # 過(guò)濾所有請(qǐng)求,除了下面routes中聲明過(guò)的服務(wù)
  routes:
    sponsor: #在路由中自定義服務(wù)路由名稱
      path: /ad-sponsor/**
      serviceId: mscx-ad-sponsor #微服務(wù)name
      strip-prefix: false
    search: #在路由中自定義服務(wù)路由名稱
      path: /ad-search/**
      serviceId: mscx-ad-search #微服務(wù)name
      strip-prefix: false
  prefix: /gateway/api
  strip-prefix: false #不對(duì) prefix: /gateway/api 設(shè)置的路徑進(jìn)行截取,默認(rèn)轉(zhuǎn)發(fā)會(huì)截取掉配置的前綴
過(guò)濾器編寫

我們來(lái)編寫一個(gè)記錄請(qǐng)求時(shí)間周期的過(guò)濾器,根據(jù)Filter的三種類型:Pre filters,routing filtersPost filters,我們需要定義2個(gè)filter,用來(lái)記錄開始和結(jié)束時(shí)間,很明顯,我們需要實(shí)現(xiàn)Pre & Post2個(gè)過(guò)濾器。

@Slf4j
@Component
public class PreRequestFilter extends ZuulFilter {
    @Override
    public String filterType() {
        // pre filter
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        //獲取當(dāng)前請(qǐng)求的請(qǐng)求上下文
        RequestContext requestContext = RequestContext.getCurrentContext();
        //記錄請(qǐng)求進(jìn)入時(shí)間
        requestContext.set("api_request_time", System.currentTimeMillis());
        return null;
    }
}

---

@Slf4j
@Component
public class AccessLogFilter extends ZuulFilter {

    @Override
    public String filterType() {
        return FilterConstants.POST_TYPE;
    }

    @Override
    public int filterOrder() {
        //需要最后一個(gè)執(zhí)行的filter
        return FilterConstants.SEND_RESPONSE_FILTER_ORDER - 1;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletRequest request = requestContext.getRequest();
        log.info("Request \"{}\" spent : {} seconds.", request.getRequestURI(),
                (System.currentTimeMillis() - Long.valueOf(requestContext.get("api_request_time").toString())) / 1000);
        return null;
    }
}
Gateway

后續(xù)更新


文章標(biāo)題:[Springcloud一步步實(shí)現(xiàn)廣告系統(tǒng)]3.網(wǎng)關(guān)路由
URL網(wǎng)址:http://weahome.cn/article/pgscso.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部