Skip to content

如何自定义实现基于域名前缀的环境路由隔离

Neptune edited this page Sep 23, 2022 · 1 revision

集成篇

概念篇

实践篇

功能篇

配置篇

扩展篇

二次开发篇

升级篇

贡献篇

Clone this wiki locally

实现基于域名前缀的环境路由隔离的功能,需要继承RouteFilter,例如,在Spring Cloud Gateway上继承DefaultGatewayStrategyRouteFilter

public class MyGatewayStrategyRouteFilter extends DefaultGatewayStrategyRouteFilter {
    private static final Logger LOG = LoggerFactory.getLogger(MyGatewayStrategyRouteFilter.class);

    // 自定义根据域名全链路环境隔离
    @Override
    public String getRouteEnvironment() {
        String host = gatewayStrategyContextHolder.getURI().getHost();
        if (host.contains("nepxion.com")) {
            LOG.info("自定义根据域名全链路环境隔离, URL={}", host);

            String environment = host.substring(0, host.indexOf("."));

            LOG.info("执行全链路环境隔离={}", environment);

            return environment;
        }

        return super.getRouteEnvironment();
    }
}

在配置类里@Bean方式进行过滤类创建,覆盖框架内置的过滤类

@Bean
public GatewayStrategyRouteFilter gatewayStrategyRouteFilter() {
    return new MyGatewayStrategyRouteFilter();
}

本地测试,为验证结果,请事先在hosts文件中配置如下

127.0.0.1 common.nepxion.com
127.0.0.1 env1.nepxion.com
127.0.0.1 env2.nepxion.com
  • 根据env1.nepxion.com域名路由到env1环境

  • 根据common.nepxion.com域名路由到common环境




2017-2050 ©Nepxion Studio Apache License

           

Total visits