Spring Cloud Gateway官方教程讲的都是提前在配置文件中配置网关,实际项目中,Spring Cloud Gateway作为微服务的入口,需要尽量避免重启,所以我们需要在Spring Cloud Gateway运行时动态配置网关。
Spring Cloud Gateway自带接口
Spring Cloud Gateway在2018年6月份发布了2.0第一个release版本,官方文档并没有讲如何动态配置,翻看Spring Cloud Gateway源码,发现类org.springframework.cloud.gateway.actuate.GatewayControllerEndpoint中提供了网关配置Restful接口,默认没有启用。在类org.springframework.cloud.gateway.config.GatewayAutoConfiguration中配置了GatewayControllerEndpoint
@Configuration
@ConditionalOnClass(Health.class)
protected static class GatewayActuatorConfiguration {
@Bean
@ConditionalOnEnabledEndpoint
public GatewayControllerEndpoint gatewayControllerEndpoint(RouteDefinitionLocator routeDefinitionLocator, List<GlobalFilter> globalFilters,
List<GatewayFilterFactory> GatewayFilters, RouteDefinitionWriter routeDefinitionWriter,
RouteLocator routeLocator) {
return new GatewayControllerEndpoint(routeDefinitionLocator, globalFilters, GatewayFilters, routeDefinitionWriter, routeLocator);
}
}
存在org.springframework.boot.actuate.health.Health时启用,添加actuator依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
在gateway项目的application.yml中启用gateway api:
#开启actuator管理api,后面要关闭
management:
endpoints:
web:
exposure:
include: "*"
访问 http://localhost:网关端口/actuator/gateway/routes,返回了当前网关的路由信息。
编码方式
不过这里我们不打算用自带的Restful接口,一来官方文档也没说新增网关参数怎么传,再者我们也不希望在网关暴露这些接口。参照org.springframework.cloud.gateway.actuate.GatewayControllerEndpoint,我们自己编程来动态改变网关。
直接上代码:
import java.net.URI;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.cloud.gateway.route.RouteDefinitionWriter;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;
import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Mono;
@Service
public class TestGatewayService implements ApplicationEventPublisherAware {
@Autowired
private RouteDefinitionWriter routeDefinitionWriter;
private ApplicationEventPublisher publisher;
public String save() {
RouteDefinition definition = new RouteDefinition();
PredicateDefinition predicate = new PredicateDefinition();
Map<String, String> predicateParams = new HashMap<>(8);
definition.setId("baiduRoute");
predicate.setName("Path");
predicateParams.put("pattern", "/baidu");
predicateParams.put("pathPattern", "/baidu");
predicate.setArgs(predicateParams);
definition.setPredicates(Arrays.asList(predicate));
URI uri = UriComponentsBuilder.fromHttpUrl("http://www.baidu.com").build().toUri();
definition.setUri(uri);
routeDefinitionWriter.save(Mono.just(definition)).subscribe();
this.publisher.publishEvent(new RefreshRoutesEvent(this));
return "success";
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.publisher = applicationEventPublisher;
}
}
代码说明:
1、predicate.setName("Path"); 设置predicat名称,这个名称不是乱起的,Spring会根据名称去查找对应的FilterFactory,目前支持的名称有:After、Before、Between、Cookie、Header、Host、Method、Path、Query、RemoteAddr。
对应官方文档的Route Predicate Factories(http://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.0.0.RELEASE/single/spring-cloud-gateway.html)
2、definition.setId("baiduRoute"); 设置definition的id,需要全局唯一,默认使用UUID。
3、predicateParams.put("pattern", "/baidu"); 每个Route Predicate的参数不同,详情在官网文档查看对应的Route Predicate配置示例,然而官方文档也很坑,比如Path Route的- Path=/foo/{segment},把参数给省略了。还是得看源码,在包org.springframework.cloud.gateway.handler.predicate里有Spring Cloud Gateway所有的Predicate,打开对应的RoutePredicateFactory,内部类Config就是该Predicate支持的参数。
4、routeDefinitionWriter.save(Mono.just(definition)).subscribe(); 默认的RouteDefinitionWriter实现类是org.springframework.cloud.gateway.route.InMemoryRouteDefinitionRepository。注意最后一定要调用subscribe(),否则不执行。
至此已编码动态配置了一个基本的网关。
带Filter的配置代码:
RouteDefinition routeDefinition = new RouteDefinition();
PredicateDefinition predicateDefinition = new PredicateDefinition();
Map<String, String> predicateParams = new HashMap<>(8);
Map<String, String> filterParams = new HashMap<>(8);
FilterDefinition filterDefinition = new FilterDefinition();
URI uri = UriComponentsBuilder.fromUriString("lb://HELLO-SERVICE").build().toUri();
routeDefinition.setId("rateLimitTest");
// 名称是固定的,spring gateway会根据名称找对应的PredicateFactory
predicateDefinition.setName("Path");
predicateParams.put("pattern", "/rate/**");
predicateDefinition.setArgs(predicateParams);
// 名称是固定的,spring gateway会根据名称找对应的FilterFactory
filterDefinition.setName("RequestRateLimiter");
// 每秒最大访问次数
filterParams.put("redis-rate-limiter.replenishRate", "2");
// 令牌桶最大容量
filterParams.put("redis-rate-limiter.burstCapacity", "3");
// 限流策略(#{@BeanName})
filterParams.put("key-resolver", "#{@hostAddressKeyResolver}");
// 自定义限流器(#{@BeanName})
//filterParams.put("rate-limiter", "#{@redisRateLimiter}");
filterDefinition.setArgs(filterParams);
routeDefinition.setPredicates(Arrays.asList(predicateDefinition));
routeDefinition.setFilters(Arrays.asList(filterDefinition));
routeDefinition.setUri(uri);
routeDefinitionWriter.save(Mono.just(routeDefinition)).subscribe();
publisher.publishEvent(new RefreshRoutesEvent(this));
需要注意的是filterParams.put("key-resolver", "#{@hostAddressKeyResolver}"); hostAddressKeyResolver是我自定义的Spring Bean,#{@BeanName}是Spring的表达式,用来注入Bean。
自定义RouteDefinitionWriter
Spring Cloud Gateway默认的RouteDefinitionWriter实现类是org.springframework.cloud.gateway.route.InMemoryRouteDefinitionRepository,Route信息保存在当前实例的内存中,这在集群环境中会存在同步问题。我们可以自定义一个基于Redis的RouteDefinitionWriter。
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.cloud.gateway.route.RouteDefinitionRepository;
import org.springframework.cloud.gateway.support.NotFoundException;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSON;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
/**
* 使用Redis保存自定义路由配置(代替默认的InMemoryRouteDefinitionRepository)
* <p/>
* 存在问题:每次请求都会调用getRouteDefinitions,当网关较多时,会影响请求速度,考虑放到本地Map中,使用消息通知Map更新。
*
* @since 2018年7月9日 下午2:39:02
*/
@Component
public class RedisRouteDefinitionRepository implements RouteDefinitionRepository {
public static final String GATEWAY_ROUTES = "geteway_routes";
@Autowired
private StringRedisTemplate redisTemplate;
@Override
public Flux<RouteDefinition> getRouteDefinitions() {
List<RouteDefinition> routeDefinitions = new ArrayList<>();
redisTemplate.opsForHash().values(GATEWAY_ROUTES).stream().forEach(routeDefinition -> {
routeDefinitions.add(JSON.parseObject(routeDefinition.toString(), RouteDefinition.class));
});
return Flux.fromIterable(routeDefinitions);
}
@Override
public Mono<Void> save(Mono<RouteDefinition> route) {
return route
.flatMap(routeDefinition -> {
redisTemplate.opsForHash().put(GATEWAY_ROUTES, routeDefinition.getId(),
JSON.toJSONString(routeDefinition));
return Mono.empty();
});
}
@Override
public Mono<Void> delete(Mono<String> routeId) {
return routeId.flatMap(id -> {
if (redisTemplate.opsForHash().hasKey(GATEWAY_ROUTES, id)) {
redisTemplate.opsForHash().delete(GATEWAY_ROUTES, id);
return Mono.empty();
}
return Mono.defer(() -> Mono.error(new NotFoundException("RouteDefinition not found: " + routeId)));
});
}
}