This repo contains a simple, containerised microservice architecture, written in Java and using the Spring framework. The purpose of this project was to learn more about the interaction between microservices and their supporting infrastructure (and not to create any meaningful API endpoints).
- At this point, the notification service only records logs for each event received. It's only purpose was to provide the foundation of playground to work with Kafka.
- I have intentionally kept this project single-repo (code & Docker Hub images), even though this is not what I would do for a real application.
The following inventory is created for testing purposes:
Name | Quantity |
---|---|
Bicycle | 20 |
Scooter | 100 |
Car | 3 |
Bus | 0 |
POST api/order
- Expects JSON with
customerId
andorderLineItemDtoList
(containingname
,price
andquantity
) - Example:
{ "customerId": 5, "orderLineItemDtoList":[ { "name": "Scooter", "price": 5000.00, "quantity": 1 } ] }
- If successful, returns HTTP status code
201
and JSON message containing order number - for example:{ "status": "CREATED", "message": "Order placed successfully.", "orderNumber": "b835fd6d-052a-4f66-a2a4-8be7181fbcf7" }
- Depending on error type, returns
400
,500
,503
, or504
- Requests are made to
http://localhost:8181
(requires valid OAUth2.0 token, see 'Generate OAUth 2.0 token with Postman' below) - pgAdmin is available at
http://localhost:5050
(password:password
) - KeyCloak is available at
http://localhost:8080
(username:admin
, password:admin
)
- Clone repo
- Run Docker Compose
- Run
docker compose up -d
- Run
- Generate a new client secret
- Go to
http://localhost:8080/
and log into KeyCloak using the usernameadmin
and passwordadmin
- Select
Clients
,microservices-cloud-client
,Credentials,
and clickRegenerate
in the 'Client Secret' column - Copy the secret to generate a token (see 'Generate OAUth 2.0 token with Postman' below)
- Go to
- Generate token and start sending request
- Generate a valid OAuth2.0 token e.g. with Postman
- Using a valid token, you can now send request to
localhost:8181
- You can also use pgAdmin to view the tables at
localhost:5050
- The Eureka discovery server status page is accessible at
localhost:8181/eureka/web
Only recommended if you want to expand on this project.
- Clone repo
- Create separate repo for configurations
- Manually create second, local Git repo for configuration files
- Paste contents of the folder
resources/config-repo-files
into the newly created repo - Add and commit all files to the newly created repo
- Open
application.properties
underconfig-server/src/main/resources
- Update
spring.cloud.config.server.git.uri=file://${user.home}/IdeaProjects/Practice-Microservices-Config-Repo/
to reflect the correct path of the newly created repo
- Set credentials...
- ...for your local Postgres database by (a) replacing variables in
application.properties
in config repo with the actual credentials or (b) injecting them as environmental variables when starting services - ...for the discovery server repeat (a) or (b) above (default credentials are
eureka
/password
)
- ...for your local Postgres database by (a) replacing variables in
- Set up KeyCloak on Docker
- Follow the steps provided by KeyCloak - Getting Started on Docker to set up KeyCloak as a Docker image
- Use default configuration with the following exceptions:
- Make sure to set the port to
8181
or update theapi-gateway.properties
file accordingly - When creating a client, set
client authentication
toOn
and enableService accounts roles
- When creating a client, disable
Standard flow
andDirect access grants
- Make sure to set the port to
- Configure properties file for Jib
- Open
properties.gradle
in the root folder of the project and update your DockerHub credentials, or - Add a new file
local.properties
in your.gradle/
folder with your DockerHub credentials
- Open
- Build all services and run them in the following order:
discovery-server
config-server
api-gateway
- All other services
- Generate token and start sending request
- You can now use Postman or other apps to send request
- Make sure to send your requests with a valid OAuth2.0 token
Without further modification (and when not using Docker Compose), ports used by default are:
8080
-api-gateway
8761
-discovery-server
8181
- KeyCloak Docker image
Ports for all other services will be assigned and controlled by the discovery server. As a result, all requests to business services must be made through localhost:8080
.
- Create a new request
- Select
Authorization
and set the following:- Access token URL =
http://keycloak:8080/realms/practice-microservices-realm/protocol/openid-connect/token
- Client ID =
microservices-cloud-client
- Client Secret (follow steps above to generate from KeyCloak admin portal)
- Scope =
openid offline_access
- Access token URL =
- Click "Get New Access Token"