美文网首页禅与计算机程序设计艺术软件架构 · 成为架构师系列
Diagrams 系统架构图绘制工具: 使用 Python 绘制

Diagrams 系统架构图绘制工具: 使用 Python 绘制

作者: 光剑书架上的书 | 来源:发表于2022-07-30 03:30 被阅读0次

    Diagrams

    Diagram as Code.

    Diagrams lets you draw the cloud system architecture in Python code. It was born for prototyping a new system architecture design without any design tools. You can also describe or visualize the existing system architecture as well. Diagrams currently supports main major providers including: AWS, Azure, GCP, Kubernetes, Alibaba Cloud, Oracle Cloud etc... It also supports On-Premise nodes, SaaS and major Programming frameworks and languages.

    Diagram as Code also allows you to track the architecture diagram changes in any version control system.

    NOTE: It does not control any actual cloud resources nor does it generate cloud formation or terraform code. It is just for drawing the cloud system architecture diagrams.

    Getting Started

    It requires Python 3.6 or higher, check your Python version first.

    It uses Graphviz to render the diagram, so you need to install Graphviz to use diagrams. After installing graphviz (or already have it), install the diagrams.

    Graphviz下载地址: https://graphviz.gitlab.io/download/

    macOS users can download the Graphviz via brew install graphviz if you're using Homebrew.

    $brew install graphviz
    

    然后,配置目录 /usr/local/Cellar/graphviz/5.0.0/bin 到PATH环境变量。

    $/usr/local/Cellar/graphviz/5.0.0/bin
    acyclic      circo        dijkstra     dot_builtins gc           gv2gml       gvgen        gvpack       gxl2gv       nop          prune        tred
    bcomps       cluster      dot          edgepaint    gml2gv       gv2gxl       gvmap        gvpr         mm2gv        osage        sccmap       twopi
    ccomps       diffimg      dot2gxl      fdp          graphml2gv   gvcolor      gvmap.sh     gxl2dot      neato        patchwork    sfdp         unflatten
    
    # using pip (pip3)
    $ pip3 install diagrams
    

    Or, using pipenv/poetry install:

    # using pipenv
    $ pipenv install diagrams
    
    # using poetry
    $ poetry add diagrams
    

    You can start with quick start. Check out guides for more details, and you can find all available nodes list in here.

    Quick Start

    Example 1

    # diagram1.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("diagram1", show=False):
        ELB("lb") >> EC2("web") >> RDS("userdb")
    

    This code generates below diagram.

    $ python diagram1.py
    
    

    It will be saved as diagram1.png on your working directory.

    Example 2

    from diagrams import Cluster, Diagram
    from diagrams.aws.compute import ECS
    from diagrams.aws.database import ElastiCache, RDS
    from diagrams.aws.network import ELB
    from diagrams.aws.network import Route53
    
    with 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
      
    

    Example 3

    from diagrams import Cluster, Diagram
    from diagrams.gcp.analytics import BigQuery, Dataflow, PubSub
    from diagrams.gcp.compute import AppEngine, Functions
    from diagrams.gcp.database import BigTable
    from diagrams.gcp.iot import IotCore
    from diagrams.gcp.storage import GCS
    
    with Diagram("Message Collecting", show=False):
        pubsub = PubSub("pubsub")
    
        with Cluster("Source of Data"):
            [IotCore("core1"),
             IotCore("core2"),
             IotCore("core3")] >> pubsub
    
        with Cluster("Targets"):
            with Cluster("Data Flow"):
                flow = Dataflow("data flow")
    
            with Cluster("Data Lake"):
                flow >> [BigQuery("bq"),
                         GCS("storage")]
    
            with Cluster("Event Driven"):
                with Cluster("Processing"):
                    flow >> AppEngine("engine") >> BigTable("bigtable")
    
                with Cluster("Serverless"):
                    flow >> Functions("func") >> AppEngine("appengine")
    
        pubsub >> flow
    
    

    Example 4

    from diagrams import Cluster, Diagram
    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("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
    

    Contributing

    To contribute to diagram, check out contribution guidelines.

    Let me know if you are using diagrams! I'll add you in showcase page. (I'm working on it!) :)

    Who uses it?

    GitPitch is the perfect slide deck solution for Tech Conferences, Training, Developer Advocates, and Educators. Diagrams is now available as a dedicated Cloud Diagram Markdown Widget so you can use Diagrams directly on any slide for conferences, meetups, and training.

    Cloudiscovery helps you to analyze resources in your cloud (AWS/GCP/Azure/Alibaba/IBM) account. It allows you to create a diagram of analyzed cloud resource map based on this Diagrams library, so you can draw your existing cloud infrastructure with Cloudiscovery.

    Airflow Diagrams is an Airflow plugin that aims to easily visualise your Airflow DAGs on service level from providers like AWS, GCP, Azure, etc. via diagrams.

    Other languages

    • If you are familiar to Go, you can use go-diagrams as well.

    License

    MIT

    相关文章

      网友评论

        本文标题:Diagrams 系统架构图绘制工具: 使用 Python 绘制

        本文链接:https://www.haomeiwen.com/subject/hndcwrtx.html