Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
e10cb75
Merge pull request #10 from Nasruddin/revamp
Nasruddin Apr 24, 2022
4ff7d53
Update README.md
Nasruddin Apr 24, 2022
423c637
Update create-project.bash
Nasruddin Nov 7, 2022
d5d0c8f
Merge pull request #14 from Nasruddin/revamp
Nasruddin Feb 17, 2024
13a765b
Merge pull request #15 from Nasruddin/revamp
Nasruddin Feb 18, 2024
61c7e91
Merge pull request #16 from Nasruddin/revamp
Nasruddin Feb 19, 2024
543b56f
Create docker-image.yml
Nasruddin Feb 23, 2024
93ed233
Create maven.yml
Nasruddin Feb 23, 2024
11bb5e7
cleaning oauth flow
Nasruddin Mar 6, 2024
4bd88ef
Merge pull request #17 from Nasruddin/feature/authorization-server-impl
Nasruddin Mar 6, 2024
44ed279
Adding readme imgs
Nasruddin Mar 6, 2024
2e08a9b
Adding readme imgs
Nasruddin Mar 6, 2024
ae2f1ea
Merge pull request #19 from Nasruddin/feature/authorization-server-impl
Nasruddin Mar 6, 2024
12c3422
Adding readme
Nasruddin Mar 6, 2024
f884f03
Adding readme
Nasruddin Mar 6, 2024
b8d6988
Merge pull request #20 from Nasruddin/feature/authorization-server-impl
Nasruddin Mar 6, 2024
58a13c0
Commiting missing files
Nasruddin Mar 8, 2024
83f0b95
Merge pull request #22 from Nasruddin/feature/authorization-server-impl
Nasruddin Mar 8, 2024
7cf4a01
Bump org.springframework.security:spring-security-oauth2-authorizatio…
dependabot[bot] Mar 20, 2024
9349064
Merge pull request #23 from Nasruddin/dependabot/maven/spring-cloud/a…
Nasruddin Mar 21, 2024
ec2b775
Updated spring boot version to support 3.4.3 and spring cloud to 2024…
Nasruddin Feb 28, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
18 changes: 18 additions & 0 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Docker Image CI

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:

build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Build the Docker image
run: docker build . --file Dockerfile --tag my-image-name:$(date +%s)
35 changes: 35 additions & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Java CI with Maven

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
cache: maven
- name: Build with Maven
run: mvn -B package --file pom.xml

# Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive
- name: Update dependency graph
uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/.idea/
*.*
/Status

50 changes: 49 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,52 @@
# spring-boot-based-microservices

Basic skeleton for Spring Boot Microservices. It includes spring spring security for basic Auth. Zuul is also implemented as an API gateway. Lots of the spring cloud component integrated.
Basic skeleton for Spring Boot Microservices. It includes spring security for basic Auth. Spring cloud gateway is also implemented as an Edge Service. Lots of the spring cloud component integrated.

# How to run

- Navigate to root of the project
```
cd spring-boot-based-microservices
```
- Build the project
```
mvn clean package -DskipTests
```
![Maven Build](https://github.com/Nasruddin/spring-boot-based-microservices/blob/master/images/build.png?raw=true)

- Locate the docker directory from the root directory and run docker compose to startup the containers
```
cd docker && docker compose up --build
```
![Docker Compose Build](https://github.com/Nasruddin/spring-boot-based-microservices/blob/master/images/docker-compose.png?raw=true)

- Check if all our services are running and healthy
```
docker ps
```
![Docker PS](https://github.com/Nasruddin/spring-boot-based-microservices/blob/master/images/docker-ps.png?raw=true)

- Let's check if all the services are up and running. We will reach to eureka server using gateway.
Open the browser and hit http://localhost:8443/eureka/web You will need to authenticate yourself before accessing the endpoint.
```
username : user
password : password
```
![Eureka](https://github.com/Nasruddin/spring-boot-based-microservices/blob/master/images/eureka.png?raw=true)

- Now, we have a look at our gateway endpoints configurations as well. Hit http://localhost:8443/actuator/gateway/routes in the browser again and, you should be able to find all the routes configured.
![Gateway Routes](https://github.com/Nasruddin/spring-boot-based-microservices/blob/master/images/gateway-routes.png?raw=true)


- Coming to swagger/openapi specs, here is the address to access them - http://localhost:8443/openapi/swagger-ui.html
![Swagger OpenApi Specs](https://github.com/Nasruddin/spring-boot-based-microservices/blob/master/images/swagger-openapi.png?raw=true)

- Please use the below curl to generate the access token with both read and write scope.
```
curl -k http://writer:secret-writer@localhost:8443/oauth2/token -d grant_type=client_credentials -d scope="course:read course:write"
```
![Swagger OpenApi Specs](https://github.com/Nasruddin/spring-boot-based-microservices/blob/master/images/oauth-endpoint.png?raw=true)
![Swagger OpenApi Specs](https://github.com/Nasruddin/spring-boot-based-microservices/blob/master/images/jwt-io.png?raw=true)

## Note : Currently this project uses Spring Authorization server. Keep in mind, I'm planning to enhance the whole OAuth flow because it's in shaky state right now and, you may some face issues. We may also move to keycloak.
## These instructions are basic starting point and, the project does lot of other stuff like elastic and mongo configuration etc. Please feel free to play around. I really want to maintain this and keep up-to-date, however my current role doesn't give me enough spare time. Therefore, contributors and their contributions are welcome :)
20 changes: 14 additions & 6 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<version>3.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>io.javatab.microservices.api</groupId>
<artifactId>api</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>api</name>
<description>Demo project for Spring Boot</description>
<properties>
Expand Down Expand Up @@ -41,11 +42,18 @@
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.4.3</version> <!-- Match your Spring Boot version -->
<executions>
<execution>
<id>repackage</id>
<phase>none</phase>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
@SpringBootTest()
class ApiApplicationTests {

@Test
Expand Down
2 changes: 2 additions & 0 deletions config-repo/auth-server.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
server.port: 9999
server.forward-headers-strategy: framework
6 changes: 3 additions & 3 deletions config-repo/course-composite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ springdoc:
cache:
disabled: true
swagger-ui:
oauth2-redirect-url: https://localhost:8443/webjars/swagger-ui/oauth2-redirect.html
oauth2-redirect-url: http://localhost:8443/webjars/swagger-ui/oauth2-redirect.html
oauth:
clientId: writer
clientSecret: secret
useBasicAuthenticationWithAccessCodeGrant: true
oAuthFlow:
authorizationUrl: https://localhost:8443/oauth2/authorize
tokenUrl: https://localhost:8443/oauth2/token
authorizationUrl: http://localhost:8443/oauth2/authorize
tokenUrl: http://localhost:8443/oauth2/token

server.forward-headers-strategy: framework

Expand Down
17 changes: 17 additions & 0 deletions config-repo/eureka-server.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
server:
port: 8761

eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
# from: https://github.com/spring-cloud-samples/eureka/blob/master/src/main/resources/application.yml
server:
waitTimeInMsWhenSyncEmpty: 0
response-cache-update-interval-ms: 5000


76 changes: 76 additions & 0 deletions config-repo/gateway.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
server.port: 8443
test1.data: helloupdate
logging:
level:
root: INFO
org.springframework.cloud.gateway.route.RouteDefinitionRouteLocator: INFO
org.springframework.cloud.gateway: TRACE
org.springframework.web.server.adapter.HttpWebHandlerAdapter: TRACE

spring.cloud.gateway.routes:
- id: course-composite
uri: lb://course-composite
predicates:
- Path=/course-composite/**

- id: oauth2-server
uri: lb://auth-server
predicates:
- Path=/oauth2/**

- id: oauth2-login
uri: lb://auth-server
predicates:
- Path=/login/**

- id: oauth2-error
uri: lb://auth-server
predicates:
- Path=/error/**

- id: course-composite-swagger-ui
uri: lb://course-composite
predicates:
- Path=/openapi/**

- id: course-composite-swagger-ui-webjars
uri: lb://course-composite
predicates:
- Path=/webjars/**

- id: eureka-api
uri: http://${app.eureka-server}:8761
predicates:
- Path=/eureka/api/{segment}
filters:
- SetPath=/eureka/{segment}

- id: eureka-web-start
uri: http://${app.eureka-server}:8761
predicates:
- Path=/eureka/web
filters:
- SetPath=/

- id: eureka-web-other
uri: http://${app.eureka-server}:8761
predicates:
- Path=/eureka/**
- id: config-server
uri: ${spring.cloud.config.uri}
predicates:
- Path=/config/**
filters:
- RewritePath=/config/(?<segment>.*), /$\{segment}

spring.security.oauth2.resourceserver.jwt.issuer-uri: http://${app.auth-server}:9999


management.endpoint.gateway.enabled: true
management.endpoints.web.exposure.include: "*"

#server.ssl:
# key-store-type: PKCS12
# key-store: classpath:keystore/edge.p12
# key-store-password: password
# key-alias: localhost
16 changes: 8 additions & 8 deletions create-project.bash
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env bash

mkdir microservices
cd microservices
mkdir spring-boot-based-microservices
cd spring-boot-based-microservices

spring init \
--boot-version=2.5.2 \
--boot-version=3.4.3 \
--build=maven \
--java-version=17 \
--packaging=jar \
Expand All @@ -16,7 +16,7 @@ spring init \
student-service

spring init \
--boot-version=2.5.2 \
--boot-version=3.4.3 \
--build=maven \
--java-version=17 \
--packaging=jar \
Expand All @@ -28,7 +28,7 @@ spring init \
course-service

spring init \
--boot-version=2.5.2 \
--boot-version=3.4.3 \
--build=maven \
--java-version=17 \
--packaging=jar \
Expand All @@ -40,7 +40,7 @@ spring init \
vote-service

spring init \
--boot-version=2.5.2 \
--boot-version=3.4.3 \
--build=maven \
--java-version=17 \
--packaging=jar \
Expand All @@ -52,7 +52,7 @@ spring init \
search-service

spring init \
--boot-version=2.5.2 \
--boot-version=3.4.3 \
--build=maven \
--java-version=17 \
--packaging=jar \
Expand All @@ -63,4 +63,4 @@ spring init \
--version=1.0.0 \
course-composite-service

cd ..
cd ..
2 changes: 0 additions & 2 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '2.1'

services:
course:
build: ../microservices/course-service
Expand Down
Binary file added images/build.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/docker-compose.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/docker-ps.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/eureka.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/gateway-routes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/jwt-io.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/oauth-endpoint.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/swagger-openapi.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions microservices/course-composite-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# stage 1
# Start with a base image containing Java runtime
# TODO :: Upgrade to slim jre version for 17 once available
FROM openjdk:17-alpine as builder
FROM eclipse-temurin:17-jre-alpine as builder
WORKDIR application
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} application.jar
RUN java -Djarmode=layertools -jar application.jar extract

# the second stage of our build will copy the extracted layers
FROM openjdk:17-alpine
FROM eclipse-temurin:17-jre-alpine
WORKDIR application
# Copy extracted layers into the correct locations
COPY --from=builder application/dependencies/ ./
COPY --from=builder application/spring-boot-loader/ ./
COPY --from=builder application/snapshot-dependencies/ ./
COPY --from=builder application/application/ ./

EXPOSE 8080

ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]
ENTRYPOINT ["java", "org.springframework.boot.loader.launch.JarLauncher"]
Loading
Loading