spring cloud gateway原始碼解析

2021-09-26 03:53:18 字數 3786 閱讀 8259

1.zuul是基於servlet 2.5,相容servlet3.0,使用的是阻塞api,不支援長連線如websocket

2.gateway基於spring5,reactor和spring boot2使用了非阻塞api,支援websocket,和spring完美整合,對開發者友好。

3.zuul2支援非阻塞api,但沒有和spring cloud整合,且已經停止維護,不在考慮之列。

看spring-cloud-gateway-core包下的 spring.factories

# auto configure

org.springframework.boot.autoconfigure.enableautoconfiguration=\

org.springframework.cloud.gateway.config.gatewayclasspathwarningautoconfiguration,\

org.springframework.cloud.gateway.config.gatewayautoconfiguration,\

org.springframework.cloud.gateway.config.gatewayloadbalancerclientautoconfiguration,\

org.springframework.cloud.gateway.config.gatewaynoloadbalancerclientautoconfiguration,\

org.springframework.cloud.gateway.config.gatewaymetricsautoconfiguration,\

org.springframework.cloud.gateway.config.gatewayredisautoconfiguration,\

org.springframework.cloud.gateway.discovery.gatewaydiscoveryclientautoconfiguration

org.springframework.boot.env.environmentpostprocessor=\

org.springframework.cloud.gateway.config.gatewayenvironmentpostprocessor

package org.springframework.cloud.gateway.config;

import org.apache.commons.logging.log;

import org.apache.commons.logging.logfactory;

import org.springframework.boot.autoconfigure.autoconfigurebefore;

import org.springframework.boot.autoconfigure.condition.conditionalonclass;

import org.springframework.boot.autoconfigure.condition.conditionalo****singclass;

import org.springframework.context.annotation.configuration;

@configuration

@autoconfigurebefore(gatewayautoconfiguration.class)

public class gatewayclasspathwarningautoconfiguration

} @configuration

@conditionalo****singclass("org.springframework.web.reactive.dispatcherhandler")

protected static class webfluxmissingfromclasspathconfiguration

}}

解釋:1.若存在springmvc的前端控制器org.springframework.web.servlet.dispatcherservlet,則報警,原因是使用非阻塞的api。

2.若不存在spring webflux的前端控制器org.springframework.web.reactive.dispatcherhandler,則報警,原因是需要使用非阻塞的api。

@bean

filteringwebhandler webhandler, routelocator routelocator,

globalcorsproperties globalcorsproperties, environment environment)

@override

protected mono> gethandlerinternal(serverwebexchange exchange)

return lookuproute(exchange)

.flatmap((function>) r ->

exchange.getattributes().put(gateway_route_attr, r);

return mono.just(webhandler);

}).switchifempty(mono.empty().then(mono.fromrunnable(() ->

})));

}

protected monolookuproute(serverwebexchange exchange) )

// instead of immediately stopping main flux due to error, log and

// swallow it

.doonerror(e -> logger.error(

e)).onerrorresume(e -> mono.empty()))

// .defaultifempty() put a static route not found

// or .switchifempty()

// .switchifempty(mono.empty().log("noroute"))

.next()

// todo: error handling

.map(route ->

validateroute(route, exchange);

return route;

});/*

* todo: trace logging if (logger.istraceenabled()) */}

1.通過routelocator返回route

關於route的定義

可以看出url和gatewayfilter進行了繫結

2.將route放入serverwebexchange的屬性中。

@bean

public filteringwebhandler filteringwebhandler(listglobalfilters)

azkaban web server原始碼解析

azkaban主要用於hadoop相關job任務的排程,但也可以應用任何需要排程管理的任務,可以完全代替crontab。azkaban主要分為web server 任務上傳,管理,排程 executor server 接受web server的排程指令,進行任務執行 1.資料表 projects 工...

JDK LinkedHashMap原始碼解析

今天來分析一下jdk linkedhashmap的源 public class linkedhashmapextends hashmapimplements map可以看到,linkedhashmap繼承自hashmap,並且也實現了map介面,所以linkedhashmap沿用了hashmap的大...

Redux原始碼createStore解讀常用方法

const store createstore reducer,preloadedstate enhancer 直接返回當前currentstate,獲取state值,return state 我覺得應該深轉殖乙個新的物件返回,不然有可能會被外部修改 function getstate consol...