当前位置:网站首页>Implementation of cqrs architecture mode under Kratos microservice framework
Implementation of cqrs architecture mode under Kratos microservice framework
2022-07-03 18:32:00 【Kratos open source community】
Separation of responsibility for command inquiry Command Query Responsibility Segregation Usually simplified as Command query separation , That is, the separation of reading and writing .
In a specific scenario , It can provide better performance . however , In terms of strong consistency , It does not guarantee . and , It will also bring cognitive burden . therefore , In practice , Need to be careful .
What is? CQRS
This concept comes from Command and query are separated (CQS, Command Query Separation), From 1987 year Bertrand Meyer Of
command (Command): After execution , Will change the state of the object . Inquire about (Query): Just view the data of the object , Without changing the object .
and Separation of responsibility for command inquiry Command Query Responsibility Segregation ( abbreviation CQRS) Pattern is an architecture pattern , It can separate the command of changing the state of the model from the query of the state of the model .
In the era of monomer application , It is read-write separation :

In the era of micro Services , It becomes the separation of responsibility of command query :

What does the separation of reading and writing solve ?
The separation of read and write of database is : Divide the database into master and slave databases , A main library for writing data , Multiple slave libraries complete the operation of reading data , The master and slave libraries synchronize data through some mechanism .
Most Internet businesses , Often read more and write less . Now , The reading of the database will first become the bottleneck of the database . At this time , If we want to improve the read performance of the database linearly , Eliminate read-write lock conflicts to improve the write performance of the database , Then you can use the read-write separation architecture : Master-slave , Master, master, etc .
MySQL The most used is master-slave , The main database passes BinLog Synchronize to the slave database . This raises a question , Data inconsistency . If the pressure of writing data is great ,binlog Congestion will occur , The slave database data is not updated in time , Will read the old dirty data . So this scheme limits its scope of application : Only the scenes with low requirements for consistency can be used . such as , Log query , Reports, etc. .
Realization CQRS
Here we discuss the access scenario of sequential data of the Internet of things .
We are divided into two micro Services :
Log query service (kratos.logger.service)
Mainly open API Used to query the database , Get log data .
Log writing service (kratos.logger.job)
subscribe Kafka Log data write Topic, Write to the timing database .
Docker Deploy the development server
TimeScaleDB
docker pull timescale/timescaledb:latest-pg14
docker pull timescale/timescaledb-postgis:latest-pg13
docker pull timescale/pg_prometheus:latest-pg11
docker run -itd \
--name timescale-test \
-p 5432:5432 \
-e POSTGRES_PASSWORD=123456 \
timescale/timescaledb-postgis:latest-pg13
Kafka
docker pull bitnami/kafka:latest
docker pull bitnami/zookeeper:latest
docker pull hlebalbau/kafka-manager:latest
docker run -itd \
--name zookeeper-test \
-p 2181:2181 \
-e ALLOW_ANONYMOUS_LOGIN=yes \
bitnami/zookeeper:latest
docker run -itd \
--name kafka-standalone \
--link zookeeper-test \
-p 9092:9092 \
-v /home/data/kafka:/bitnami/kafka \
-e KAFKA_BROKER_ID=1 \
-e KAFKA_LISTENERS=PLAINTEXT://:9092 \
-e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://127.0.0.1:9092 \
-e KAFKA_ZOOKEEPER_CONNECT=zookeeper-test:2181 \
-e ALLOW_PLAINTEXT_LISTENER=yes \
--user root \
bitnami/kafka:latest
docker run -itd \
-p 9000:9000 \
-e ZK_HOSTS="localhost:2181" \
hlebalbau/kafka-manager:latest
Consul
docker pull bitnami/consul:latest
docker run -itd \
--name consul-server-standalone \
-p 8300:8300 \
-p 8500:8500 \
-p 8600:8600/udp \
-e CONSUL_BIND_INTERFACE='eth0' \
-e CONSUL_AGENT_MODE=server \
-e CONSUL_ENABLE_UI=true \
-e CONSUL_BOOTSTRAP_EXPECT=1 \
-e CONSUL_CLIENT_LAN_ADDRESS=0.0.0.0 \
bitnami/consul:latest
Jaeger
docker pull jaegertracing/all-in-one:latest
docker run -d \
--name jaeger \
-e COLLECTOR_ZIPKIN_HOST_PORT=:9411 \
-p 5775:5775/udp \
-p 6831:6831/udp \
-p 6832:6832/udp \
-p 5778:5778 \
-p 16686:16686 \
-p 14268:14268 \
-p 14250:14250 \
-p 9411:9411 \
jaegertracing/all-in-one:latest
test
Download tool
Postman[1] Offset Explorer[2]
To test
Write the test
Use Offset Explorer Analog equipment , towards Topic logger.sensor.ts send out JSON data :
[{"ts": 1646409307, "sensor_id": 1, "temperature":30, "cpu":20}]
Test reading
Use Postman Initiate to log service gRPC Ask for a query .
Technology stack
Kratos[3] TimeScaleDB[4] Kafka[5] Consul[6] Jaeger[7] Entgo[8]
The sample code
Kratos Examples[9]
Reference material
Postman: https://www.postman.com/downloads/
[2]Offset Explorer: https://www.kafkatool.com/download.html
[3]Kratos: https://go-kratos.dev/
[4]TimeScaleDB: https://www.timescale.com/
[5]Kafka: https://kafka.apache.org/
[6]Consul: https://www.consul.io/
[7]Jaeger: https://www.jaegertracing.io/
[8]Entgo: https://entgo.io/
[9]Kratos Examples: https://github.com/go-kratos/examples/tree/main/cqrs
[10]Talking about CQRS The implementation of : https://medium.brobridge.com/%E6%B7%BA%E8%AB%87-cqrs-%E5%AF%A6%E7%8F%BE%E6%96%B9%E6%B3%95-3b4fcb8d5c86
[11]Talking about the principle of micro service splitting : https://medium.brobridge.com/%E6%B7%BA%E8%AB%87%E5%BE%AE%E6%9C%8D%E5%8B%99%E6%8B%86%E5%88%86%E5%8E%9F%E7%90%86-d43fbb33e722
[12]Detailed explanation CQRS Architecture mode : https://www.infoq.cn/article/wdlpjosudoga34jutys9


边栏推荐
- The vscode code is automatically modified to a compliance code when it is formatted and saved
- Analysis of the reasons why enterprises build their own software development teams to use software manpower outsourcing services at the same time
- Should I be laid off at the age of 40? IBM is suspected of age discrimination, calling its old employees "dinosaurs" and planning to dismiss, but the employees can't refute it
- After the festival, a large number of people change careers. Is it still time to be 30? Listen to the experience of the past people
- Computer graduation design PHP campus address book telephone number inquiry system
- [combinatorics] generating function (use generating function to solve the combination number of multiple sets R)
- Bloom filter [proposed by bloom in 1970; redis cache penetration solution]
- Bidding procurement scheme management of Oracle project management system
- Torch learning notes (6) -- logistic regression model (self training)
- On Data Mining
猜你喜欢

The vscode code is automatically modified to a compliance code when it is formatted and saved
![The number of incremental paths in the grid graph [dfs reverse path + memory dfs]](/img/57/ff494db248171253996dd6c9110715.png)
The number of incremental paths in the grid graph [dfs reverse path + memory dfs]

2022-2028 global petroleum pipe joint industry research and trend analysis report

Analysis of the reasons why enterprises build their own software development teams to use software manpower outsourcing services at the same time

模块九作业

Install apache+php+mysql+phpmyadmin xampp and its error resolution
![Golang string (string) and byte array ([]byte) are converted to each other](/img/41/20f445ef9de4adf2a2aa97828cb67f.jpg)
Golang string (string) and byte array ([]byte) are converted to each other

English grammar_ Adjective / adverb Level 3 - multiple expression

English语法_形容词/副词3级 - 倍数表达

2022-2028 global solid phase extraction column industry research and trend analysis report
随机推荐
How to expand the capacity of golang slice slice
Recent learning experience
2022-2028 global lithium battery copper foil industry research and trend analysis report
2022-2028 global solid phase extraction column industry research and trend analysis report
Change the single node of Postgres database into master-slave
PHP MySQL inserts multiple pieces of data
Closure and closure function
Win32: analyse du fichier dump pour la défaillance du tas
After the festival, a large number of people change careers. Is it still time to be 30? Listen to the experience of the past people
4. Load balancing and dynamic static separation
win32:堆破坏的dump文件分析
SDNUOJ1015
Theoretical description of linear equations and summary of methods for solving linear equations by eigen
Torch learning notes (4) -- torch's dynamic calculation diagram
Redis cache avalanche, penetration, breakdown
[combinatorics] generating function (example of generating function | calculating generating function with given general term formula | calculating general term formula with given generating function)
Image 24 bits de profondeur à 8 bits de profondeur
[combinatorics] generating function (positive integer splitting | unordered | ordered | allowed repetition | not allowed repetition | unordered not repeated splitting | unordered repeated splitting)
Nodejs (01) - introductory tutorial
Lesson 13 of the Blue Bridge Cup -- tree array and line segment tree [exercise]