- Cloud-Native Applications in Java
- Ajay Mahajan Munish Kumar Gupta Shyam Sundar
- 378字
- 2021-06-24 19:07:17
Externalizing configuration
Configuration can be any property that is likely to differ between environments or production deployments. Typical examples are queue and topic names, ports, URLs, connection and pool properties, and so on.
A deployable should not have configuration in it. A configuration should be injected from outside. This makes the deployable unit immutable as it goes through the various stages of the life cycle, such as Dev, QA, and UAT.
Let's assume we have to run our product service in different environments, where the URL differentiates the environment. Therefore, the small change we make in the request mapping is as follows:
@RequestMapping("/${env}product/{id}")
Product getProduct(@PathVariable("id") int id) {
We can inject this variable in various ways. Once injected, the value is not expected to change for the life of the deployment. The simplest is to pass it in the command-line argument. Bring up the Run Configurations dialog and in the Arguments, add the command-line parameter -env=dev/ as follows:

Now, Run the configuration. During startup, you will find the value substituted in the log statement as follows:
... Mapped "{[/dev/product/{id}]}" onto com.mycompany.product.Product com.mycompany.product.ProductService.getProduct(int)
The configuration can also be provided through configuration files, database, operating system environment properties, and so on.
Spring applications popularly use application.properties to store a few properties such as port numbers. Recently, YAML, which is a superset of JSON, is becoming more popular due to the hierarchical definition of the properties.
Create an application.yml file in the /product/src/main/resources folder of the application and put in the following:
server: port: 8081
This tells the product service to run at port 8081 instead of the default 8080. This concept is further extended to profiles. So, it is possible to load different profiles by loading the configuration specific to the profile.
Spring Cloud Config as a project handles this well. It uses a bootstrap.yml file to get the application started up with a name and details of the source to further load the configuration. Hence, bootstrap.yml contains the application name and config server details, and then loads the respective profile configuration.
Create a bootstrap.yml file in the resources folder of the application and put in the following:
spring: application: name: product
We will come back to these files when we discuss service registration later.
- 光傳輸設(shè)備安裝測試實訓(xùn)教程
- Android板級支持與硬件相關(guān)子系統(tǒng)
- 通信原理教程
- 輕松跟我學(xué)Protel 99SE電路設(shè)計與制版
- 從算法到電路:數(shù)字芯片算法的電路實現(xiàn)
- 5G社會:從“見字如面”到“萬物互聯(lián)”
- 移動通信技術(shù)及應(yīng)用
- 3GPP系統(tǒng)架構(gòu)演進(jìn)(SAE)原理與設(shè)計(第2版)(“十二五”國家重點圖書出版規(guī)劃項目)
- 從1G到5G:移動通信如何改變世界
- Microsoft Dynamics CRM 2011 Cookbook
- UI 那些事兒:新手設(shè)計師的成長之路
- Android Studio移動應(yīng)用開發(fā)從入門到實戰(zhàn)(微課版)
- 面向?qū)拵o線接入的光載無線系統(tǒng)
- WLAN關(guān)鍵技術(shù)及運營模式
- 電視機原理與維修