Skip to content

Commit 51b82e0

Browse files
committed
add images
1 parent aba0131 commit 51b82e0

6 files changed

+112
-5
lines changed

README.md

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,63 @@
11
# Spring Boot Overriding Environment Variables Guide
2-
This is an example of overriding the application.yml file with the OS environment variables. `Single` or `Array` environment variables can be overridden.
2+
3+
4+
This is an example of overriding the application.yml file with the OS environment variables.
5+
`Single` or `Array` environment variables can be overridden.
6+
7+
![img.png](images/title.png)
8+
9+
We talk about many ways to override environment variables in a Spring Boot application. Not auto-configuration :(
10+
11+
---
12+
13+
## Understanding Environment Variables
14+
* Your Spring Boot application has a `application.yml` or `application.properties` file.
15+
* The application properties are converted to environment variables when you run the Spring Boot application.
16+
* The environment variables are key-value pairs that are set in the operating system.
17+
* They are used to configure applications and services.
18+
* Many ways to override environment variables in a Spring Boot application.
19+
20+
---
321

422
## Overview
523
1. This guide will show you how to override environment variables in a Spring Boot application.
6-
2. Single and multiple environment variables will be overridden.
24+
2. Single and array environment variables will be overridden.
725
3. When you run a Spring Boot application, the application properties are converted to environment variables.
826
4. You can override environment variables using the `-D` flag when java -jar is used.
927
5. You can override environment variables using the `-e` flag when running a Spring Boot application in a Docker container.
1028
6. You can override environment variables using ConfigMaps and Secrets when running a Spring Boot application in a Kubernetes cluster.
1129
7. You can override environment variables using the `application.yml` file from git repository with spring cloud config server.
30+
31+
----
32+
33+
## Prerequisites
34+
* JDK 17
35+
* Maven 3.9x
36+
* Spring Boot 3x
37+
1238
---
1339

14-
## Our Use Case
40+
## Getting Started
41+
* Clone the repository
42+
```shell
43+
git clone https://github.com/cevheri/spring-boot-overriding-environment-variables.git
44+
```
45+
46+
* Change directory
47+
```shell
48+
cd spring-boot-overriding-environment-variables
49+
```
50+
51+
* Run the Spring Boot application
52+
```shell
53+
mvn spring-boot:run
54+
```
55+
56+
---
57+
58+
59+
## Our Use Cases
60+
![img.png](images/uses-cases.png)
1561
4 and 5 are the most common use cases for overriding environment variables in a Spring Boot application. Maybe next time we can talk about the Kubernetes and Spring Cloud Config Server use cases.
1662

1763
---
@@ -75,25 +121,46 @@ Run the Spring Boot application using the `java -jar` command.
75121
```shell
76122
java -jar target/soe.jar
77123
```
78-
124+
![default-values.png](images/default-values.png)
79125
---
80126

81127
#### Override Environment Variables
82128
```shell
83129
java -jar '-Dapp-properties.name=overridden-name-cevheri' '-Dapp-roles[0]=overridden-role-0-cevheri' '-Dapp-paths[0].roles[4]=overridden-path-0-role-0-cevheri' target/soe.jar
84130
```
131+
![overrided-values.png](images/overrided-values.png)
132+
---
133+
85134
### Docker
86135
#### Simple
87136
```shell
88137
docker run -p 8080:8080 soe:latest
89138
```
90-
139+
![docker-default-values.png](images/docker-default-values.png)
91140
#### Override Environment Variables
92141
```shell
93142
docker run -p 8080:8080 -e APP_PROPERTIES_NAME=overridden-name-cevheri-docker -e APP_ROLES_0=overridden-role-0-cevheri-docker -e APP_PATHS_0_ROLES_4=overridden-path-0-role-0-cevheri-docker soe:latest
94143
```
144+
![docker-overrided-values.png](images/docker-overrided-values.png)
95145

96146

147+
## Test
148+
149+
```shell
150+
mvn test
151+
```
152+
153+
---
154+
155+
## Conclusion
156+
* This guide showed you how to override environment variables in a Spring Boot application.
157+
* Single and multiple(array) environment variables were overridden.
158+
* When you run a Spring Boot application, the application properties are converted to environment variables.
159+
* You can override environment variables using the `-D` flag when java -jar is used.
160+
* You can override environment variables using the `-e` flag when running a Spring Boot application in a Docker container.
161+
162+
---
163+
97164
### Reference Documentation
98165

99166
For further reference, please consider the following sections:

images/docker-default-values.png

189 KB
Loading

images/docker-overrided-values.png

195 KB
Loading

images/title.png

209 KB
Loading

images/uses-cases.png

97.3 KB
Loading

medium_deploy.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import requests
2+
3+
# Create a new token in https://medium.com/me/settings/ > security and app > integration tokens
4+
# get userId in https://api.medium.com/v1/me
5+
# curl -H "Authorization: Bearer TOKEN" -H "Content-Type: application/json" -H "Accept: application/json" https://api.medium.com/v1/me | jq '.data.id'
6+
# copy the id
7+
8+
9+
MEDIUM_USER_ID = "USER_ID"
10+
MEDIUM_API_URL = f"https://api.medium.com/v1/users/{MEDIUM_USER_ID}/posts"
11+
MEDIUM_TOKEN = "TOKEN"
12+
TITLE = "NLP Sentiment Analysis on Restaurant Reviews"
13+
FILE_NAME = "nlp-sentiment-analysis-restaurant.md"
14+
15+
16+
def main():
17+
headers = {
18+
"Authorization": f"Bearer {MEDIUM_TOKEN}",
19+
"Content-Type": "application/json",
20+
"Accept": "application/json",
21+
"Accept-Charset": "utf-8"
22+
}
23+
24+
data = {
25+
"title": TITLE,
26+
"contentFormat": "markdown",
27+
"content": open(FILE_NAME, "r").read()
28+
}
29+
30+
response = requests.post(MEDIUM_API_URL, headers=headers, json=data)
31+
32+
if response.status_code == 201:
33+
print("Article published successfully!")
34+
else:
35+
print(f"Error: {response.status_code}")
36+
print(f"Error content: {response.content}")
37+
38+
39+
if __name__ == "__main__":
40+
main()

0 commit comments

Comments
 (0)