利用eureka的元数据新闻,再配上ribbon的路由成效,就足以在api-gateway落到实处无数效用,比如灰度测试、生产调节和测试等等。下边介绍一下,怎么选用jmnarloch大神提供的ribbon-discovery-filter-spring-cloud-starter,利用简易的几行代码解决那总体。

maven

        <dependency>
            <groupId>io.jmnarloch</groupId>
            <artifactId>ribbon-discovery-filter-spring-cloud-starter</artifactId>
            <version>2.1.0</version>
        </dependency>

eureka元数据配置

eureka:
  instance:
    metadataMap:
      tag: pre-prod

路由

@Component
public class DynamicRoutesFilter extends ZuulFilter {

    private static final Logger LOGGER = LoggerFactory.getLogger(DynamicRoutesFilter.class);

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {
        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletRequest request = requestContext.getRequest();
        String tag = request.getHeader("tag");
        if(tag != null){
            RibbonFilterContextHolder.getCurrentContext().add("tag", tag);
        }

        return null;
    }
}

依照请求的tag,路由到标记有内定tag值的劳务。注意,倘诺请求不内定tag,则代码供给判空一下,其它不钦定tag的话,则暗许全体同名的服务都会被路由到。

doc


想博得最新音信,请关切微信公众号

图片 1

qrcode_for_gh_121b87c80448_258.jpg

网站地图xml地图