2

I'm trying to use Spring cloud bus with Kafka in my microservices application, and indeed I could use it, but only data which is controlled by Spring cloud config server got refreshed!

I'm using jdbc back-end with my config server, and in order to simulate my need, I'm changing some value in properties file in one of my services, beside the properties table, and call the /monintor end point again (mentioned here section 4.3 https://www.baeldung.com/spring-cloud-bus); as a result, only data coming from properties table is changed.

This is the yml file for my Config server

spring:
  cloud:
    config:
      server:
        jdbc:
          sql: SELECT KEY,VALUE from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?
          order: 1
    stream:
      kafka:
        binder:
          brokers: localhost:9092
  datasource:
    url: jdbc:mysql://localhost:3306/sweprofile?zeroDateTimeBehavior=convertToNull
    username: 123
    password: 123ertbnm
    hikari:
      maximum-pool-size: 10
      connection-timeout: 5000
  profiles:
    active:
      - jdbc
  application:
    name: configServer

These are yml files for One of my Miscroservices and its propeties file respectively

spring:
  datasource:
    username: 123
    password: 123ertbnm
    url: jdbc:mysql://localhost:3306/sweprofile?zeroDateTimeBehavior=convertToNull
    jpa:
      properties:
        hibernate:
          format_sql: true
          ddl-auto: none
  application:
    name: auth-service
  cloud:
    config:
      discovery:             
        enabled: true
        service-id: configServer
    bus:
      refresh:
        enabled: true
    profiles:
      active: jdbc

management:
  endpoints:
    web:
      exposure:
        include: ["health","info","refresh", "bus-refresh"]
# This line is dummy data for testing purpose 
ali.man = " Ola 12333"

This is snapshot from rest controller

@RestController
@RequestMapping("/user")
@RefreshScope
public class AuthController {
    private UserAuthService userAuthService;

    @Value("${name}")
    private String name;   // changed normally

    // Calling the key value mentioned in properties file after changing
    @Value("${ali.man}")
    private String k;      // -> not changed

    public AuthController(UserAuthService userAuthService) {
        this.userAuthService = userAuthService;
    }

    @GetMapping("authTest")
    public String getAuth() {
        return name + k;
    }
}

What did I miss? Why value from Properties file is not changed? hopefully I can use Spring cloud bus with Kafka to refresh these external data.

Mohamed Sweelam
  • 1,109
  • 8
  • 22
  • I don't understand. Where do name and ali.man come from respectively? – spencergibb Apr 28 '19 at 10:24
  • This _ali.man_ is just hard coded configuration in properties file to test the change, _name_ is configured in properties table, and I found when changed it and Properties table, only data in this table got changed after refresh. – Mohamed Sweelam Apr 28 '19 at 11:30

1 Answers1

2

After some hours of investigation, I found that there is some recommended way. Cloud bus can send Refresh Event and Spring boot has RefreshEvent Listener to that event; this what I build my solution on.

So when event is sent by the bus; all instances will do the same logic ( Refreshing data ) on the loaded in memory configurations.

I used this snippet to apply this

@Configuration
public class ReloadLookupEvent implements ApplicationListener<RefreshScopeRefreshedEvent> {
    @Autowired
    private CacheService cacheService;

    @Override
    public void onApplicationEvent(RefreshScopeRefreshedEvent event) {
        cacheService.refreshLookUp();     
    }

}

I could refresh all other configurations on demand, maybe it is a workaround, but acceptable.

Mohamed Sweelam
  • 1,109
  • 8
  • 22
  • Hi, I am working on the same, having a doubt, can I use this POST API (localhost:8080/actuator/busenv) to update one of the property value. IF YES, then will it update the value in application context only or it will update in JDBC table as well??? I want to change the value in JBBC table as well with API, is it possible??? – APK May 08 '22 at 17:07
  • Hello @APK, I don't think it will have any impact on the database, I would suggest reviewing your requirements as this is not the purpose of such technique. – Mohamed Sweelam May 12 '22 at 05:30