This project controls credit cards, customers, credit assessment and processes card issuance requests through a complete microservices architecture.
- Java 17
- Spring Cloud/Boot 2.7.0 Modules
- Service Discovery
- Api Gateway
- Load Balancing
- Synchronous and Asynchronous Microservices Communication
- Messaging Service/Queue with RabbitMQ
- Authorization Server with Keycloak
- Custom Images and Containers with Docker
- Microservices Replicas
- Installation
- Configuration for local environment
- Usage
- API Endpoints
- Authentication
- Database
- Contributing
- Configuration for production environment
- Clone the repository:
git clone https://github.com/kenzor1979/microservices
-
Install dependencies with Maven
-
Install Docker
-
Install RabbitMQ Install docker image "rabbitmq:3.13-management" and create a container with docker In the rabbitMQ, create a new queue "card-issuance"
-
Install Keycloak image "quay.io/keycloak/keycloak:18.0.0" and create a container with docker In keycloak, create a new realm with the import option. The "realm-export.json" file is located inside the "mskeycloak" folder. There are two files. "Realm-local" for the local environment and "realm-export.json" for the production environment that will be accessed by the microservices containers.
- Start the RabbitMQ and Keycloak containers with docker
- Start the application with Maven or IntelliJ IDEA
notes: Preferably start the applications in this order:
-
The APIs will be accessible on the eureka server at http://localhost:8761/
-
To access the swagger of each instance, click on the instance address in the eureka server to open the link in a new page. Add address + "/swagger-ui/index.html" For example, to access the client microservice address: http://host.docker.internal:55012/swagger-ui/index.html
All available endpoints will be displayed.
The APIs provide the following endpoints:
msclients
GET /clients - Retrieves the status of the API
GET /clients?cpf=123 - Retrieves all clients registered for that CPF
POST /clients - Persists the client's credit card data.
{
"nome": "person",
"cpf": "123",
"idade": 14
}
mscards
GET /cards - Retrieves the status of the API
GET /cards?renda=10000 - Retrieves all cards with income up to
GET /cards?cpf=123 - Retrieves all cards registered for that CPF
POST /cards - Persists the credit´s card data.
{
"nome": "bradesco visa",
"bandeira": "VISA",
"renda": "5000",
"limite": "8000"
}
mscreditassessor
GET /credit-assessor - Retrieves the status of the API
POST /credit-assessor - Register a new credit assessment
{
"cpf": "123",
"renda": "5000"
}
GET /credit-assessor/customer-status?cpf=123 - Retrieves all credit ratings registered for that CPF
POST /credit-assessor/solicitacoes-cartao - Registers a credit card request and returns a request protocol that is sent to the messaging service
{
"idCartao": "1",
"cpf": "123",
"endereco": "Rua Benjamin Constant",
"limiteLiberado": 11200
}
eurekaserver
Eureka server address on port 8761
http://localhost:8761/
mscloudgateway
Note: All cards, clients and credit-assessor API instances will be managed by the load balancer through the gateway at the address:
http://host.docker.internal:8080/
http://localhost:8081/realms/msrealm/protocol/openid-connect/token
The APIs use Spring Security and Keycloak with OAuth 2.0 for authentication control. The following roles are available:
Keycloak:
user: admin
password: admin
All endpoints are protected. There are two Keycloak endpoints, one for local environment and one for access between containers.
Example of Keycloak for local authentication request with Postman
Authorization type is Bearer Token.
Example of authorized authentication to use our endpoints
notes: The process to generate the authorization token for the microservices within the containers, just changes the address from port 8080 to port 8081 through the address http://localhost:8081/realms/msrealm/protocol/openid-connect/token
The project utilizes H2 as the database.
This database is created in memory automatically when each microservice is initialized.
Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request to the repository.
When contributing to this project, please follow the existing code style, commit conventions, and submit your changes in a separate branch.
- It´s necessary to create a network so that the application containers can communicate. Create it only once.
docker network create ms-network
- Inside each folder of each Mavem module, there are the following files:
"dockerBuild.bat": Will create a docker image with all the compiled files to run the microservice.
docker build --tag ms-clients .
"dockerRun.bat" Will create a container in docker with the microservice's production environment parameters.
docker run --name ms-clients --network ms-network -e EUREKA_SERVER=ms-eureka ms-clients
- Run all containers in docker via command line or through dockerDesktop.
Complete architecture of microservices running in containers
Example of accessing microservices through containers managed by the gateway with load balancing by instances
notes: In the production environment, all containers are accessed through the ms-network network, and the endpoints should be accessed through the gateway at the address:
http://host.docker.internal:8080/
Example:
http://host.docker.internal:8080/credit-assessor