前言
最近一直研究springboot,根據工作需求,工作流需要作為一個單獨的微服務工程來提供給其他服務調用,現在簡單的寫下工作流(使用的activiti)微服務的搭建與簡單使用
jdk:1.8
數據庫:mysql 5.7
ide:eclipse
springboot:1.5.8
activiti:6.0.0
1.新建空白的maven微服務架構
新建maven項目的流程不在闡述,這里添加上activiti、myslq連接的依賴,只貼主要代碼
pox.xml
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
|
<project xmlns= "http://maven.apache.org/pom/4.0.0" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation= "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion> 4.0 . 0 </modelversion> <groupid>com.xxx</groupid> <artifactid>xxx</artifactid> <version> 0.0 . 1 -snapshot</version> <properties> <project.build.sourceencoding>utf- 8 </project.build.sourceencoding> <java.version> 1.8 </java.version> </properties> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version> 1.5 . 8 .release</version> </parent> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.activiti</groupid> <artifactid>activiti-spring-boot-starter-basic</artifactid> <version> 6.0 . 0 </version> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-jpa</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-thymeleaf</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <scope>runtime</scope> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-tomcat</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project> |
確認服務是可用
2.連接服務名、服務端口、數據庫配置
在resources目錄下的application.properties(項目定位原因沒有使用yaml,可根據自己項目使用)
1
2
3
4
5
6
7
8
9
10
|
spring.datasource.driver- class -name=com.mysql.jdbc.driver spring.datasource.url=jdbc:mysql: //127.0.0.1:3306/activity?characterencoding=utf8&usessl=true spring.datasource.username=root spring.datasource.password=root spring.jpa.properties.hibernate.hbm2ddl.auto=update spring.jpa.show-sql= true server.port= 8081 server.context-path=/activity server.session.timeout= 10 server.tomcat.uri-encoding=utf- 8 |
確認配置的數據庫可用
3.main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; /** * created by guo on 2017/11/15. */ @springbootapplication public class activityapp { public static void main(string[] args) { springapplication.run(activityapp. class , args); } } |
4.service及實現
service:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
import org.activiti.engine.task.taskquery; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.restcontroller; @restcontroller @requestmapping ( "/activityservice" ) public interface activityconsumerservice { /** * 流程demo * @return */ @requestmapping (value= "/startactivitydemo" ,method=requestmethod.get) public boolean startactivitydemo(); } |
impl
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
|
import java.util.hashmap; import java.util.map; import org.activiti.engine.runtimeservice; import org.activiti.engine.taskservice; import org.activiti.engine.impl.persistence.entity.executionentity; import org.activiti.engine.task.task; import org.activiti.engine.task.taskquery; import org.apache.commons.lang3.stringutils; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service; import com.hongguaninfo.activity.service.activityconsumerservice; @service ( "activityservice" ) public class activityconsumerserviceimpl implements activityconsumerservice { @autowired private runtimeservice runtimeservice; @autowired private taskservice taskservice; @override public boolean startactivitydemo() { system.out.println( "method startactivitydemo begin...." ); map<string,object> map = new hashmap<string,object>(); map.put( "apply" , "zhangsan" ); map.put( "approve" , "lisi" ); //流程啟動 executionentity pi1 = (executionentity) runtimeservice.startprocessinstancebykey( "leave" ,map); string processid = pi1.getid(); string taskid = pi1.gettasks().get( 0 ).getid(); taskservice.complete(taskid, map); //完成第一步申請 task task = taskservice.createtaskquery().processinstanceid(processid).singleresult(); string taskid2 = task.getid(); map.put( "pass" , false ); taskservice.complete(taskid2, map); //駁回申請 system.out.println( "method startactivitydemo end...." ); return false ; } } |
5.bpmn
在resources目錄下新建文件夾:processes,并在processes創建一個新的bpmn文件,如圖
文件內容:
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
|
<?xml version= "1.0" encoding= "utf-8" ?> <definitions xmlns= "http://www.omg.org/spec/bpmn/20100524/model" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xmlns:xsd= "http://www.w3.org/2001/xmlschema" xmlns:activiti= "http://activiti.org/bpmn" xmlns:bpmndi= "http://www.omg.org/spec/bpmn/20100524/di" xmlns:omgdc= "http://www.omg.org/spec/dd/20100524/dc" xmlns:omgdi= "http://www.omg.org/spec/dd/20100524/di" xmlns:dc= "http://www.omg.org/spec/dd/20100524/dc" xmlns:di= "http://www.omg.org/spec/dd/20100524/di" typelanguage= "http://www.w3.org/2001/xmlschema" expressionlanguage= "http://www.w3.org/1999/xpath" targetnamespace= "http://www.activiti.org/testm1510735932336" id= "m1510735932336" name= "" > <process id= "leave" isexecutable= "true" isclosed= "false" processtype= "none" > <startevent id= "_2" name= "startevent" ></startevent> <endevent id= "_3" name= "endevent" ></endevent> <usertask id= "approve" name= "經理審批" activiti:assignee= "${approve}" ></usertask> <exclusivegateway id= "_5" name= "exclusivegateway" ></exclusivegateway> <sequenceflow id= "_6" sourceref= "approve" targetref= "_5" ></sequenceflow> <sequenceflow id= "_7" name= "通過" sourceref= "_5" targetref= "_3" > <conditionexpression xsi:type= "tformalexpression" ><![cdata[${pass}]]></conditionexpression> </sequenceflow> <usertask id= "application" name= "提交申請" activiti:assignee= "${apply}" ></usertask> <sequenceflow id= "_9" sourceref= "_2" targetref= "application" ></sequenceflow> <sequenceflow id= "_10" sourceref= "application" targetref= "approve" ></sequenceflow> <usertask id= "modify" name= "修改申請" activiti:assignee= "${apply}" ></usertask> <sequenceflow id= "_12" name= "不通過" sourceref= "_5" targetref= "modify" > <conditionexpression xsi:type= "tformalexpression" ><![cdata[${!pass}]]></conditionexpression> </sequenceflow> <sequenceflow id= "_13" sourceref= "modify" targetref= "approve" ></sequenceflow> </process> <bpmndi:bpmndiagram id= "bpmndiagram_leave" > <bpmndi:bpmnplane bpmnelement= "leave" id= "bpmnplane_leave" > <bpmndi:bpmnshape bpmnelement= "_2" id= "bpmnshape__2" > <omgdc:bounds height= "35.0" width= "35.0" x= "15.0" y= "60.0" ></omgdc:bounds> </bpmndi:bpmnshape> <bpmndi:bpmnshape bpmnelement= "_3" id= "bpmnshape__3" > <omgdc:bounds height= "35.0" width= "35.0" x= "630.0" y= "63.0" ></omgdc:bounds> </bpmndi:bpmnshape> <bpmndi:bpmnshape bpmnelement= "approve" id= "bpmnshape_approve" > <omgdc:bounds height= "55.0" width= "85.0" x= "315.0" y= "50.0" ></omgdc:bounds> </bpmndi:bpmnshape> <bpmndi:bpmnshape bpmnelement= "_5" id= "bpmnshape__5" > <omgdc:bounds height= "40.0" width= "40.0" x= "505.0" y= "60.0" ></omgdc:bounds> </bpmndi:bpmnshape> <bpmndi:bpmnshape bpmnelement= "application" id= "bpmnshape_application" > <omgdc:bounds height= "55.0" width= "85.0" x= "135.0" y= "50.0" ></omgdc:bounds> </bpmndi:bpmnshape> <bpmndi:bpmnshape bpmnelement= "modify" id= "bpmnshape_modify" > <omgdc:bounds height= "55.0" width= "85.0" x= "315.0" y= "150.0" ></omgdc:bounds> </bpmndi:bpmnshape> <bpmndi:bpmnedge bpmnelement= "_6" id= "bpmnedge__6" > <omgdi:waypoint x= "400.0" y= "77.0" ></omgdi:waypoint> <omgdi:waypoint x= "505.0" y= "80.0" ></omgdi:waypoint> </bpmndi:bpmnedge> <bpmndi:bpmnedge bpmnelement= "_7" id= "bpmnedge__7" > <omgdi:waypoint x= "545.0" y= "80.0" ></omgdi:waypoint> <omgdi:waypoint x= "630.0" y= "80.0" ></omgdi:waypoint> </bpmndi:bpmnedge> <bpmndi:bpmnedge bpmnelement= "_9" id= "bpmnedge__9" > <omgdi:waypoint x= "50.0" y= "77.0" ></omgdi:waypoint> <omgdi:waypoint x= "135.0" y= "77.0" ></omgdi:waypoint> </bpmndi:bpmnedge> <bpmndi:bpmnedge bpmnelement= "_10" id= "bpmnedge__10" > <omgdi:waypoint x= "220.0" y= "77.0" ></omgdi:waypoint> <omgdi:waypoint x= "315.0" y= "77.0" ></omgdi:waypoint> </bpmndi:bpmnedge> <bpmndi:bpmnedge bpmnelement= "_12" id= "bpmnedge__12" > <omgdi:waypoint x= "525.0" y= "100.0" ></omgdi:waypoint> <omgdi:waypoint x= "525.0" y= "177.0" ></omgdi:waypoint> <omgdi:waypoint x= "400.0" y= "177.0" ></omgdi:waypoint> </bpmndi:bpmnedge> <bpmndi:bpmnedge bpmnelement= "_13" id= "bpmnedge__13" > <omgdi:waypoint x= "357.0" y= "150.0" ></omgdi:waypoint> <omgdi:waypoint x= "357.0" y= "105.0" ></omgdi:waypoint> </bpmndi:bpmnedge> </bpmndi:bpmnplane> </bpmndi:bpmndiagram> </definitions> |
需要認知的問題:.項目啟動的時候,activiti會自動在mysql中創建activiti相關表,不用像oracle那樣需要手動去創建
6.驗證
啟動項目前,連接數據庫,查看需要連接數據庫中沒有表,啟動項目完成后,刷新數據庫,activiti已經創建相關表,打開act_re_procdef表,流程數據已經存在,即流程已經部署成功。
用瀏覽器訪問地址:http://127.0.0.1:8081/activity/activityservice/startactivitydemo
打印了預計的日志,沒有報錯信息,查看數據庫中的act_ru_task表,發現剛才執行形成的數據,項目成功。
ps:只是簡單的微服務,沒有去寫注冊服務、網關配置、熔斷機制等等,僅用于activiti與springboot的結合
=========================后續==========================
1.在項目單獨作為一個引擎,本身不部署流程的時候,如果resources目錄沒有“processes”目錄,啟動項目報錯--找不到processes目錄。需要在配置文件中添加一下內容:
1
2
3
4
|
spring: activiti: ####校驗流程文件,默認校驗resources下的processes文件夾里的流程文件 check-process-definitions: false |
即啟動項目,不去校驗processes。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:https://blog.csdn.net/kkkder/article/details/78562349