springspring-bootconsulspring-cloud-consul

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?


Solution

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