具有外部属性文件的Cloud上的Spring Boot应用程序 [英] Spring boot app on Cloud with external properties file

查看:115
本文介绍了具有外部属性文件的Cloud上的Spring Boot应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring Boot应用程序,需要使用属性文件.该应用程序将在云上.因此,属性文件也将出现在该代码中.

I have a spring boot application which needs to use a property file. This application will be on the cloud. So, the property file will also be on the coud.

我正在使用Cloud Foundry与云进行通信,并使用'cf push'来推送我的spring-boot应用程序.

I am using cloud foundry to communicate with the cloud and use 'cf push' to push my spring-boot application.

我的问题: 1.在何处以及如何存储属性文件?可以将其存储在云中吗?如何?还是需要将其存储在git上并在您的应用程序中引用它? 2.如何使用位于外部位置的此属性文件?

My question : 1. Where and how to store a properties file? Can it be stored on the cloud? How? Or does it need to be stored on git and reference it in your application? 2. How to use this properties file which is at the external location ?

推荐答案

您不能在Cloud Foundry中访问文件系统.您必须将配置文件保留在类路径本身中,并从类路径加载属性,这也是Spring引导的默认行为. Spring Boot application.properties文件(位于src/main/resource内)进入jar并由Spring加载.因此,当您构建嵌入式jar/war时,它将进入classpath并直接运行.

You cant have file system access in cloud foundry. You have to keep the config file in classpath itself and load the properties from classpath which is also the default behaviour with Spring boot. Spring boot application.properties file (which is inside src/main/resource) goes inside your jar and springs loads it. Hence when you build a embedded jar/war it goes inside classpath and works directly.

处理属性的云代工厂方法是通过VCAP变量.您可以使用在cf推送期间使用的deplyoment清单来外部化这些属性. 在清单yml文件中,定义以下属性.

The cloud foundry way of handling properties is through VCAP Variables. You can externalize these properties using the deplyoment manifest which is used during the cf push. Inside your manifest yml file define properties as below.

env:
    myprop1: myvalue1
    myprop2: myvalue2

使用这种方法,您应用的所有属性都可以作为常规系统属性使用.您可以在Java代码中以System.getProperties()的形式在应用程序中访问它们.

Using this approach all the properties for your app would be available as normal System Properties. You can access them in your application as System.getProperties() inside your Java code.

https://docs.run.pivotal.io/devguide/deploy-apps/environment-variable.html#VCAP-APPLICATION 

https://docs.cloudfoundry.org/devguide/deploy-apps/manifest.html

这两种方法的主要缺点是需要将应用程序重建/重新推送到Cloud Foundry.一旦应用程序启动并运行,该应用程序将无法动态读取属性中的更改.

The major drawback with these two approaches is that the application needs to be rebuilt/repushed to the cloud foundry. The application cant read change in the properties dynamically once application is up and running.

要克服此问题,Spring-cloud提供了一个项目cloud-config.这可以由GIT或其他几种类型的数据源(如文件系统/保险库等)支持.它作为不同的配置服务运行. 您可以在应用程序属性文件中定义配置服务器,以使应用程序从Config服务读取属性.

To overcome this Spring-cloud has a project cloud-config. This can be backed by GIT or few other types of Datasource like File System/ Vault etc. This run as a different configuration service. You can define the config server inside your application properties file which makes application read properties from the Config service.

https://cloud.spring.io/spring-cloud-config/spring-cloud-config.html

通常,当您需要集中微服务体系结构中的属性时,可以采用配置服务方法.

Mostly when u need to centralize the properties in a microservice architecture you can go for config service approach.

这篇关于具有外部属性文件的Cloud上的Spring Boot应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆