1.2 万 Star!超实用的架构图绘制工具

Diagrams 是一个用于绘制云系统架构的开源工具,支持多种云服务提供商。本文介绍了如何安装使用 Diagrams,包括图、节点、集群和边的详细用法,帮助你轻松绘制系统架构图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【导语】:你还在用visio绘制架构图吗?这有一款绘制系统架构的开源工具,确定不来看看?

简介

Diagrams 可以让你用 Python 代码绘制云系统架构。无需使用任何其他工具(比如 visio),你就可以用它来为一个全新的系统架构进行原型设计。当然它也可以用来绘制你现有的系统架构。

Diagrams 目前支持的主要提供商包括:AWS、Azure、GCP、Kubernetes、阿里云、Oracle云等。同时它也支持本地节点、SaaS和主流的编程框架和语言。

Diagram as Code 还能让你跟踪各种版本控制系统中的体系结构图变化。

它不能控制任何实际的云资源也不能生成云信息或地形代码。它只能帮你绘制云系统架构图。

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

安装使用

需要 Python 3.6 版本及以上,使用前请先检测你的 python 版本。

绘制时需要使用 Graphviz,所以你需要先安装 Graphviz。

macOS 用户可以使用 brew install graphviz 命令来下载 Graphviz。

安装 diagrams
# using pip (pip3)  
$ pip install diagrams  

# using pipenv  
$ pipenv install diagrams  

# using poetry  
$ poetry add diagrams  

快速上手

# 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")  

运行此代码,将会生成一个如下的图:

在你的工作目录会生成一个web_service.png。

详细介绍

接下来我们主要分四块介绍一下 diagrams 的构成,分别是:图、节点、集群和边。

一、图

Diagram是展现关系图最关键的部分。

基础使用

你可以使用 Diagram 类来创建一个关系图的上下文。Diagram 构造函数的第一个参数是你输出的文件名。我们来看一个例子:

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

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

在 cmd 输入 python diagram.py 来执行下,代码会在你的代码目录生成一个 simple_diagram.png 文件,可以看到里面只有一个 EC2 节点。

Jupyter Notebooks

Diagrams也可以直接在Jupyter的notebook里使用,示例如下:

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

with Diagram("Simple Diagram") as diag:  
    EC2("web")  
diag  
可选项

你可以指定输出文件的格式,默认格式是.png,比如将默认的png格式改为jpg格式:

(png, jpg, svg, and pdf) 都是允许的格式

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

with Diagram("Simple Diagram", outformat="jpg"):  
 EC2("web")  

你也可以使用 filename 参数来指定输出的文件名,但此时不能再指定扩展名,因为扩展名是由 outformat 指定的,下面我们将文件名指定为 my_diagram:

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

with Diagram("Simple Diagram", filename="my_diagram"):  
    EC2("web")  
<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值