user2057006
user2057006

Reputation: 647

Refresh spring.active.profile at runtime using refreshscope - spring boot and cloud

Java Class:

package com.hm.refreshscoperesearch;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RefreshScope
@RestController
public class RefreshScopeResearchApplication {

    @Value("${integrations.ecom.api-url}")
    private String url;

     @RequestMapping("/hello")
        String hello() {
            return "Hello " + url + "!";
        }

    public static void main(String[] args) {
        SpringApplication.run(RefreshScopeResearchApplication.class, args);
    }
}

application.yml

spring:
  profiles:
    active: default,mocked-ecom

integrations:
  ecom:
    api-url: dev-api.com

management:
  security:
    enabled: false

application-mocked-ecom.yml

integrations:
  ecom:
    api-url: mock-api.com

When i hit, http://localhost:8080/hello, i m getting response "Hello mock-api.com!".

If i remove mocked-ecom from application.yml and save it, and then invoke post refresh api call http://localhost:8080/refresh to refresh context, i expect result "Hello dev-api.com!" but i'm getting "Hello mock-api.com!".

How to refresh profile at run time using refreshscope in spring boot?

spring:
  profiles:
    active: default

integrations:
  ecom:
    api-url: dev-api.com

management:
  security:
    enabled: false

Upvotes: 0

Views: 1321

Answers (1)

ootero
ootero

Reputation: 3475

I'm not sure you can change the Spring profile value at runtime. You can change the value of a property in one of the active profiles and POSTing to /refresh should pick the new value up. Try changing:

application-mocked-ecom.yml

integrations:
  ecom:
    api-url: new-mock-api.com

Upvotes: 0

Related Questions