1
0
mirror of https://github.com/ebosas/microservices.git synced 2025-02-22 18:41:59 +02:00
microservices/README.md

133 lines
3.7 KiB
Markdown
Raw Normal View History

2021-06-01 21:38:22 +03:00
# Microservices
2021-11-04 20:52:23 +02:00
A basic example of microservice architecture which demonstrates communication between a few loosely coupled services.
2021-06-01 21:38:22 +03:00
2021-06-02 14:43:57 +03:00
* Written in Go
* Uses RabbitMQ to communicate between services
* Uses WebSocket to talk to the front end
* Stores data in PostgreSQL
2021-11-01 17:10:20 +02:00
* Stores cache in Redis
2021-06-02 14:43:57 +03:00
* Uses React for front end development
2021-11-03 11:32:20 +02:00
* Builds with Docker
2021-11-04 20:51:27 +02:00
* Deployed as containers on AWS
2021-06-01 21:38:22 +03:00
2021-06-12 15:31:39 +03:00
![](demo.gif)
2021-11-03 08:36:06 +02:00
## Local use
2021-06-01 21:38:22 +03:00
2021-06-08 21:26:14 +03:00
To run the example, clone the Github repository and start the services using Docker Compose. Once Docker finishes downloading and building images, the front end is accessible by visiting `localhost:8080`.
2021-06-01 21:38:22 +03:00
```bash
git clone https://github.com/ebosas/microservices
cd microservices
```
```bash
docker-compose up
```
### Database
2021-06-02 14:43:57 +03:00
To inspect the database, launch a new container that will connect to our Postgres database. Then enter the password `demopsw` (see the `.env` file).
2021-06-01 21:38:22 +03:00
```bash
2021-06-11 16:18:47 +03:00
docker run -it --rm \
--network microservices_network \
postgres:13-alpine \
psql -h postgres -U postgres -d microservices
2021-06-01 21:38:22 +03:00
```
Select everything from the messages table:
```sql
select * from messages;
```
2021-06-02 12:38:11 +03:00
2021-11-01 17:10:20 +02:00
### Redis
To inspect Redis, connect to its container via redis-cli.
```bash
docker run -it --rm \
--network microservices_network \
redis:6-alpine \
redis-cli -h redis
```
Get all cached messages or show the number of messages.
```bash
lrange messages 0 -1
get total
```
2021-06-03 13:39:31 +03:00
### RabbitMQ
2021-06-02 12:38:11 +03:00
2021-06-02 14:43:57 +03:00
Access the RabbitMQ management interface by visiting `localhost:15672` with `guest` as both username and password.
2021-06-11 16:18:47 +03:00
2021-11-01 17:10:20 +02:00
### Back end
To access the back end service, attach to its docker container from a separate terminal window. Messages from the front end will show up here. Also, standart input will be sent to the front end for two way communication.
2021-10-05 16:10:14 +03:00
```bash
2021-11-01 17:10:20 +02:00
docker attach microservices_backend
2021-10-05 16:10:14 +03:00
```
2021-11-01 17:14:17 +02:00
## Deployment to Amazon ECS/AWS Fargate
2021-11-01 17:10:20 +02:00
2021-11-06 21:11:16 +02:00
`cd deployments` and create the CI/CD pipeline stack. Once finished, visit the `ExternalUrl` available in the load balancer's Outputs tab in CloudFormation.
2021-11-01 17:10:20 +02:00
2021-10-05 16:10:14 +03:00
```bash
2021-11-03 11:32:20 +02:00
aws cloudformation create-stack \
--stack-name MicroservicesFargate \
--template-body file://pipeline.yml \
--parameters \
ParameterKey=DeploymentType,ParameterValue=fargate \
ParameterKey=EnvironmentName,ParameterValue=microservices-fargate \
ParameterKey=GitHubRepo,ParameterValue=<github_repo_name> \
ParameterKey=GitHubBranch,ParameterValue=<github_branch> \
ParameterKey=GitHubToken,ParameterValue=<github_token> \
ParameterKey=GitHubUser,ParameterValue=<github_user> \
--capabilities CAPABILITY_NAMED_IAM
2021-11-01 17:10:20 +02:00
```
2021-11-03 11:32:20 +02:00
### Github repo setup
2021-11-01 17:10:20 +02:00
2021-11-03 11:32:20 +02:00
Fork this repo to have a copy in your Github account.
2021-11-01 17:10:20 +02:00
2021-11-03 11:32:20 +02:00
Then, on the [Github access token page](https://github.com/settings/tokens), generate a new token with the following access:
2021-10-05 16:10:14 +03:00
2021-11-03 11:32:20 +02:00
* `repo`
* `admin:repo_hook`
2021-11-01 17:10:20 +02:00
### Deleting stacks
2021-11-03 11:32:20 +02:00
When deleting the ECS cluster stack (`cluster-ecs.yml`) in CloudFormation, the auto scaling group needs to be manually deleted. You can do it from the Auto Scaling Groups section in the AWS EC2 console.
With capacity providers, container instances need to be protected from scale-in. This interferes with the automatic deletion process in CloudFormation.
2021-11-01 17:10:20 +02:00
### References
Deployment is based on these templates: https://github.com/nathanpeck/ecs-cloudformation
2021-11-03 11:32:20 +02:00
## Local development
2021-06-11 16:18:47 +03:00
For development, run the RabbitMQ and Postgres containers with Docker Compose.
```bash
docker-compose -f docker-compose-dev.yml up
```
Generate static web assets for the server service by going to `web/react` and `web/bootstrap` and running:
```bash
npm run build-server
```
### React
For React development, run `npm run serve` in `web/react` and change the script tag in the server's template to the following:
```html
<script src="http://127.0.0.1:8000/index.js"></script>
```