Intro to Structurizr – 结构化介绍

最后修改: 2017年 6月 3日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.介绍

This article is about Structurizr, a tool that provides a programmatic approach to architectural definitions and visualizations based on the C4 Model.

本文是关于Structurizr的,该工具提供了一种基于C4模型的架构定义和可视化的编程方法

Structurizr breaks with traditional drag-and-drop approaches of architectural diagram editors such as UML and allows us to describe our architectural artifacts using the tool we know best: Java.

Structurizr打破了传统的建筑图编辑器(如UML)的拖放方式,允许我们使用我们最熟悉的工具来描述我们的建筑工件。Java。

2. Getting Started

2.入门

To get started, let’s add the structurizr-core dependency to our pom.xml:

为了开始,让我们把structurizr-core依赖性添加到我们的pom.xml中:

<dependency>
    <groupId>com.structurizr</groupId>
    <artifactId>structurizr-core</artifactId>
    <version>1.0.0</version>
</dependency>

3. Systems

3.系统

Let’s start modeling a sample architecture. Suppose we’re building a fraud-detection capable payment terminal used by merchants for clearing payments.

让我们开始对一个样本架构进行建模。假设我们正在建立一个具有欺诈检测能力的支付终端,供商家用于结算付款。

First, we need to create a Workspace and a Model:

首先,我们需要创建一个Workspace和一个Model

Workspace workspace = new Workspace("Payment Gateway", "Payment Gateway");
Model model = workspace.getModel();

We also define a user and two software systems within that model:

我们还在该模型中定义了一个用户和两个软件系统。

Person user = model.addPerson("Merchant", "Merchant");
SoftwareSystem paymentTerminal = model.addSoftwareSystem(
  "Payment Terminal", "Payment Terminal");
user.uses(paymentTerminal, "Makes payment");
SoftwareSystem fraudDetector = model.addSoftwareSystem(
  "Fraud Detector", "Fraud Detector");
paymentTerminal.uses(fraudDetector, "Obtains fraud score");

Now that our system is defined, we can create a view:

现在我们的系统已经定义好了,我们可以创建一个视图。

ViewSet viewSet = workspace.getViews();

SystemContextView contextView = viewSet.createSystemContextView(
  paymentTerminal, "context", "Payment Gateway Diagram");
contextView.addAllSoftwareSystems();
contextView.addAllPeople();

Here we created a view that includes all software systems and persons. Now the view needs to be rendered.

这里我们创建了一个包括所有软件系统和人员的视图。现在需要对该视图进行渲染。

4. View via PlantUML

4.通过PlantUML查看

In the previous section, we created a view of a simple payment gateway.

在上一节中,我们创建了一个简单支付网关的视图。

Next step is to create a human-friendly diagram. Probably the simplest solution for an organization already using PlantUML would be to instruct Structurizr to do a PlantUML export:

下一步是创建一个人性化的图表。对于已经使用PlantUML的组织来说,最简单的解决方案可能是指示Structurizr做一个PlantUML导出。

StringWriter stringWriter = new StringWriter();
PlantUMLWriter plantUMLWriter = new PlantUMLWriter();
plantUMLWriter.write(workspace, stringWriter);
System.out.println(stringWriter.toString());

Here the resulting markup is printed to screen, but it may just as easily be sent to a file. Rendering the data this way produces the diagram below:

这里产生的标记被打印到屏幕上,但它也可以很容易地被发送到一个文件中。以这种方式渲染数据会产生下面的图表。

try

5. View via the Structurizr Website

5.通过Structurizr网站查看

Another option for rendering diagrams exists. An architectural view can be sent to the Structurizr website via a client API. The diagram will be then generated using their rich UI.

存在另一个渲染图的选项。一个建筑视图可以通过一个客户端API发送到Structurizr网站。然后,该图将使用他们丰富的用户界面生成。

Let’s can create an API client:

让我们可以创建一个API客户端。

StructurizrClient client = new StructurizrClient("key", "secret");

The key and secret parameters are obtained from the workspace dashboard on their website. The workspace can be then be referred to by:

密钥和秘密参数可从其网站上的工作区仪表板获得。然后,该工作区可以通过以下方式进行参考。

client.putWorkspace(1337, workspace);

Obviously, we need to register on the website and create a workspace. A basic account with a single workspace is free. At the same time, commercial plans are available as well.

很明显,我们需要在网站上注册并创建一个工作区。拥有一个工作区的基本账户是免费的。同时,也有商业计划。

6. Containers

6.集装箱

Let’s extend our software system by adding some containers. In a C4 model, containers can be web applications, mobile apps, desktop applications, databases and file systems: pretty much anything that holds code and/or data.

让我们通过添加一些容器来扩展我们的软件系统。在C4模型中,容器可以是网络应用、移动应用、桌面应用、数据库和文件系统:几乎所有持有代码和/或数据的东西。

First, we create some containers for our payment terminal:

首先,我们为我们的支付终端创建一些容器。

Container f5 = paymentTerminal.addContainer(
  "Payment Load Balancer", "Payment Load Balancer", "F5");
Container jvm1 = paymentTerminal.addContainer(
  "JVM-1", "JVM-1", "Java Virtual Machine");
Container jvm2 = paymentTerminal.addContainer(
  "JVM-2", "JVM-2", "Java Virtual Machine");
Container jvm3 = paymentTerminal.addContainer(
  "JVM-3", "JVM-3", "Java Virtual Machine");
Container oracle = paymentTerminal.addContainer(
  "oracleDB", "Oracle Database", "RDBMS");

Next, we define relationships between these newly created elements:

接下来,我们定义这些新创建的元素之间的关系。

f5.uses(jvm1, "route");
f5.uses(jvm2, "route");
f5.uses(jvm3, "route");

jvm1.uses(oracle, "storage");
jvm2.uses(oracle, "storage");
jvm3.uses(oracle, "storage");

Finally, create a container view that can be fed to a renderer:

最后,创建一个可以送入渲染器的容器视图。

ContainerView view = workspace.getViews()
  .createContainerView(paymentTerminal, "F5", "Container View");
view.addAllContainers();

Rendering the resulting diagram via PlantUML produces:

通过PlantUML渲染产生的图表。

Containers

7. Components

7.组件

The next level of detail in the C4 model is provided by the component view. Creating one is similar to what we’ve done before.

C4模型的下一级细节是由组件视图提供的。创建一个类似于我们之前所做的。

First, we create some components in a container:

首先,我们在一个容器中创建一些组件。

Component jaxrs = jvm1.addComponent("jaxrs-jersey", 
  "restful webservice implementation", "rest");
Component gemfire = jvm1.addComponent("gemfire", 
  "Clustered Cache Gemfire", "cache");
Component hibernate = jvm1.addComponent("hibernate", 
  "Data Access Layer", "jpa");

Next, let’s add some relationships:

接下来,让我们添加一些关系。

jaxrs.uses(gemfire, "");
gemfire.uses(hibernate, "");

Finally, let’s create the view:

最后,我们来创建视图。

ComponentView componentView = workspace.getViews()
  .createComponentView(jvm1, JVM_COMPOSITION, "JVM Components");

componentView.addAllComponents();

A rendition of the resulting diagram via PlantUML results in:

通过PlantUML对所产生的图进行演绎的结果是:。

Components

8. Component Extraction

8.组件的提取

For existing code-bases using the Spring framework, Structurizr provides an automated way of extracting Spring-annotated components and adding them to the architectural artifacts.

对于使用Spring框架的现有代码库,Structurizr提供了一种自动提取Spring注释的组件并将其添加到架构工件的方法。

To utilize this feature, we need to add yet another dependency:

为了利用这一功能,我们需要添加另一个依赖关系。

<dependency>
    <groupId>com.structurizr</groupId>
    <artifactId>structurizr-spring</artifactId>
    <version>1.0.0-RC5</version>
</dependency>

Next, we need to create a ComponentFinder configured with one or more resolution strategies. Resolution strategies affect things such as which components will be added to the model, depth of dependency tree traversal etc.

接下来,我们需要创建一个ComponentFinder,并配置一个或多个解析策略。解析策略会影响一些事情,比如哪些组件会被添加到模型中,依赖树的遍历深度等。

We can even plug in custom resolution strategies:

我们甚至可以插入自定义的解决策略:

ComponentFinder componentFinder = new ComponentFinder(
  jvm, "com.baeldung.structurizr",
  new SpringComponentFinderStrategy(
    new ReferencedTypesSupportingTypesStrategy()
  ),
  new SourceCodeComponentFinderStrategy(new File("/path/to/base"), 150));

Finally, we start the finder:

最后,我们启动查找器。

componentFinder.findComponents();

The code above scans the package com.baeldung.structurizr for Spring-annotated beans and adds them as components to the container JVM. Needless to say, we’re free to implement our own scanners, JAX-RS annotated resources and even Google Guice binders.

上面的代码扫描了包com.baeldung.structurizr中的Spring注释的Bean,并将它们作为组件添加到容器JVM中。不用说,我们可以自由地实现我们自己的扫描器、JAX-RS注解的资源,甚至是Google Guice绑定器。

An example of a simple diagram from a sample project is reproduced below:

下面是一个样本项目的简单图表的例子。

spring

9. Conclusion

9.结论

This quick tutorial covers the basics of the Structurizr for Java project.

这个快速教程涵盖了Structurizr for Java项目的基础知识。

And, as always, example code can be found over on GitHub.

而且,像往常一样,可以在GitHub上找到示例代码