当前位置: 首页 > 知识库问答 >
问题:

使用刷新范围在运行时刷新spring.active.profile-Spring引导和云

乌灿
2023-03-14

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

应用yml公司

spring:
  profiles:
    active: default,mocked-ecom

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

management:
  security:
    enabled: false

应用程序模拟ecom。yml公司

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

当我击中时,http://localhost:8080/hello,我得到的回应是“Hello mock api.com!”。

如果我从应用程序中删除模拟的ecom。然后调用刷新后api调用http://localhost:8080/refresh要刷新上下文,我希望得到“Hello dev api.com!”但我收到了“你好,mock api.com!”。

如何刷新配置文件在运行时使用刷新范围在Spring启动?

spring:
  profiles:
    active: default

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

management:
  security:
    enabled: false

共有1个答案

拓拔俊德
2023-03-14

我不确定您是否可以在运行时更改Spring profile值。您可以更改其中一个活动配置文件中属性的值,发布到/刷新时应选择新值。尝试更改:

应用程序模拟ecom。yml公司

integrations:
  ecom:
    api-url: new-mock-api.com
 类似资料:
  • 问题内容: 我是新手。我想在几分钟后使用$ timeout of angular刷新范围。我正在开发一个社交应用程序,需要在几分钟后刷新通知范围。使用服务从http请求获取通知。 JS: } 现在,如何在几分钟后发出http请求,让我们说1分钟并刷新通知范围。我尝试使用$ timeout,但工作不正常。 问题答案: 但我建议将其移至控制器。 这似乎是一个更好的体系结构。 通过,解决或拒绝诺言将成为

  • 我知道通过配置服务器和刷新endpoint,可以在Spring启动应用程序中动态更改日志记录级别。为了控制日志轮换策略和文件的json编码,我决定使用logback。但是这会阻止我动态更改日志记录级别。 这意味着只有信息会被写入控制台/文件。但是如果我想在运行时将其更改为调试/跟踪怎么办? 编辑我仍然不理解根级别标记。但是,logback似乎从应用程序获取了日志级别。属性,这基本上回答了我的问题。

  • 我在pom中添加了以下依赖项,以便使用Spring云 spring-cloud.version芬奇利 这是我打算刷新的bean: 访问控制器中的bean: 在应用程序配置中设置的属性: 无法刷新模式的值。有人能指出这段代码中的错误吗?我的属性文件位于未在git中提交的文件夹中,因此我没有使用spring-cloud d-config。

  • 问题内容: 如果将新文档索引到Elasticsearch索引,则可在索引操作后1秒钟左右搜索新文档。但是,可以通过调用或对索引进行操作来强制使该文档可立即搜索。这两个操作之间有什么区别- 结果似乎对他们来说是相同的,可以立即搜索文档。 这些操作中的每一项到底是什么? ES文档似乎并未深入解决此问题。 问题答案: 您得到的答案是正确的,但我认为值得详细说明。 刷新有效地调用了Lucene索引读取器上

  • 我使用WSO2和SSOCircle以及Spring-SAML扩展。我们目前正在测试配置,并在我们的应用程序上下文中定义了2个IdP和2个SP。因此,目前,我们在spring xml配置中有2个静态定义的IdP,并且正在工作。出于测试目的,我们结合使用CachingMetadataManager和ResourceBackedMetadataProvider,因此IdP元数据构建在我们的WAR档案中。

  • 我已经浏览了许多链接,如@RefreshScope和/或refresh not working和Spring Boot 2:动态刷新属性not working,但仍然有些东西对我不起作用。 我开发了spring云配置服务器 当我击中 发布== 我得到了回应 [“config.client.version”] 看起来它不知何故没有加载更改的属性 pom。xml: PropertyAccessBean