既然是一個網關。那么全局過濾器肯定是少不了的一個存在。像是鑒權、認證啥的不可能每個服務都做一次,一般都是在網關處就搞定了。
Zuul他就有很強大的過濾器體系來給人使用。
Gateway當然也不會差這么點東西。
對于SpringCloud體系來說,一切的實現都是那么的簡單。那么廢話不多說,直接開始寫起來。
Gateway內部有一個接口 名為GlobalFilter,這個就是Gateway的全局過濾器接口,只要在應用中實現此接口后注冊為Spring的Bean,背后就會幫你將這個實現注冊到全局過濾器鏈條里邊去。
我這里就簡單的寫了個模擬鑒權的過濾器實現:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
@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)); } //最后執行 @Override public int getOrder() { return Ordered.LOWEST_PRECEDENCE; } } |
雖說是鑒權,但實際上我這就是個簡單的demo而已。想知道真正的Spring Security鑒權/認證怎么寫?
我以前寫的這個:https://github.com/skypyb/code_demo/tree/master/spring-security-demo 應該可以幫助你。
看我寫的這個過濾器內部實現哈,其實就是拿出Request Header中的 Authorization字段(token) 然后判斷是否存在。不存在就返回錯誤,存在就交給鏈條中的下一個過濾器。
過濾器其實也沒啥好說的,那么說說限流。
關于限流這個東西,常見的算法就是漏桶和令牌桶了,對于一個應用單機限流來說也復雜不到哪兒去。
靠著google guava包里的RateLimiter工具都能搞定大多數場景了。
不過既然人家Gateway好心好意給你搞了個限流的實現。那么還是尊重他用一下。
由于Gateway是用的Redis和lua腳本實現了令牌桶的算法,那么先導入幾個需要的依賴:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<!--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序列化先:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@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; } } |
萬事俱備,開始進行限流的具體實現了。
既然是限流,那么也得有個限流策略
是根據用戶來限流呢?還是說根據請求路徑限流?或者是IP限流?
不過這個都是由需求來決定了,我這就簡單的寫個根據IP來限流的。
人家也給你封裝完畢了,只需要你自己實現KeyResolver這個接口就可以。
由于實現這個一般來說也就一行代碼,所以我就不寫個單獨的類去實現了,而是直接寫在配置類里邊。
1
2
3
4
5
6
7
8
9
10
11
12
|
@Configuration public class GatewayRateLimiterConfig { /** * Gateway通過內置的RequestRateLimiter過濾器實現限流,用的是令牌桶算法,借助Redis保存中間數據 * 這里自定義一個KeyResolver * 作用是對來源ip進行限流 */ @Bean (value = "ipKeyResolver" ) public KeyResolver ipKeyResolver() { return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress()); } } |
看,我其實只需要返回我需要限制的東西就可以了。我這里提取到用戶的IP將其返回,即可實現通過ip來進行限流的策略。
不過限流相關的配置寫了,那也得用起來。
這個怎么用起來? 其實直接在配置文件里配置就OK了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
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: # 這里是設置與服務注冊發現組件結合,這樣可以采用服務名的路由策略 discovery: locator: enabled: true # 配置路由規則 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參數。
這個是為了指定路由的各種過濾器的。這個參數也有很多種,可以參考官方講解: https://cloud.spring.io/spring-cloud-gateway/2.0.x/single/spring-cloud-gateway.html#gateway-route-filters
我這就是指定了一個RequestRateLimiter,請求限流。
- redis-rate-limiter.burstCapacity: 20
這個參數表示突發容量,即每秒可以最大通過多少次請求
- redis-rate-limiter.replenishRate: 5
這個是令牌桶的補充速度,每秒往桶里邊放幾個令牌
- key-resolver: ‘#{@ipKeyResolver}'
這個就是用上KeyResolver的具體實現了,這里用spel表達式指定我寫的那個ip限制類
準備好之后將應用啟動就完事了,想測的話可以用jmeter測測看,或者將請求限制寫的更小一點,在網頁上狂按f5也行。
以上就是如何為Spring Cloud Gateway加上全局過濾器的詳細內容,更多關于Spring Cloud Gateway添加全局過濾器的資料請關注服務器之家其它相關文章!
原文鏈接:https://www.skypyb.com/2019/10/jishu/1130/