0%

plantUML时序图

对于一些时序性的工作,你可以使用plantuml来构建简单时序关系,它势必会提升你的工作效率。

1
2
3
4
5
6
@startuml
client -> server : request
server --> proxy : request
server <- proxy : response
server -> client: response
@enduml

建立两个参与者,你可以使用简单的-> ,<- ,–> ,<–等符号来实现参与者之间的消息传递。

安装

下载

https://plantuml.com/zh/download

不建议以命令方式运行plantuml,尽量将plantuml集成到你的IDE中

安装 Graphviz

1
brew install Graphviz

安装 libtool

1
brew install libtool

声明参与者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@startuml
'https://plantuml.com/sequence-diagram

autonumber
'第一个参与者
participant participant as Foo #red
'角色
actor actor as Foo1
'边界
boundary boundary as Foo2
'控制
control control as Foo3
'实体
entity entity as Foo4
'数据库
database database as Foo5
'集合
collections collections as Foo6
'队列
queue Queue

Foo -> Foo1 : To actor
Foo -> Foo2 : To boundary
Foo -> Foo3 : To control
Foo -> Foo4 : To entity
Foo -> Foo5 : To database
Foo -> Foo6 : To collections
Foo -> Queue : To queue
Foo5 <- Queue : back database

@enduml
  • participant 参与者
  • actor 角色
  • boundary 边界
  • control 控制
  • entity 实体
  • database 数据库
  • collections 集合
  • queue 队列

样式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@startuml
'https://plantuml.com/sequence-diagram
' 5和7对齐
skinparam responseMessageBelowArrow true
autonumber
participant participant as Foo #ff00ff
actor actor as Foo1
participant Foo4 order 30 #00ff00
participant Foo3 order 20
participant Foo2 order 10

Foo -> Foo1 : To actor
Foo -> Foo2 : To boundary
Foo2 -> Foo3: to foo3
Foo2--> Foo3: again to foo3
Foo3--> Foo4: to foo4
Foo -> Foo: loop
Foo3 <- Foo4: back Foo3

@enduml

  • 你还可以使用RGB值或者颜色名修改 actor 或参与者的背景颜色
  • 您可以使用关键字 order自定义顺序来打印参与者
  • 你还可以使用–> 表示虚线 4
  • 你还可以给自己发消息 6
  • 你可以使用skinparam responseMessageBelowArrow true命令,让响应信息显示在箭头下面。

修改箭头样式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@startuml
'https://plantuml.com/sequence-diagram
autonumber

Bob ->x Alice: hello
Alice -> June
June ->> Leo
Leo -[#00ffff]\ Eric
Leo \\[#ff0000]- Eric
June //-[#ff00ff]- Leo
June ->o Leo
June o\\-- Leo
June <-> Alice
Bob <->o Alice

@enduml

消息序列编号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@startuml
'https://plantuml.com/sequence-diagram
autonumber 3
Bob -> Alice : Auth request
Bob <- Alice : Auth request

autonumber
Alice -> Eric : Auth request
Alice -> Eric : Auth request

autonumber stop
Eric -> John : Auth request

autonumber "<b>[000]"

Bob -> Alice : Auth request
Bob <- Alice : Auth request

autonumber 15 "<b>(<u>##</u>)"
Alice -> Eric : Auth request
Alice -> Eric : Auth request
@enduml

页面标题,页眉,页脚

1
2
3
4
5
6
7
8
9
@startuml
'https://plantuml.com/sequence-diagram
header Page Header
footer Page %page% of %lastpage%
title Example Title

Alice -> Bob : message 1
Alice -> Bob : message 2
@enduml

分割示意图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@startuml
'https://plantuml.com/sequence-diagram
header Page Header
footer Page %page% of %lastpage%

title Example Title
Alice -> Bob : message 1
Alice -> Bob : message 2

newpage

header Page Header
footer Page %page% of %lastpage%

Alice -> Bob : message 3
Alice -> Bob : message 4

newpage A title for the\nlast page

Alice -> Bob : message 5
Alice -> Bob : message 6

@enduml

组合消息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@startuml
'https://plantuml.com/sequence-diagram
Alice -> Bob: 认证请求

alt 成功情况
Bob -> Alice: 认证接受
else 某种失败情况
Bob -> Alice: 认证失败
group 我自己的标签
Alice -> Log : 开始记录攻击日志
loop 1000次数
Alice -> Bob: DNS 攻击
end
end
else 另一种失败
Bob -> Alice: 请重复
end

@enduml
  • alt/else
  • opt
  • loop
  • par
  • break
  • critical
  • group, 后面紧跟着消息内容

消息注释

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@startuml
'https://plantuml.com/sequence-diagram
Alice -> Bob: 认证请求
note left #bbbbbb : this is a first note

Bob -> Alice: 认证失败
note right : this is a second note

Bob -> Bob
note left #ff00ff
loop
a
b
c
end note
@enduml

改变备注框形状

你可以使用 hnote 和 rnote 这两个关键字来修改备注框的形状:

  • hnote代表六边形(hexagonal)的备注框;
  • rnote代表正方形(rectangle)的备注框。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@startuml
caller -> server : conReq
hnote over caller : 空闲
caller <- server : conConf
rnote over server
"r"是正方形
"h"是六边形
endrnote
rnote over server


文本
endrnote

hnote over server,caller
跨越多行
endhnote
server -> proxy
note across :跨越所有参与者

@enduml

在同一级对齐多个备注 [/]

1
2
3
4
5
6
@startuml
caller -> server : conReq
note over caller : call init
/ note over server: call init
caller <- server : conConf
@enduml

Creole和HTML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@startuml
participant Alice
participant "The **Famous** Bob" as Bob

Alice -> Bob : hello --there--
... Some ~~long delay~~ ...
Bob -> Alice : ok
note left
This is **bold**
This is //italics//
This is ""monospaced""
This is --stroked--
This is __underlined__
This is ~~waved~~
end note

Alice -> Bob : A //well formatted// message
note right of Alice
This is <back:cadetblue><size:18>displayed</size></back>
__left of__ Alice.
end note
note left of Bob
<u:red>This</u> is <color #118888>displayed</color>
**<color purple>left of</color> <s:red>Alice</strike> Bob**.
end note
note over Alice, Bob
<w:#FF33FF>This is hosted</w> by <img sourceforge.jpg>
end note
@enduml

分隔符

1
2
3
4
5
6
7
@startuml
== request ==
participant 我是参与者A as A
A -> B : request
== response ==
A <- B : response
@enduml

引用

1
2
3
4
5
6
7
8
9
10
11
12
@startuml
participant Alice
actor Bob

ref over Alice,Bob:init
Alice -> Bob : hello

ref over Bob
hi
i am bob
end ref
@enduml

延迟

1
2
3
4
5
6
7
8
9
10
@startuml
A -> B
...

B -> A

... 延迟...

A -> B
@enduml

文本换行

1
2
3
4
5
6
@startuml
skinparam maxMessageSize 50
A -> B : r\ne\nq\nu\ne\ns\nt

A <- B : reponse a b c d e
@enduml

空间

1
2
3
4
5
6
7
8
9
@startuml
A -> B: request
A -> B: request
A -> B: request
|||
A -> B: request
||100||
A -> B: request
@enduml

生命线的激活与撤销

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@startuml
A -> B: request
activate B #ff00ff
B -> B: loop

activate B
B -> C: request
deactivate B

activate C
C -> D: request
activate D

C <- D: response
destroy D
B <- C: response
deactivate C
A <- B: response
deactivate B
@enduml


1
2
3
4
5
6
7
8
9
10
11
12
@startuml
autoactivate on
alice -> bob : hello
bob -> bob : self call
bill -> bob #005500 : hello from thread 2
bob -> george ** : create
return done in thread 2
return rc
bob -> george !! : delete
return success

@enduml

返回

新命令return可以用于生成一个带有可选文本标签的返回信息。返回的点是导致最近一次激活生命线的点。语法是简单的返回标签,其中标签(如果提供)可以是传统信息中可以接受的任何字符串。

1
2
3
4
5
6
@startuml
A -> B : request
activate B
B -> B: loop
return bye
@enduml

创建参与者

1
2
3
4
5
6
7
8
9
10
11
12
13
@startuml
Bob -> Alice : hello

create Other
Alice -> Other : new

create control String
Alice -> String
note right : You can also put notes!

Alice --> Bob : ok

@enduml

激活、撤销和创建的快捷语法

在指定目标参与者后,可以立即使用以下语法:

  • ++ 激活目标(可选择在后面加上#color)
  • – 撤销激活源
  • ** 创建目标实例
  • !! 摧毁目标实例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@startuml
A -> B ++ : request
B -> C ++ #ff00ff : request

C -> D ** : new instance

C --> C -- : loop

C -> D !! : delete

C -> E ++ : request

E -> F --++ : request

F -> C : response

B <- C : response

return response

@enduml

进入和发出消息

1
2
3
4
5
6
7
8
9
10
@startuml
[-> A: request
A++
A->A: loop
A++
A ->]: leave
A<--]: back
A--
A->[ -- : leave
@enduml

锚定和持续时间

使用teoz在图表中添加锚定,从而指定持续时间。

1
2
3
4
5
6
7
8
9
10
11
@startuml
!pragma teoz true

{start} Alice -> Bob : start doing things during duration
Bob -> Max : something
Max -> Bob : something else
{end} Bob -> Alice : finish

{start} <-> {end} : some time

@enduml

构造类型和圈点

可以使用<<和>>给参与者添加构造类型。

在构造类型中,你可以使用(X,color)格式的语法添加一个圆圈圈起来的字符。

1
2
3
4
5
@startuml
participant JuneLeo as A <<(A,#ff00ff)>>
participant Eric <<(B,#ff0000)>>
A -> Eric : request
@enduml

包裹参与者

1
2
3
4
5
6
7
8
9
10
@startuml
box "internal service" #dddddd
participant Bob
participant Alice
end box
participant Eric

Bob -> Alice : request
Alice -> Eric : request
@enduml

移除脚注

1
2
3
4
5
6
7
@startuml

A -> B : request
newpage
hide footbox
A -> B : request
@enduml

外观参数(skinparam)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
@startuml
'箭头线粗细
skinparam sequenceArrowThickness 4
'参与者 框圆角
skinparam roundcorner 10
'箭头上文案限制
skinparam maxmessagesize 10
'参与者文案下划线
skinparam sequenceParticipant underline
'参与者之间间距
skinparam ParticipantPadding 30
'box padding
skinparam BoxPadding 20
'background
skinparam backgroundColor #00ff00
'手画效果
'skinparam handwritten true
'

skinparam sequence {
'箭头颜色
ArrowColor DeepSkyBlue
'actor border 颜色
ActorBorderColor DeepSkyBlue
'参与者生命条 border颜色
LifeLineBorderColor blue
'参与者生命条背景颜色
LifeLineBackgroundColor #A9DCDF
'参与者border颜色
ParticipantBorderColor DeepSkyBlue
'参与者背景颜色
ParticipantBackgroundColor DodgerBlue
'参与字体
ParticipantFontName Impact
'参与者字体大小
ParticipantFontSize 17
'参与者字体颜色
ParticipantFontColor #A9DCDF

ActorBackgroundColor aqua
ActorFontColor DeepSkyBlue
ActorFontSize 17
ActorFontName Aapex
}

actor User
box
participant "First Class" as A
end box
participant "Second Class" as B
participant "Last Class" as C

User -> A: DoWork
activate A

A -> B: Create Request
activate B

B -> C: DoWork
activate C
C --> B: WorkDone
destroy C

B --> A: Request Created
deactivate B

A --> User: Done
deactivate A

@enduml


箭头类型大全

普通箭头

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@startuml
participant Alice as a
participant Bob as b
a -> b : ""-> ""
a ->> b : ""->> ""
a -\ b : ""-\ ""
a -\\ b : ""-\\\\""
a -/ b : ""-/ ""
a -// b : ""-// ""
a ->x b : ""->x ""
a x-> b : ""x-> ""
a o-> b : ""o-> ""
a ->o b : ""->o ""
a o->o b : ""o->o ""
a <-> b : ""<-> ""
a o<->o b : ""o<->o""
a x<->x b : ""x<->x""
a ->>o b : ""->>o ""
a -\o b : ""-\o ""
a -\\o b : ""-\\\\o""
a -/o b : ""-/o ""
a -//o b : ""-//o ""
a x->o b : ""x->o ""
@enduml

进入信息(使用’[‘)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@startuml
participant Alice as a
participant Bob as b
[-> b : ""[-> ""
[->> b : ""[->> ""
[-\ b : ""[-\ ""
[-\\ b : ""[-\\\\""
[-/ b : ""[-/ ""
[-// b : ""[-// ""
[->x b : ""[->x ""
[x-> b : ""[x-> ""
[o-> b : ""[o-> ""
[->o b : ""[->o ""
[o->o b : ""[o->o ""
[<-> b : ""[<-> ""
[o<->o b : ""[o<->o""
[x<->x b : ""[x<->x""
[->>o b : ""[->>o ""
[-\o b : ""[-\o ""
[-\\o b : ""[-\\\\o""
[-/o b : ""[-/o ""
[-//o b : ""[-//o ""
[x->o b : ""[x->o ""
@enduml

发出信息(使用’]’)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@startuml
participant Alice as a
participant Bob as b
a ->] : ""->] ""
a ->>] : ""->>] ""
a -\] : ""-\] ""
a -\\] : ""-\\\\]""
a -/] : ""-/] ""
a -//] : ""-//] ""
a ->x] : ""->x] ""
a x->] : ""x->] ""
a o->] : ""o->] ""
a ->o] : ""->o] ""
a o->o] : ""o->o] ""
a <->] : ""<->] ""
a o<->o] : ""o<->o]""
a x<->x] : ""x<->x]""
a ->>o] : ""->>o] ""
a -\o] : ""-\o] ""
a -\\o] : ""-\\\\o]""
a -/o] : ""-/o] ""
a -//o] : ""-//o] ""
a x->o] : ""x->o] ""
@enduml

短进入信息(使用’?’)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@startuml
participant Alice as a
participant Bob as b
a -> b : //Long long label//
?-> b : ""?-> ""
?->> b : ""?->> ""
?-\ b : ""?-\ ""
?-\\ b : ""?-\\\\""
?-/ b : ""?-/ ""
?-// b : ""?-// ""
?->x b : ""?->x ""
?x-> b : ""?x-> ""
?o-> b : ""?o-> ""
?->o b : ""?->o ""
?o->o b : ""?o->o ""
?<-> b : ""?<-> ""
?o<->o b : ""?o<->o""
?x<->x b : ""?x<->x""
?->>o b : ""?->>o ""
?-\o b : ""?-\o ""
?-\\o b : ""?-\\\\o ""
?-/o b : ""?-/o ""
?-//o b : ""?-//o ""
?x->o b : ""?x->o ""
@enduml

短发出信息(使用’?’)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@startuml
participant Alice as a
participant Bob as b
a -> b : //Long long label//
a ->? : ""->? ""
a ->>? : ""->>? ""
a -\? : ""-\? ""
a -\\? : ""-\\\\?""
a -/? : ""-/? ""
a -//? : ""-//? ""
a ->x? : ""->x? ""
a x->? : ""x->? ""
a o->? : ""o->? ""
a ->o? : ""->o? ""
a o->o? : ""o->o? ""
a <->? : ""<->? ""
a o<->o? : ""o<->o?""
a x<->x? : ""x<->x?""
a ->>o? : ""->>o? ""
a -\o? : ""-\o? ""
a -\\o? : ""-\\\\o?""
a -/o? : ""-/o? ""
a -//o? : ""-//o? ""
a x->o? : ""x->o? ""
@enduml

特定外观参数

1
2
3
4
5
6
7
8
'生命线 虚线和实线
skinparam lifelineStrategy nosolid
skinparam lifelineStrategy solid
'为了符合严格UML的标准(线头的形状必须是三角形,而不能是箭头形)
skinparam style strictuml
' 隐藏没有链接到的参与者
hide unlinked