如何為Spring Cloud Gateway加上全局過濾器
既然是一個網關。那么全局過濾器肯定是少不了的一個存在。像是鑒權、認證啥的不可能每個服務都做一次,一般都是在網關處就搞定了。
Zuul他就有很強大的過濾器體系來給人使用。
Gateway當然也不會差這么點東西。
對于SpringCloud體系來說,一切的實現(xiàn)都是那么的簡單。那么廢話不多說,直接開始寫起來。
Gateway內部有一個接口 名為GlobalFilter,這個就是Gateway的全局過濾器接口,只要在應用中實現(xiàn)此接口后注冊為Spring的Bean,背后就會幫你將這個實現(xiàn)注冊到全局過濾器鏈條里邊去。
我這里就簡單的寫了個模擬鑒權的過濾器實現(xiàn):
@Component public class AuthFilter implements GlobalFilter, Ordered { @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { String token = exchange.getRequest().getHeaders().getFirst("Authorization"); //不為空則通過 if (!StringUtils.isEmpty(token)) return chain.filter(exchange); ServerHttpResponse response = exchange.getResponse(); // 封裝錯誤信息 Map<String, Object> responseData = Maps.newHashMapWithExpectedSize(3); responseData.put("code", HttpStatus.UNAUTHORIZED.value()); responseData.put("message", "Token is empty"); responseData.put("cause", "Token is empty"); // 將信息轉換為 JSON ObjectMapper objectMapper = new ObjectMapper(); byte[] data = new byte[0]; try { data = objectMapper.writeValueAsBytes(responseData); } catch (JsonProcessingException e) { e.printStackTrace(); } // 返回錯誤信息json DataBuffer buffer = response.bufferFactory().wrap(data); response.setStatusCode(HttpStatus.UNAUTHORIZED); response.getHeaders().add("Content-Type", "application/json;charset=UTF-8"); return response.writeWith(Mono.just(buffer)); } //最后執(zhí)行 @Override public int getOrder() { return Ordered.LOWEST_PRECEDENCE; } }
雖說是鑒權,但實際上我這就是個簡單的demo而已。想知道真正的Spring Security鑒權/認證怎么寫?
我以前寫的這個:https://github.com/skypyb/code_demo/tree/master/spring-security-demo 應該可以幫助你。
看我寫的這個過濾器內部實現(xiàn)哈,其實就是拿出Request Header中的 Authorization字段(token) 然后判斷是否存在。不存在就返回錯誤,存在就交給鏈條中的下一個過濾器。
過濾器其實也沒啥好說的,那么說說限流。
關于限流這個東西,常見的算法就是漏桶和令牌桶了,對于一個應用單機限流來說也復雜不到哪兒去。
靠著google guava包里的RateLimiter工具都能搞定大多數(shù)場景了。
不過既然人家Gateway好心好意給你搞了個限流的實現(xiàn)。那么還是尊重他用一下。
由于Gateway是用的Redis和lua腳本實現(xiàn)了令牌桶的算法,那么先導入幾個需要的依賴:
<!--Redis begin--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis-reactive</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> <!--Redis end-->
既然是Redis,那還是先配一下Redis序列化先:
@Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(stringRedisSerializer); redisTemplate.setHashKeySerializer(stringRedisSerializer); redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class)); redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.setConnectionFactory(connectionFactory); return redisTemplate; } }
萬事俱備,開始進行限流的具體實現(xiàn)了。
既然是限流,那么也得有個限流策略
是根據(jù)用戶來限流呢?還是說根據(jù)請求路徑限流?或者是IP限流?
不過這個都是由需求來決定了,我這就簡單的寫個根據(jù)IP來限流的。
人家也給你封裝完畢了,只需要你自己實現(xiàn)KeyResolver這個接口就可以。
由于實現(xiàn)這個一般來說也就一行代碼,所以我就不寫個單獨的類去實現(xiàn)了,而是直接寫在配置類里邊。
@Configuration public class GatewayRateLimiterConfig { /** * Gateway通過內置的RequestRateLimiter過濾器實現(xiàn)限流,用的是令牌桶算法,借助Redis保存中間數(shù)據(jù) * 這里自定義一個KeyResolver * 作用是對來源ip進行限流 */ @Bean(value = "ipKeyResolver") public KeyResolver ipKeyResolver() { return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress()); } }
看,我其實只需要返回我需要限制的東西就可以了。我這里提取到用戶的IP將其返回,即可實現(xiàn)通過ip來進行限流的策略。
不過限流相關的配置寫了,那也得用起來。
這個怎么用起來? 其實直接在配置文件里配置就OK了
spring: application: # 應用名稱 name: sc-demo-alibaba-gateway cloud: nacos: discovery: server-addr: 192.168.3.105:8848 #注冊進nacos # 使用 Sentinel 作為熔斷器 sentinel: transport: port: 18102 dashboard: 192.168.3.105:8858 # 路由網關配置 gateway: # 這里是設置與服務注冊發(fā)現(xiàn)組件結合,這樣可以采用服務名的路由策略 discovery: locator: enabled: true # 配置路由規(guī)則 routes: - id: ROUTER#sc-demo-alibaba-consumer #這個是路由ID,需要保證在所有路由定義中唯一,值隨便寫就是了 # 采用 LoadBalanceClient 方式請求,以 lb:// 開頭,后面的是注冊在 Nacos 上的服務名 uri: lb://sc-demo-alibaba-consumer predicates: # Method ,這里是匹配 GET 和 POST 請求 - Method=GET,POST filters: - name: RequestRateLimiter args: redis-rate-limiter.burstCapacity: 20 redis-rate-limiter.replenishRate: 5 key-resolver: '#{@ipKeyResolver}' - id: ROUTER#sc-demo-alibaba-provider uri: lb://sc-demo-alibaba-provider predicates: - Method=GET,POST #Redis配置 redis: host: 192.168.3.105 port: 6379 #Redis連接池配置 jedis: pool: min-idle: 0 max-idle: 8 max-active: 8 max-wait: -1ms server: port: 8888 feign: sentinel: enabled: true management: endpoints: web: exposure: include: "*" # 配置日志級別,方別調試 logging: level: org.springframework.cloud.gateway: debug
這里可以看到,除了Redis配置 ( spring.redis.** )以外。
主要就是對于Getway的配置。
在Gateway路由配置中,設置了一個filters參數(shù)。
這個是為了指定路由的各種過濾器的。這個參數(shù)也有很多種,可以參考官方講解: https://cloud.spring.io/spring-cloud-gateway/2.0.x/single/spring-cloud-gateway.html#gateway-route-filters
我這就是指定了一個RequestRateLimiter,請求限流。
- redis-rate-limiter.burstCapacity: 20
這個參數(shù)表示突發(fā)容量,即每秒可以最大通過多少次請求
- redis-rate-limiter.replenishRate: 5
這個是令牌桶的補充速度,每秒往桶里邊放幾個令牌
- key-resolver: ‘#{@ipKeyResolver}'
這個就是用上KeyResolver的具體實現(xiàn)了,這里用spel表達式指定我寫的那個ip限制類
準備好之后將應用啟動就完事了,想測的話可以用jmeter測測看,或者將請求限制寫的更小一點,在網頁上狂按f5也行。
以上就是如何為Spring Cloud Gateway加上全局過濾器的詳細內容,更多關于Spring Cloud Gateway添加全局過濾器的資料請關注腳本之家其它相關文章!
相關文章
idea新建Springboot項目,設置默認maven和jdk版本方式
這篇文章主要介紹了idea新建Springboot項目,設置默認maven和jdk版本方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12Apache Dubbo的SPI機制是如何實現(xiàn)的
SPI全稱為Service Provider Interface,對應中文為服務發(fā)現(xiàn)機制。SPI類似一種可插拔機制,首先需要定義一個接口或一個約定,然后不同的場景可以對其進行實現(xiàn),調用方在使用的時候無需過多關注具體的實現(xiàn)細節(jié)。在Java中,SPI體現(xiàn)了面向接口編程的思想,滿足開閉設計原則。2021-06-06