最近发现一个画架构图的神器diagrams,提供了很多云厂商及开源组件的图标,相比于C4-PlantUML显得更专业一点。
之前写过技术文档画图工具箱,diagrams属于diagram as code工具派别。
brew install graphvizpip install diagramsbrew install python@3.11
from diagrams import Diagramfrom diagrams.aws.compute import EC2from diagrams.aws.database import RDSfrom diagrams.awswork 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图片。
from diagrams import Cluster, Diagramfrom diagrams.aws.compute import ECSfrom diagrams.aws.database import ElastiCache, RDSfrom diagrams.awswork import ELBfrom diagrams.awswork 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生成完图片会默认打开图片。
from diagrams import Cluster, Diagramfrom diagrams.k8s.compute import Pod, StatefulSetfrom diagrams.k8swork 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.onpremwork 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
# aws resourcesfrom diagrams.aws.compute import ECS, Lambdafrom diagrams.aws.database import RDS, ElastiCachefrom diagrams.awswork 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.k8swork import Servicefrom diagrams.k8s.storage import PV, PVC, StorageClass...# oracle resourcesfrom diagrams.oci.compute import VirtualMachine, Containerfrom diagrams.ociwork 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工具,它最大的特点就是提供了很多云厂商及开源组件的图标,画出来的图显得更专业一点,也更易懂一点。
本文链接:http://www.28at.com/showinfo-26-10566-0.html使用Diagrams画架构图,你会吗?
声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。邮件:2376512515@qq.com
上一篇: 图解「正向代理」的原理 + 实践应用