- 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.
- 構(gòu)建運(yùn)營級IPv6網(wǎng)絡(luò)
- Premiere Pro與After Effects從入門到實(shí)戰(zhàn)(微課版)
- BBC監(jiān)聽音箱完全指南
- 室內(nèi)分布系統(tǒng)規(guī)劃與設(shè)計(jì):GSM/TD-SCDMA/TD-LTE/WLAN
- 移動Ad Hoc網(wǎng)絡(luò)
- 集成電路靜態(tài)時序分析與建模
- 嵌入式Linux網(wǎng)絡(luò)體系結(jié)構(gòu)設(shè)計(jì)與TCP/IP協(xié)議棧
- 艦船尾跡的電磁成像機(jī)理及特征提取技術(shù)
- 5G XR技術(shù)與應(yīng)用
- 3D顯示技術(shù)
- BREW進(jìn)階與精通
- Final Cut Pro X實(shí)戰(zhàn)從入門到精通
- GSM網(wǎng)絡(luò)優(yōu)化理論與實(shí)踐
- 光纖通信技術(shù)
- 信號與系統(tǒng)