当前位置:首页 >热点 >使用Diagrams画架构图,你会吗? 使用Diagrams画架构图

使用Diagrams画架构图,你会吗? 使用Diagrams画架构图

2024-06-30 22:28:24 [百科] 来源:避面尹邢网

使用Diagrams画架构图,你会吗你会吗?

作者:码匠乱炖 开发 开发工具 Diagrams是使用基于Python的一款Diagram as Code工具,它最大的画架特点就是提供了很多云厂商及开源组件的图标,画出来的构图图显得更专业一点,也更易懂一点。你会吗

最近发现一个画架构图的使用神器diagrams,提供了很多云厂商及开源组件的画架图标,相比于C4-PlantUML显得更专业一点。构图
之前写过技术文档画图工具箱,你会吗diagrams属于diagram as code工具派别。使用

使用Diagrams画架构图,你会吗? 使用Diagrams画架构图

mac安装

brew install graphvizpip install diagramsbrew install python@3.11

示例1

from diagrams import Diagramfrom diagrams.aws.compute import EC2from diagrams.aws.database import RDSfrom diagrams.aws.network import ELB# python aws_example.pywith Diagram("Grouped Workers",画架 show=False, direction="TB"):    ELB("lb") >> [EC2("worker1"),                  EC2("worker2"),                  EC2("worker3"),                  EC2("worker4"),                  EC2("worker5")] >> RDS("events")

执行python example.py即可以在当前目录生成png图片。

使用Diagrams画架构图,你会吗? 使用Diagrams画架构图

使用Diagrams画架构图,你会吗? 使用Diagrams画架构图

示例2

from diagrams import Cluster,构图 Diagramfrom diagrams.aws.compute import ECSfrom diagrams.aws.database import ElastiCache, RDSfrom diagrams.aws.network import ELBfrom diagrams.aws.network import Route53with Diagram("Clustered Web Services", show=False):    dns = Route53("dns")    lb = ELB("lb")    with Cluster("Services"):        svc_group = [ECS("web1"),                     ECS("web2"),                     ECS("web3")]    with Cluster("DB Cluster"):        db_primary = RDS("userdb")        db_primary - [RDS("userdb ro")]    memcached = ElastiCache("memcached")    dns >> lb >> svc_group    svc_group >> db_primary    svc_group >> memcached

基本语法就是import node,以with Diagram开始,你会吗之后声明组件,使用然后使用with来进行分组,画架最后通过>>来串联。
默认文件名是Diagram名,空格替换为下划线,可以用filename指定。
图片格式默认是png,可以用outformat=[“jpg”, “png”, “dot”]来指定要生成的图片类型。
show默认为True,也就是python生成完图片会默认打开图片。

k8s示例

from diagrams import Cluster, Diagramfrom diagrams.k8s.compute import Pod, StatefulSetfrom diagrams.k8s.network import Servicefrom diagrams.k8s.storage import PV, PVC, StorageClasswith Diagram("Stateful Architecture", show=False):    with Cluster("Apps"):        svc = Service("svc")        sts = StatefulSet("sts")        apps = []        for _ in range(3):            pod = Pod("pod")            pvc = PVC("pvc")            pod - sts - pvc            apps.append(svc >> pod >> pvc)    apps << PV("pv") << StorageClass("sc")

开源组件示例

from diagrams import Cluster, Diagramfrom diagrams.onprem.analytics import Sparkfrom diagrams.onprem.compute import Serverfrom diagrams.onprem.database import PostgreSQLfrom diagrams.onprem.inmemory import Redisfrom diagrams.onprem.aggregator import Fluentdfrom diagrams.onprem.monitoring import Grafana, Prometheusfrom diagrams.onprem.network import Nginxfrom diagrams.onprem.queue import Kafkawith Diagram("Advanced Web Service with On-Premise", show=False):    ingress = Nginx("ingress")    metrics = Prometheus("metric")    metrics << Grafana("monitoring")    with Cluster("Service Cluster"):        grpcsvc = [            Server("grpc1"),            Server("grpc2"),            Server("grpc3")]    with Cluster("Sessions HA"):        primary = Redis("session")        primary - Redis("replica") << metrics        grpcsvc >> primary    with Cluster("Database HA"):        primary = PostgreSQL("users")        primary - PostgreSQL("replica") << metrics        grpcsvc >> primary    aggregator = Fluentd("logging")    aggregator >> Kafka("stream") >> Spark("analytics")    ingress >> grpcsvc >> aggregator

主要结构

node

# aws resourcesfrom diagrams.aws.compute import ECS, Lambdafrom diagrams.aws.database import RDS, ElastiCachefrom diagrams.aws.network import ELB, Route53, VPC...# azure resourcesfrom diagrams.azure.compute import FunctionAppsfrom diagrams.azure.storage import BlobStorage...# alibaba cloud resourcesfrom diagrams.alibabacloud.compute import ECSfrom diagrams.alibabacloud.storage import ObjectTableStore...# gcp resourcesfrom diagrams.gcp.compute import AppEngine, GKEfrom diagrams.gcp.ml import AutoML ...# k8s resourcesfrom diagrams.k8s.compute import Pod, StatefulSetfrom diagrams.k8s.network import Servicefrom diagrams.k8s.storage import PV, PVC, StorageClass...# oracle resourcesfrom diagrams.oci.compute import VirtualMachine, Containerfrom diagrams.oci.network import Firewallfrom diagrams.oci.storage import FileStorage, StorageGateway

完整版见nodes

数据流及布局

  • >>表示从左到右连接
  • <<表示从右到左连接
  • -表示无方向的连接

Diagram有个属性direction来表示整体布局,可选的值有TB, BT, LR及RL,默认是LR,即从左到右

TB: top to bottom
BT: bottom to top
LR: left to right
RL: right to left

Cluster用于分组,也支持内嵌,比如

with Cluster("Event Flows"):        with Cluster("Event Workers"):            workers = [ECS("worker1"),                       ECS("worker2"),                       ECS("worker3")]        queue = SQS("event queue")        with Cluster("Processing"):            handlers = [Lambda("proc1"),                        Lambda("proc2"),                        Lambda("proc3")]

连接符之间可以用Edge来衔接,用于个性化处理边的属性,比如

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

小结

diagrams是基于python的一款diagram as code工具,它最大的特点就是提供了很多云厂商及开源组件的图标,画出来的图显得更专业一点,也更易懂一点。

doc

  • diagrams
  • Diagrams: Diagram as Code
  • diagrams examples
  • 技术文档画图工具箱
责任编辑:姜华 来源: 今日头条 DiagramsPython工具

(责任编辑:综合)

    推荐文章
    热点阅读