Spring Cloud Canary部署

时间:2019-02-14 09:37:57

标签: spring-boot docker netflix-zuul canary-deployment

我有一个Spring Cloud微服务,其中Zuul在docker上运行。

要求:

我要创建具有特定需求的Canary部署,因为我们将有x个客户端,并且我想与y个特定客户端(使用电子邮件或用户名)进行canary测试。

我可以为这些y客户端配置网关以将请求路由到新版本的微服务吗?

1 个答案:

答案 0 :(得分:0)

因此您可以通过配置或动态路由来做到这一点,但我认为第一个idom不适用于您必须一次又一次定义它的每个客户端的通用部分,但是第二个则更好

@Component
public class PostFilter extends ZuulFilter {

    private static final String REQUEST_PATH = "/special-customer-product-request-url";
    private static final String TARGET_SERVICE = "special-customer-service";
    private static final String HTTP_METHOD = "POST or GET";

    private final DiscoveryClient discoveryClient;

    public PostOrdersFilter(DiscoveryClient discoveryClient) {
        this.discoveryClient = discoveryClient;
    }

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

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

    @Override
    public boolean shouldFilter() {
        RequestContext context = RequestContext.getCurrentContext();
        HttpServletRequest request = context.getRequest();
        String method = request.getMethod();
        String requestURI = request.getRequestURI();
        return HTTP_METHOD.equalsIgnoreCase(method) && requestURI.startsWith(REQUEST_PATH);
    }

    @Override
    public Object run() {

        RequestContext context = RequestContext.getCurrentContext();
        List<ServiceInstance> instances = discoveryClient.getInstances(TARGET_SERVICE);
        try {
            if (instances != null && instances.size() > 0) {
                context.setRouteHost(instances.get(0).getUri().toURL());
            } else {
                throw new IllegalStateException("Target service instance not found!");
            }
        } catch (Exception e) {
            throw new IllegalArgumentException("Couldn't get service URL!", e);
        }
        return null;
    }
}