简体   繁体   English

使用refreshscope在运行时刷新spring.active.profile-Spring Boot和Cloud

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

Java Class: Java类:

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 application.yml

spring:
  profiles:
    active: default,mocked-ecom

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

management:
  security:
    enabled: false

application-mocked-ecom.yml 应用嘲笑,ecom.yml

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

When i hit, http://localhost:8080/hello , im getting response "Hello mock-api.com!". 当我点击http:// localhost:8080 / hello时 ,我收到响应“ 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!" 如果我从application.yml中删除了mocked-ecom并保存,然后调用刷新后的api调用http:// localhost:8080 / refresh来刷新上下文,则我期望结果为“ Hello dev-api.com!” but i'm getting "Hello mock-api.com!". 但我收到“ Hello mock-api.com!”。

How to refresh profile at run time using refreshscope in spring boot? 如何在春季启动时使用refreshscope在运行时刷新配置文件?

spring:
  profiles:
    active: default

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

management:
  security:
    enabled: false

I'm not sure you can change the Spring profile value at runtime. 我不确定您可以在运行时更改Spring配置文件的值。 You can change the value of a property in one of the active profiles and POSTing to /refresh should pick the new value up. 您可以在活动配置文件之一中更改属性的值,并且POSTing到/ refresh应该会选择新值。 Try changing: 尝试更改:

application-mocked-ecom.yml 应用嘲笑,ecom.yml

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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