您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“怎么為Spring Cloud Gateway加上全局過濾器”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
既然是一個網(wǎng)關(guān)。那么全局過濾器肯定是少不了的一個存在。像是鑒權(quán)、認(rèn)證啥的不可能每個服務(wù)都做一次,一般都是在網(wǎng)關(guān)處就搞定了。
Zuul他就有很強(qiáng)大的過濾器體系來給人使用。
Gateway當(dāng)然也不會差這么點東西。
對于SpringCloud體系來說,一切的實現(xiàn)都是那么的簡單。那么廢話不多說,直接開始寫起來。
Gateway內(nèi)部有一個接口 名為GlobalFilter,這個就是Gateway的全局過濾器接口,只要在應(yīng)用中實現(xiàn)此接口后注冊為Spring的Bean,背后就會幫你將這個實現(xiàn)注冊到全局過濾器鏈條里邊去。
我這里就簡單的寫了個模擬鑒權(quá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"); // 將信息轉(zhuǎn)換為 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; } }
雖說是鑒權(quán),但實際上我這就是個簡單的demo而已。想知道真正的Spring Security鑒權(quán)/認(rèn)證怎么寫?
我以前寫的這個:https://github.com/skypyb/code_demo/tree/master/spring-security-demo 應(yīng)該可以幫助你。
看我寫的這個過濾器內(nèi)部實現(xiàn)哈,其實就是拿出Request Header中的 Authorization字段(token) 然后判斷是否存在。不存在就返回錯誤,存在就交給鏈條中的下一個過濾器。
過濾器其實也沒啥好說的,那么說說限流。
關(guān)于限流這個東西,常見的算法就是漏桶和令牌桶了,對于一個應(yīng)用單機(jī)限流來說也復(fù)雜不到哪兒去。
靠著google guava包里的RateLimiter工具都能搞定大多數(shù)場景了。
不過既然人家Gateway好心好意給你搞了個限流的實現(xiàn)。那么還是尊重他用一下。
由于Gateway是用的Redis和lua腳本實現(xiàn)了令牌桶的算法,那么先導(dǎo)入幾個需要的依賴:
<!--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; } }
萬事俱備,開始進(jìn)行限流的具體實現(xiàn)了。
既然是限流,那么也得有個限流策略
是根據(jù)用戶來限流呢?還是說根據(jù)請求路徑限流?或者是IP限流?
不過這個都是由需求來決定了,我這就簡單的寫個根據(jù)IP來限流的。
人家也給你封裝完畢了,只需要你自己實現(xiàn)KeyResolver這個接口就可以。
由于實現(xiàn)這個一般來說也就一行代碼,所以我就不寫個單獨的類去實現(xiàn)了,而是直接寫在配置類里邊。
@Configuration public class GatewayRateLimiterConfig { /** * Gateway通過內(nèi)置的RequestRateLimiter過濾器實現(xiàn)限流,用的是令牌桶算法,借助Redis保存中間數(shù)據(jù) * 這里自定義一個KeyResolver * 作用是對來源ip進(jìn)行限流 */ @Bean(value = "ipKeyResolver") public KeyResolver ipKeyResolver() { return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress()); } }
看,我其實只需要返回我需要限制的東西就可以了。我這里提取到用戶的IP將其返回,即可實現(xiàn)通過ip來進(jìn)行限流的策略。
不過限流相關(guān)的配置寫了,那也得用起來。
這個怎么用起來? 其實直接在配置文件里配置就OK了
spring: application: # 應(yīng)用名稱 name: sc-demo-alibaba-gateway cloud: nacos: discovery: server-addr: 192.168.3.105:8848 #注冊進(jìn)nacos # 使用 Sentinel 作為熔斷器 sentinel: transport: port: 18102 dashboard: 192.168.3.105:8858 # 路由網(wǎng)關(guān)配置 gateway: # 這里是設(shè)置與服務(wù)注冊發(fā)現(xiàn)組件結(jié)合,這樣可以采用服務(wù)名的路由策略 discovery: locator: enabled: true # 配置路由規(guī)則 routes: - id: ROUTER#sc-demo-alibaba-consumer #這個是路由ID,需要保證在所有路由定義中唯一,值隨便寫就是了 # 采用 LoadBalanceClient 方式請求,以 lb:// 開頭,后面的是注冊在 Nacos 上的服務(wù)名 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: "*" # 配置日志級別,方別調(diào)試 logging: level: org.springframework.cloud.gateway: debug
這里可以看到,除了Redis配置 ( spring.redis.** )以外。
主要就是對于Getway的配置。
在Gateway路由配置中,設(shè)置了一個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
這個是令牌桶的補(bǔ)充速度,每秒往桶里邊放幾個令牌
key-resolver: ‘#{@ipKeyResolver}'
這個就是用上KeyResolver的具體實現(xiàn)了,這里用spel表達(dá)式指定我寫的那個ip限制類
準(zhǔn)備好之后將應(yīng)用啟動就完事了,想測的話可以用jmeter測測看,或者將請求限制寫的更小一點,在網(wǎng)頁上狂按f5也行。
“怎么為Spring Cloud Gateway加上全局過濾器”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。