Skip to content

Practice project with a basic microservice architecture (API gateway, config & discovery server, and service applications with Postgres databases), written in Java using the Spring Framework.

License

Notifications You must be signed in to change notification settings

kimgoetzke/practice-microservices

Repository files navigation

The Microservices Project

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).

Architecture overview

Overview diagram

  • 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.

Instructions to use

The following inventory is created for testing purposes:

Name Quantity
Bicycle 20
Scooter 100
Car 3
Bus 0

Place order

POST api/order

  • Expects JSON with customerId and orderLineItemDtoList (containing name, price and quantity)
  • 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, or 504
Notes
  • 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)

Instructions to run

With Docker Compose (recommended)

  1. Clone repo
  2. Run Docker Compose
    1. Run docker compose up -d
  3. Generate a new client secret
    1. Go to http://localhost:8080/ and log into KeyCloak using the username admin and password admin
    2. Select Clients, microservices-cloud-client, Credentials, and click Regenerate in the 'Client Secret' column
    3. Copy the secret to generate a token (see 'Generate OAUth 2.0 token with Postman' below)
  4. Generate token and start sending request
    1. Generate a valid OAuth2.0 token e.g. with Postman
    2. Using a valid token, you can now send request to localhost:8181
    3. You can also use pgAdmin to view the tables at localhost:5050
    4. The Eureka discovery server status page is accessible at localhost:8181/eureka/web

Without Docker Compose (not recommended)

Only recommended if you want to expand on this project.

  1. Clone repo
  2. Create separate repo for configurations
    1. Manually create second, local Git repo for configuration files
    2. Paste contents of the folder resources/config-repo-files into the newly created repo
    3. Add and commit all files to the newly created repo
    4. Open application.properties under config-server/src/main/resources
    5. 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
  3. Set credentials...
    1. ...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
    2. ...for the discovery server repeat (a) or (b) above (default credentials are eureka / password)
  4. Set up KeyCloak on Docker
    1. Follow the steps provided by KeyCloak - Getting Started on Docker to set up KeyCloak as a Docker image
    2. Use default configuration with the following exceptions:
      1. Make sure to set the port to 8181 or update the api-gateway.properties file accordingly
      2. When creating a client, set client authentication to On and enable Service accounts roles
      3. When creating a client, disable Standard flow and Direct access grants
  5. Configure properties file for Jib
    1. Open properties.gradle in the root folder of the project and update your DockerHub credentials, or
    2. Add a new file local.properties in your .gradle/ folder with your DockerHub credentials
  6. Build all services and run them in the following order:
    1. discovery-server
    2. config-server
    3. api-gateway
    4. All other services
  7. Generate token and start sending request
    1. You can now use Postman or other apps to send request
    2. Make sure to send your requests with a valid OAuth2.0 token
Notes

Without further modification (and when not using Docker Compose), ports used by default are:

  1. 8080 - api-gateway
  2. 8761 - discovery-server
  3. 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.

Generate a valid OAuth2.0 token with Postman

  • 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
  • Click "Get New Access Token"

About

Practice project with a basic microservice architecture (API gateway, config & discovery server, and service applications with Postgres databases), written in Java using the Spring Framework.

Topics

Resources

License

Stars

Watchers

Forks

Languages