python工具——diagrams

diagrams云系统架构原型图制作库

官网 https://diagrams.mingrammer.com/

github地址 https://github.com/mingrammer/diagrams

1.准备

Diagrams 使用 Graphviz 来渲染图表,先安装 Graphviz

2.安装 diagrams

pip install diagrams

 3.示例

diagram.py

from diagrams import Diagram
from diagrams.aws.compute import EC2
from diagrams.aws.database import RDS
from diagrams.aws.network import ELB

with Diagram("Web Service", show=False):
    ELB("lb") >> EC2("web") >> RDS("userdb")

运行

python diagram.py

Diagram 类的第一个参数,会成为架构图的文件名(转为小写,空格被下划线所替代)

web_service.png

 4.主要对象

  (1)Diagram 架构图

    使用with Diagram()

from diagrams import Diagram
from diagrams.aws.compute import EC2

with Diagram("Simple Diagram"):
    EC2("web")

生成一个包含 EC2 节点的架构图,在当前目录的 生成 simple_diagram.png 

常用参数说明:

  outformat:指定输出图片的类型,默认是 png,可以是 png、jpg、svg 和 pdf

  show:指定是否显示图片

  (2)Node 节点或系统组件

    支持的云资源节点有五类:AWS、Azure、AlibabaCloud、GCP 和 K8S

    节点之间的关系

>>:左节点指向右节点
<<:右节点指向左节点
-:节点互相连接,没有方向

eg:

from diagrams import Diagram
from diagrams.aws.compute import EC2
from diagrams.aws.database import RDS
from diagrams.aws.network import ELB
from diagrams.aws.storage import S3

with Diagram("Web Services", show=False):
    ELB("lb") >> EC2("web") >> RDS("userdb") >> S3("store")
    ELB("lb") >> EC2("web") >> RDS("userdb") << EC2("stat")
    (ELB("lb") >> EC2("web")) - EC2("web") >> RDS("userdb")

  (3)Cluster 集群或分组

     集群使用with Cluster()

     支持集群嵌套,实现方法:嵌套使用with Cluster()

from diagrams import Cluster, Diagram
from diagrams.aws.compute import ECS
from diagrams.aws.database import RDS
from diagrams.aws.network import Route53

with Diagram("Simple Web Service with DB Cluster", show=False):
    dns = Route53("dns")
    web = ECS("service")

    with Cluster("DB Cluster"):
        db_master = RDS("master")
        db_master - [RDS("slave1"),
                     RDS("slave2")]

    dns >> web >> db_master

     (4)Edge 边

      自定义线的颜色(color)与样式(style)以及备注(label)

eg:

from diagrams import Cluster, Diagram, Edge
from diagrams.onprem.analytics import Spark
from diagrams.onprem.compute import Server
from diagrams.onprem.database import PostgreSQL
from diagrams.onprem.inmemory import Redis
from diagrams.onprem.aggregator import Fluentd
from diagrams.onprem.monitoring import Grafana, Prometheus
from diagrams.onprem.network import Nginx
from diagrams.onprem.queue import Kafka

with Diagram(name="Advanced Web Service with On-Premise (colored)", show=False):
    ingress = Nginx("ingress")

    metrics = Prometheus("metric")
    metrics << Edge(color="firebrick", style="dashed") << Grafana("monitoring")

    with Cluster("Service Cluster"):
        grpcsvc = [
            Server("grpc1"),
            Server("grpc2"),
            Server("grpc3")]

    with Cluster("Sessions HA"):
        master = Redis("session")
        master - Edge(color="brown", style="dashed") - Redis("replica") << Edge(label="collect") << metrics
        grpcsvc >> Edge(color="brown") >> master

    with Cluster("Database HA"):
        master = PostgreSQL("users")
        master - Edge(color="brown", style="dotted") - PostgreSQL("slave") << Edge(label="collect") << metrics
        grpcsvc >> Edge(color="black") >> master

    aggregator = Fluentd("logging")
    aggregator >> Edge(label="parse") >> Kafka("stream") >> Edge(color="black", style="bold") >> Spark("analytics")

    ingress >> Edge(color="darkgreen") << grpcsvc >> Edge(color="darkorange") >> aggregator

 

posted @ 2021-01-07 23:43  慕尘  阅读(3530)  评论(0编辑  收藏  举报