We would like to design a website where students can post their opinion about a teacher. This website should have following functions: 1. Clients can sign up a new account 2. Clients can post evaluation under courses they attended 3. Clients can view other students’ evaluation
The backend application is created based on Flask - a web service framework from Python ecosystem, so, a python environment need to be installed before run the application.
Just download Python from its official site and install it. Also pip has already been packaged inside Python since latest version, it is better to install one from pip's official site if the OS is Mac OSX.
Run the following command to install all dependencies:
pip install -r requirements.txt
IMPORTANT: Don't forget update requirements.txt once we add/remove python dependencies such as Flask, SQLAlchemy, etc.
Run the following command to start the backend server:
python backup.py
Just open the index.html from a browser.
In case of changing something in the source codes, you can rebuild the html files with necessary JS files by the following steps:
- Install NodeJS and Yarn
- From the project folder run the following commands.
yarn install grunt build
Reference to official site.
# start
docker-compose -p ce up -d --scale consul=3
# stop
docker-compose -p ce down
# List
docker-compose -p ce ps
Refer to Control startup order in Compose, choose the wait-for as the solution.
Instead of recording API visits into Redis directly from the Backend server, send a visiting event to Kafka. A new microservice - consumer.py - will be listening to those events and incr API usages in Redis.
someone provides a python lib for access kafka https://github.com/dpkp/kafka-python
# 在当前目录内创建一个简单的网页 - index.html
docker run -d --name web -p 8000:8000 -v "$PWD":/app -w /app python:alpine python -m http.server 8000
# 在浏览器中访问 http://localhost:8000 应该出现刚刚创建的网页内容
Similar to this, we can split backend.py into multiple python programs. then start them in their own docker containers respectively.
We need a custom docker image to run our own python app since they require flask and many other python libraries. Assume we want the name of the image to be "ce-server", then from our project root directory run the following docker command to build our image.
docker build -t consul-python .
Choose Consul for simplicity by leverage Docker official Consul image.
docker run --rm --name consul -p 8500:8500 -p 8400:8400 consul agent -server -bootstrap -client 0.0.0.0 -ui
docker run --rm --name consul1 --link consul consul agent -bind=0.0.0.0 -retry-join=consul
Then consul will provide a simple UI at http://localhost:8500, access it to make sure the installation is successful.
#### Start backend services
docker run -it --rm --name backend -p 5000:5000 -v "$PWD":/app -w /app ce-server python backend.py
#### Query service address
##### From DNS
1. Query IP from A record
docker exec ce_consului_1 dig @172.20.0.2 -p 8600 redis.service.consul
docker exec ce_consului_1 dig +noall +answer @172.20.0.2 -p 8600 redis.service.consul
2. Query port from SRV record
docker exec ce_consului_1 dig srv +noall +answer @172.20.0.2 -p 8600 redis.service.consul