简体   繁体   中英

How to change consitency mode to Stale in Spring Cloud consul config

Consul supports consistency mode param in http . As per consul documentation it can have DEFAULT,CONSISTENT,STALE . I want to change the consistency mode from default ot STALE in one of my application. I didn't find any way in the provided spring documentation. Is this achievable using spring cloud consul config?

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.

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");
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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