技术文章第一时间送达!

GateWay配置

在mysql定义表gateway_define, 表结构如下面的GatewayDefine实体类:

定义repository和service,采用JPA实现

定义MysqlRouteDefinitionRepository类,实现RouteDefinitionRepository接口的getRouteDefinitions方法,获取从数据库里面装载的路由配置,当然还有save和delete其他方法。

在启动类GatewayServiceApplication中添加两个Bean。 添加ApplicationStartup类,在Spring Boot启动时装载路由配置信息, 说明看注释:

Spring Cloud Gateway是由spring官方基于Spring5.0,Spring Boot2.0,Project Reactor等技术开发的网关,目的是代替原先版本中的Spring Cloud Netfilx Zuul。目前Netfilx已经开源了Zuul2.0,但Spring 没有考虑集成,而是推出了自己开发的Spring Cloud GateWay。

该项目提供了一个构建在Spring Ecosystem之上的API网关,旨在提供一种简单而有效的途径来发送API,并向他们提供交叉关注点:例如:安全性,监控/指标和弹性。在这里废话少说,直接把我实现动态自定义路由的方法托出,共大家参考。由于水平有限,难免有不当或者错误之处,请大家指正,谢谢。

一般的,我们如果使用Spring Cloud GateWay进行配置,类似于下面的样子:

enabled: true

- id: sample-service-a

uri: lb://SAMPLE-SERVICE-A-HA

predicates:

- Path=/customeradd/**

- RewritePath=/customeradd,/customer/add

当我们要新增或者改变一个网关路由时,我们不得不停止网关服务,修改配置文件,保存再重新启动网关服务,这样才能让我们新的设置生效。

设想一样,如果是在生产环境,为了一个小小的路由变更,这样的停止再重启恐怕谁也受不了吧。接下来,看看我们怎么能做到动态配置网关路由,让网关路由配置在服务不需要重启的情况生效。

@Table(name = "gateway_define")

public class GatewayDefine implements Serializable {

@GeneratedValue(strategy = GenerationType.AUTO)

private String id;

private String uri;

private String predicates;

private String filters;

public String getId() {

public void setId(String id) {

this.id = id;

public String getUri() {

return uri;

public void setUri(String uri) {

this.uri = uri;

public String getPredicates() {

return this.predicates;

public void setPredicates(String predicates) {

this.predicates = predicates;

public List<PredicateDefinition> getPredicateDefinition() {

if (this.predicates != null) {

List<PredicateDefinition> predicateDefinitionList = JSON.parseArray(this.predicates, PredicateDefinition.class);

return predicateDefinitionList;

return null;

public String getFilters() {

return filters;

public List<FilterDefinition> getFilterDefinition() {

if (this.filters != null) {

List<FilterDefinition> filterDefinitionList = JSON.parseArray(this.filters, FilterDefinition.class);

return filterDefinitionList;

return null;

public void setFilters(String filters) {

this.filters = filters;

public String toString() {

return "GatewayDefine{" +

"id='" + id + '\'' +

", uri='" + uri + '\'' +

", predicates='" + predicates + '\'' +

", filters='" + filters + '\'' +

其中:id为Eureka注册的服务名; uri、predicates、filters分别应上面配置文件片段中的predicates和filters(这两个保存的都是json)

@Repository

public interface GatewayDefineRepository  extends JpaRepository<GatewayDefine, String> {

List<GatewayDefine> findAll();

GatewayDefine save(GatewayDefine gatewayDefine);

void deleteById(String id);

boolean existsById(String id);

public interface GatewayDefineService {

List<GatewayDefine> findAll() throws Exception;

String loadRouteDefinition() throws Exception;

GatewayDefine save(GatewayDefine gatewayDefine) throws Exception;

void deleteById(String id) throws Exception;

boolean existsById(String id)throws Exception;

public class GatewayDefineServiceImpl implements GatewayDefineService {

GatewayDefineRepository gatewayDefineRepository;

private GatewayDefineService gatewayDefineService;

private RouteDefinitionWriter routeDefinitionWriter;

private ApplicationEventPublisher publisher;

public List<GatewayDefine> findAll() throws Exception {

return gatewayDefineRepository.findAll();

public String loadRouteDefinition() {

List<GatewayDefine> gatewayDefineServiceAll = gatewayDefineService.findAll();

if (gatewayDefineServiceAll == null) {

return "none route defined";

for (GatewayDefine gatewayDefine : gatewayDefineServiceAll) {

RouteDefinition definition = new RouteDefinition();

definition.setId(gatewayDefine.getId());

definition.setUri(new URI(gatewayDefine.getUri()));

List<PredicateDefinition> predicateDefinitions = gatewayDefine.getPredicateDefinition();

if (predicateDefinitions != null) {

definition.setPredicates(predicateDefinitions);

List<FilterDefinition> filterDefinitions = gatewayDefine.getFilterDefinition();

if (filterDefinitions != null) {

definition.setFilters(filterDefinitions);

routeDefinitionWriter.save(Mono.just(definition)).subscribe();

this.publisher.publishEvent(new RefreshRoutesEvent(this));

return "success";

} catch (Exception e) {

e.printStackTrace();

return "failure";

public GatewayDefine save(GatewayDefine gatewayDefine) throws Exception {

gatewayDefineRepository.save(gatewayDefine);

return gatewayDefine;

public void deleteById(String id) throws Exception {

gatewayDefineRepository.deleteById(id);

public boolean existsById(String id) throws Exception {

return gatewayDefineRepository.existsById(id);

loadRouteDefinition是重点,它从数据库里获取动态定义的路由,最后封装成RouteDefinition 类实例,调用RouteDefinitionWriter 的save方法保存。

RouteDefinitionWriter是个接口,真正实现的是InMemoryRouteDefinitionRepository类,在InMemoryRouteDefinitionRepository定义了一个SynchronizedMap 类,所有的设置都在这儿保存。

public class MysqlRouteDefinitionRepository implements RouteDefinitionRepository {

private GatewayDefineService gatewayDefineService;

public Flux<RouteDefinition> getRouteDefinitions() {

List<GatewayDefine> gatewayDefineList = gatewayDefineService.findAll();

Map<String, RouteDefinition> routes = new LinkedHashMap<String, RouteDefinition>();

for (GatewayDefine gatewayDefine: gatewayDefineList) {

RouteDefinition definition = new RouteDefinition();

definition.setId(gatewayDefine.getId());

definition.setUri(new URI(gatewayDefine.getUri()));

List<PredicateDefinition> predicateDefinitions = gatewayDefine.getPredicateDefinition();

if (predicateDefinitions != null) {

definition.setPredicates(predicateDefinitions);

List<FilterDefinition> filterDefinitions = gatewayDefine.getFilterDefinition();

if (filterDefinitions != null) {

definition.setFilters(filterDefinitions);

routes.put(definition.getId(), definition);

return Flux.fromIterable(routes.values());

} catch (Exception e) {

e.printStackTrace();

return Flux.empty();

public Mono<Void> save(Mono<RouteDefinition> route) {

return route.flatMap(r -> {

GatewayDefine gatewayDefine = new GatewayDefine();

gatewayDefine.setId(r.getId());

gatewayDefine.setUri(r.getUri().toString());

gatewayDefine.setPredicates(JSON.toJSONString(r.getPredicates()));

gatewayDefine.setFilters(JSON.toJSONString(r.getFilters()));

gatewayDefineService.save(gatewayDefine);

return Mono.empty();

} catch (Exception e) {

e.printStackTrace();

return Mono.defer(() -> Mono.error(new NotFoundException("RouteDefinition save error: "+ r.getId())));

public Mono<Void> delete(Mono<String> routeId) {

return routeId.flatMap(id -> {

gatewayDefineService.deleteById(id);

return Mono.empty();

} catch (Exception e) {

e.printStackTrace();

return Mono.defer(() -> Mono.error(new NotFoundException("RouteDefinition delete error: " + routeId)));

public RouteDefinitionWriter routeDefinitionWriter() {

return new InMemoryRouteDefinitionRepository();

public MysqlRouteDefinitionRepository mysqlRouteDefinitionRepository() {

return new MysqlRouteDefinitionRepository();

* 在Spring Boot程序启动后会检测程序中是否有CommandLineRunner

* 和ApplicationRunner接口的实例,

* 如果存在,则会执行对应实现类中的run()方法,而且只执行一次

public class ApplicationStartup implements ApplicationRunner {

private GatewayDefineService gatewayDefineService;

public void run(ApplicationArguments args) throws Exception {

gatewayDefineService.loadRouteDefinition();

我这里面没有界面设置路由,我是在配置文件中配置我要的路由,然后通过 /actuator/gateway/routes 获取所有路由的json, 也可以通过 /actuator/gateway/routes/{id} 获取单独一个路由的json.然后手工往数据库里面插入数据,再把网关服务停止,删除配置文件中的路由设定,再重新启动网关功能,通过 /actuator/gateway/routes 能够获取同样的路由json, 通过curl访问设置的路由同样生效。

当然完全可以独立开发一个应用,有界面来读取数据库中的路由配置,可以增加和修改路由信息。再通过Spring Cloud Config的配置来刷新多个网关路由的信息,实现多个网关服务的路由信息实时更新。反正有各种方法可供选择。

完全是记录自己前一段时间的研究心得。水平有限,有什么不对的地方请大家指正。还有,Spring Cloud GateWay还不支持OAuth2, 所以想统一集成授权、认证等功能的还是使用ZUUL吧。下一次有时间,我会写一下ZUUL的动态路由功能实现以及避免频繁刷新路由信息。反正和GateWay相似,但是还是有区别的。

实现参考了网上很多人的源码和文章,在此表示感谢!也阅读了Spring Cloud GateWay 部分源代码,对Spring Cloud GateWay有了一定的认识,聊以自慰。

最新的Spring Cloud Greenwich.RELEASE中Gateway 过滤器新增支持OAuth2,我觉得可以抛弃ZUUL了。

更多Java技术文章,尽在【Java知音】网站。

网址:  ,搜索Java知音可达!