Skip to content

Commit 7f3109f

Browse files
Added dockerization.
1 parent 8d28ec9 commit 7f3109f

File tree

7 files changed

+135
-13
lines changed

7 files changed

+135
-13
lines changed

README.md

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ You get bonus points if:
6262
-**RestTemplate** ❌ Older way to make calls in Spring and synchronous. [RestTemplate](https://www.baeldung.com/rest-template).
6363
-**Feign Client** ❌ Also synchronous. [Feign Client](https://www.baeldung.com/spring-boot-feignclient-vs-webclient).
6464
- [![Netty](https://img.shields.io/badge/Netty-grey?style=for-the-badge&logo=googleearth&logoColor=FFFFFF)](https://projectreactor.io/docs/netty/1.1.21/reference/index.html).
65-
-**Netty**✅ is suited for Microservices Architecture, for its non-blocking I/O client-server nature.
65+
-**Netty**✅ is suited for Microservices Architecture, for its non-blocking I/O client-server nature. This for bank end.
6666
- [![Lombok](https://img.shields.io/badge/Lombok-green?style=for-the-badge)](https://docs.spring.io/spring-framework/reference/web/webflux.html).
6767
-**Lombok**✅ For reducing boilerplate code.
6868

@@ -78,6 +78,7 @@ You get bonus points if:
7878
- I have decided to use **ResponseEntity** in **Controller** class. [ReadMore](https://www.baeldung.com/spring-response-entity).
7979

8080
> While **ResponseEntity** is very powerful, we shouldn’t overuse it. In simple cases, there are other options that satisfy our needs and they result in much cleaner code.
81+
8182
- We are making tests!
8283
- **Unit Tests**:
8384
- For Bean Validation.
@@ -86,7 +87,15 @@ You get bonus points if:
8687
- [![WebTestClient](https://img.shields.io/badge/WebTestClient-grey?style=for-the-badge&logo=spring)](https://docs.spring.io/spring-framework/reference/testing/webtestclient.html).
8788
- Inside Spring application.
8889
- [![WireMock](https://img.shields.io/badge/WireMock%20-grey?style=for-the-badge)](https://wiremock.org/).
89-
- For testing external APIs. In progress ♻️ **Could not get this working, for now** ♻️.
90+
-**WireMock**✅ for testing external APIs. In progress ♻️ **Could not get this working, for now** ♻️.
91+
- [![Docker](https://img.shields.io/badge/docker-%230db7ed.svg?style=for-the-badge&logo=docker&logoColor=white)](https://www.docker.com/)
92+
-**Docker**✅ for easy deployment.
93+
94+
- [![Angular](https://img.shields.io/badge/Angular-DD0031?style=for-the-badge&logo=angular&logoColor=white)](#)
95+
-**Angular**✅ front end, because i like it!
96+
97+
- [![nginx](https://img.shields.io/badge/nginx-grey?style=for-the-badge&logo=nginx)](https://nginx.org/en/)
98+
-**Nginx**✅ for hosting front end.
9099

91100
- API versioning:
92101
-**URI Path Versioning**✅, were chosen for its popularity.
@@ -98,6 +107,10 @@ You get bonus points if:
98107

99108
- Country letter representation "**two letters**" [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) will be used, since it was in specifications.
100109

110+
### Prerequisites:
111+
- Lates java JDK is fine to run commands.
112+
- Latest Docker.
113+
101114
# Not sure things ⚠️👀?
102115

103116
- I am not sure that, am i utilizing the **.subscribe** rightly. Like in this example, i don't have any of such in code directly `.subscribe(this::someFunction, this::someFunctionIfErrorHappened);`. My service is simple passing data to front end by means of **Project Reactor**. Well, yes if there would be db attached, then this would be suitable approach, but i didn't come any use for such case.
@@ -112,19 +125,18 @@ Get the repository.
112125
git clone https://github.com/developersCradle/springboot-microservices.git
113126
```
114127

115-
# Docker way (Preferred!). ⚠️ Start separately fe and be for now! ⚠️
128+
# Docker way (Preferred!).
116129

117-
- Compile your Maven project, run tests, and package it into a JAR.
130+
For now just start the containers by running the following command:
118131

119132
```bash
120-
./mvnw package
133+
docker-compose up --build
121134
```
122135

123-
♻️ **Under progress** ♻️ This could possible in one command leveraging a multi-step process. For now just start the containers by running the following command:
136+
- Front end will be in `http://localhost:8081/`.
137+
- Back end will be example `http://localhost:8080/countries/v1/`.
124138

125-
```bash
126-
docker compose up
127-
```
139+
⚠️ fix WireMock, so you can remove skip test flag! ⚠️
128140

129141
# Front end.
130142

country-service-backend/Dockerfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#Includes maven!
2+
FROM maven:3.8.7-eclipse-temurin-17 AS build
3+
4+
# Set the working directory
5+
WORKDIR /app
6+
7+
# Copy the pom.xml and source code to the container
8+
COPY pom.xml .
9+
COPY src ./src
10+
11+
# Build the application, skipping tests. TODO(HEIKKI) WireMock wokring, so can test integration test correctly.
12+
RUN mvn clean package -DskipTests
13+
14+
# Use a lightweight JDK image to run the app
15+
FROM openjdk:17-jdk-slim
16+
17+
# Set the working directory
18+
WORKDIR /app
19+
20+
# Copy the built JAR file from the Maven build stage
21+
COPY --from=build /app/target/*.jar app.jar
22+
23+
# Expose the application port (default 8080 for Spring Boot)
24+
EXPOSE 8080
25+
26+
# Run the application
27+
ENTRYPOINT ["java", "-jar", "app.jar"]

country-service-backend/src/main/resources/application.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
# We are making this configuration here, because if there would be more environments for this microservice to be ran on it would be easier for uss. Also for better manage the API versioning.
22
spring:
33
profiles:
4-
active: local
4+
active: local # By default local is activated.
55
application:
66
name: country-service-backend
77
restClient:
88
countriesNowUrl: https://countriesnow.space/api/v0.1/countries/
99
---
10-
# different env non-pod
10+
spring:
11+
config:
12+
activate:
13+
on-profile:
14+
- non-prod # If profile is non-prod, use what inside these ones.
15+
---
16+
# Different env prod
1117
---
12-
# different env prod

country-service-front/Dockerfile

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Build the Angular application
2+
FROM node:16 AS build
3+
4+
# Set the working directory
5+
WORKDIR /app
6+
7+
# Copy package.json and package-lock.json
8+
COPY package.json package-lock.json ./
9+
10+
# Install dependencies
11+
RUN npm install --production
12+
13+
# Copy the rest of the application code
14+
COPY . .
15+
16+
# Build the Angular application
17+
RUN npm run build --prod
18+
19+
# Serve the application using Nginx
20+
FROM nginx:alpine
21+
22+
# Remove default Nginx index page
23+
RUN rm /usr/share/nginx/html/*
24+
25+
# Copy the built Angular app from the previous stage
26+
COPY --from=build /app/dist/country-service-front /usr/share/nginx/html
27+
28+
# Expose the port that Nginx will run on
29+
EXPOSE 80
30+
31+
# Start Nginx
32+
CMD ["nginx", "-g", "daemon off;"]

country-service-front/nginx.conf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
server {
2+
listen 80;
3+
server_name localhost;
4+
5+
location / {
6+
root /usr/share/nginx/html;
7+
try_files $uri $uri/ /index.html;
8+
}
9+
10+
location / { # Assuming your API endpoints start with /api
11+
proxy_pass http://backend-service:8080; # Spring WebFlux service URL
12+
proxy_http_version 1.1;
13+
proxy_set_header Upgrade $http_upgrade;
14+
proxy_set_header Connection 'upgrade';
15+
proxy_set_header Host $host;
16+
proxy_cache_bypass $http_upgrade;
17+
}
18+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export const environment = {
2-
production: true
2+
production: true,
3+
apiUrl: 'http://localhost:8080/countries/v1/'
34
};

docker-compose.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
version: '3.8'
2+
3+
services:
4+
spring-app:
5+
build:
6+
context: ./country-service-backend # Path to the folder where your backend Dockerfile is located.
7+
dockerfile: Dockerfile
8+
ports:
9+
- "8080:8080" # Standard way to map, same port inside and outside.
10+
environment:
11+
SPRING_PROFILES_ACTIVE: local # Set active Spring profile, this is default usually.
12+
restart: always # Should always restart if it stops, regardless of the exit status.
13+
14+
frontend:
15+
build:
16+
context: ./country-service-front # Path to the folder where your Angular Dockerfile is located.
17+
dockerfile: Dockerfile
18+
ports:
19+
- "8081:80" # Exposing the Angular app on port 8081 and nginx exposing inside port 80.
20+
depends_on:
21+
- spring-app # Front end waits for backend
22+
restart: always # Should always restart if it stops, regardless of the exit status.
23+
24+
25+
26+
27+

0 commit comments

Comments
 (0)