简体   繁体   English

如何在不重新启动应用程序的情况下在 PCF 中运行的 spring 引导应用程序中加载更改的环境变量?

[英]How to load changed environment variable in spring boot application running in PCF without restarting application?

We have a spring boot application running in PCF and it reads the PCF environment variables(CF_INSTANCE_INDEX, CF_INSTANCE_ADDR,..) from an application.我们有一个在 PCF 中运行的 spring 引导应用程序,它从应用程序中读取 PCF 环境变量(CF_INSTANCE_INDEX、CF_INSTANCE_ADDR、..)。 Based on those variables, we are trying to implement the logic for a scheduler.基于这些变量,我们正在尝试实现调度程序的逻辑。 While running this scheduler, these variables' values could have changed.在运行此调度程序时,这些变量的值可能已更改。 Is there a way to refresh/reload bean that have env values during runtime?有没有办法在运行时刷新/重新加载具有 env 值的 bean?

we used @RefreshScope annotation on config properties bean.我们在配置属性 bean 上使用@RefreshScope注释。

@Configuration
@RefreshScope
public class PcfEnvProperties{

@Value("${CF_INSTANCE_INDEX}")
private int intanceIndex;

@Value("${CF_INSTANCE_ADDR}")
private String intanceAddr;

...
}

and refresh using并使用刷新

context.getBean(RefreshScope.class).refresh("PcfEnvProperties");
PcfEnvProperties pcfEnv = context.getBean(PcfEnvProperties.class);

But It is not loading the recently changed env variable into running application.但它不会将最近更改的 env 变量加载到正在运行的应用程序中。 Any ideas on how to accomplish this?关于如何做到这一点的任何想法?

You can use Spring Cloud Config Server in combination with Spring Actuator to expose an endpoint in your service that will refresh the application's properties on the fly.您可以将Spring 云配置服务器Spring 执行器结合使用,在您的服务中公开一个端点,该端点将即时刷新应用程序的属性。 You could set up your scheduler to hit this endpoint on a timer or as needed.您可以将调度程序设置为在计时器上或根据需要访问此端点。

Here is one tutorial I found that seems pretty straightforward: https://jeroenbellen.com/manage-and-reload-spring-application-properties-on-the-fly/这是一个我发现看起来很简单的教程: https://jeroenbellen.com/manage-and-reload-spring-application-properties-on-the-fly/

You may have to play with the setup depending on how your platform is configured, but I believe it should do what you're wanting.根据平台的配置方式,您可能必须使用设置,但我相信它应该可以满足您的需求。 We have deployed many java web services on our PCF platform using this actuator/config server approach, and we can just make a call to the refresh endpoint and it successfully pulls in (and overwrites when necessary) the new properties and values from the config server.我们已经使用这种执行器/配置服务器方法在我们的 PCF 平台上部署了许多 java web 服务,我们只需调用刷新端点,它就会成功地从配置服务器中拉入(并在必要时覆盖)新属性和值. Also you can pull out a list of the property names and values that changed from the response.您还可以拉出从响应中更改的属性名称和值的列表。

I'm not familiar with the specific property values you mentioned, but as long as they are normally a part of Spring's ApplicationContext (where properties usually are found) then you should be able to pull in changed values using this approach with Spring's cloud config server and actuator libraries.我不熟悉您提到的特定属性值,但是只要它们通常是 Spring 的 ApplicationContext 的一部分(通常可以在其中找到属性),那么您应该能够使用这种方法与 Spring 的云配置服务器一起提取更改的值和执行器库。

Hope this helps希望这可以帮助

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

相关问题 Spring Boot,无需重启应用即可更新前端 - Spring Boot, update frontend without restarting the application 重启 Spring 引导应用程序 - Restarting Spring Boot Application 如何确保在 Apache Tomcat 中运行的 Spring 引导应用程序中替换环境变量占位符? - How to make sure that environment variable placeholders are substituted in a Spring Boot application running in Apache Tomcat? 重启 Java Spring 启动应用程序 - Restarting Java Spring Boot Application spring 启动应用程序.yml 中的环境变量 - Environment variable in spring boot application.yml 如何在我的Spring Boot应用程序中从AWS访问环境变量 - How to access Environment Variable from AWS in my spring boot application AWS secret manager Password Rotation Without Restarting Spring 引导应用程序 - AWS secret manager Password Rotation Without Restarting Spring boot application 无法加载 Spring 在 Docker 中运行的引导应用程序 - Unable to load Spring Boot application running in Docker 如何在不重新启动 Spring Boot 应用程序的情况下在运行时更改日志级别 - how do I change log level in runtime without restarting spring boot application Pivotal Cloud Foundry(PCF)弹簧启动应用程序停止运行且没有错误 - Pivotal Cloud Foundry (PCF) spring boot application stops working without error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM