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

序最近发现一个画架构图的神器diagrams,提供了很多云厂商及开源组件的图标,相比于C4-PlantUML显得更专业一点 。
之前写过技术文档画图工具箱,diagrams属于diagram as code工具派别 。
mac安装brew install graphvizpip install diagramsbrew install Python/ target=_blank class=infotextkey>Python@3.11示例示例1from 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画架构图,你会吗?

文章插图
 
示例2from 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_groupsvc_group >> db_primarysvc_group >> memcached
使用Diagrams画架构图,你会吗?

文章插图
 
基本语法就是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 - pvcapps.append(svc >> pod >> pvc)apps << PV("pv") << StorageClass("sc")
使用Diagrams画架构图,你会吗?

文章插图
 
开源组件示例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") << metricsgrpcsvc >> primarywith Cluster("Database HA"):primary = PostgreSQL("users")primary - PostgreSQL("replica") << metricsgrpcsvc >> primaryaggregator = Fluentd("logging")aggregator >> Kafka("stream") >> Spark("analytics")ingress >> grpcsvc >> aggregator
使用Diagrams画架构图,你会吗?

文章插图
 
主要结构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


推荐阅读