4

I am working with Consul using spring-cloud-consul and currently have this problem. I create a key in consul as config/ConsulServer/my/username with value as "bob". In my controller, i return this value if a API call "/foo" is made. When i modified it the first time to "steve" the value is updated and i get "steve". However, it does not work the second time. The value is still "steve". Could anyone please help on what I did wrong ? My codes are

@SpringBootApplication
@EnableDiscoveryClient
public class ConsulServer {
  public static void main(String[] args) {
    SpringApplication.run(ConsulServer.class, args);
  }
}

@Component
@RefreshScope
@ConfigurationProperties("my")
public class SampleProperties {
   private String username;
   public String getUsername() {
    return username;
   }
   public void setUsername(String username) {
    this.username= username;
   }
}

@RestController
public class ConsulController {
    @Autowired
    private SampleProperties consulConfig;

    @GetMapping("/foo")
    public String prop() {
       return this.consulConfig.getUsername();
    }
}

My bootstrap.yml is

spring:
  application:
     name: ConsulServer
  cloud:
     consul:
     host: localhost
     port: 8500
     config:
        enabled: true
        fail-fast: true
        watch:
          enabled: true
     discovery:
        register: true

My application.yml is

spring:
  application:
     name: ConsulServer

I am using spring-cloud-starter-consul-all version 2.1.0.RC3

Steven
  • 415
  • 1
  • 4
  • 12
  • strange, tried your configuration with Consul (version 1.2.1) and spring-cloud-starter-consul-all (version 2.1.0.RC3) - works as expected, value is changed every time. Did you check logs? I have "o.s.c.e.event.RefreshEventListener : Refresh keys changed: [my.username]" every time after when I change value in Consul UI – nmyk Jan 04 '19 at 08:41
  • Hi @nmyk, thanks for your help. I also see that event in the logs when I changed the consul but the value is not updated when I make the API call to /foo – Steven Jan 05 '19 at 16:09
  • I notice that it work with @Value though. That is if I map my.username directly to a String field, then that field is updated with new value. – Steven Jan 05 '19 at 16:14
  • 1
    The same for me: `@RefreshScope` doesn't refresh while `@Value` does. – Pavel Vlasov Feb 05 '20 at 21:06

0 Answers0