为了便捷地使用PlantUML,许多流行的IDE和代码编辑器提供了集成PlantUML的插件,如Visual Studio Code、IntelliJ IDEA、Eclipse等。插件提供了实时预览、语法高亮和图表导出等功能,能帮助我们更快捷,更高效地画图,整体上IDEA的插件用起来体验最好,但是IDEA大家懂的,太占内存了,VS Code相对而言,用起来就会轻量很多。
IntelliJ IDEA:比如 "PlantUML integration" 可以让我们直接在 IDE 中查看和编辑 PlantUML 图表
PlantUML integration
插件使用效果
VS Code:https://marketplace.visualstudio.com/items?itemName=jebbs.plantuml
@startuml participant Participant as Foo 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 as Foo7 @enduml
参与者
默认的颜色比较单调,也可以通过#来设置参与者的颜色:
@startuml actor Bob #blue ' The only difference between actor 'and participant is the drawing participant Alice #SkyBlue participant "I have a really\nlong name" as L #00ff00 Alice->Bob: Authentication Request Bob->Alice: Authentication Response Bob->L: Log transaction @enduml
@startuml participant User User -> A: DoWork activate A A -> B: << createRequest >> activate B B -> C: DoWork activate C C --> B: WorkDone destroy C B --> A: RequestCreated deactivate B A -> User: Done deactivate A @enduml
生命线的激活与撤销
生命线的嵌套与颜色:我们还可以使用嵌套激活条来表示内部调用,并可以给生命线添加颜色。
@startuml participant User User -> A: DoWork activate A #FFBBBB A -> A: Internal call activate A #DarkSalmon A -> B: << createRequest >> activate B B --> A: RequestCreated deactivate B deactivate A A -> User: Done deactivate A @enduml
可以用note left of,note right of或note over来控制注释相对节点的位置,还可以通过修改背景色来高亮显示注释。
@startuml participant Alice participant Bob note left of Alice #aqua This is displayed left of Alice. end note note right of Alice: This is displayed right of Alice. note over Alice: This is displayed over Alice. note over Alice, Bob #FFAAAA: This is displayed\n over Bob and Alice. note over Bob, Alice This is yet another example of a long note. end note @enduml
@startuml skinparam ParticipantBackgroundColor #DeepSkyBlue actor 用户 as c #DeepSkyBlue participant "客户端" as client participant "服务网关" as ga participant "用户服务" as user database "数据库" as DB #DeepSkyBlue participant "Google服务" as google #LightCoral activate c #DeepSkyBlue activate client #DeepSkyBlue c->client:用户登录 group#LightCoral #LightCoral Google登录客户端流程 client -> google : 请求Google OAuth登录 activate google #DeepSkyBlue google-->client:登录url client->google:跳转登录页 google -> google : 用户登录 google --> client : Google登录Token deactivate google end ||| client -> ga : 登录请求 note right#LightCoral:新增登录方式,三方登录请求实体 activate ga #DeepSkyBlue ga ->user:请求转发 activate user #DeepSkyBlue alt#DeepSkyBlue 常规登录 user -> DB : 查询用户信息 activate DB #DeepSkyBlue DB -> user : 用户信息 deactivate DB user->user:登录密码校验 ||| else Google登录 group#LightCoral #LightCoral Google登录服务端流程 user->google:验证token activate google #DeepSkyBlue google-->user:用户信息 deactivate google user->user:存储或更新用户信息 end group end user-->ga:登录结果 deactivate user ga -> client : 响应 deactivate ga alt#DeepSkyBlue 成功 client -> c : 登录成功 else 失败 client -> c : 登录失败 end deactivate client ||| @enduml