简体   繁体   English

如何在Spring Cloud Consul配置中将并发模式更改为Stale

[英]How to change consitency mode to Stale in Spring Cloud consul config

Consul supports consistency mode param in http . Consul在http中支持一致性模式参数。 As per consul documentation it can have DEFAULT,CONSISTENT,STALE . 根据领事文档,它可以具有DEFAULT,CONSISTENT,STALE。 I want to change the consistency mode from default ot STALE in one of my application. 我想在我的一个应用程序中从默认OT STALE更改一致性模式。 I didn't find any way in the provided spring documentation. 在提供的spring文档中找不到任何方式。 Is this achievable using spring cloud consul config? 使用Spring Cloud Consul配置可以实现吗?

if your use case is only to start consul after only one stayed up. 如果您的用例只是在一个人熬夜之后才开始领事。 You can use this hack, and then call it from Spring boot main method. 您可以使用此hack,然后从Spring boot main方法调用它。

public static void changeConsistencyModeToStale() {
    for (Field field : QueryParams.class.getFields()) {
        if ("DEFAULT".equals(field.getName())) {
            try {
                field.setAccessible(true);
                Field modifiersField = Field.class.getDeclaredField("modifiers");
                modifiersField.setAccessible(true);
                modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
                field.set(null, new QueryParams(ConsistencyMode.STALE));
            } catch (NoSuchFieldException | IllegalAccessException e) {
                log.error("Error while try to set stale mode to consul", e);
            }

            log.info("Consistence mode has been set to stale successfully");
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM