12.13 springboot2.0集成lcn5.0.2分佈式事務

springboot2.0集成lcn5.0.2分佈式事務


springboot2.0集成lcn5.0.2分佈式事務


1、下載lcn源碼,並導入eclipse中,記住選擇導入為maven項目

git clone [email protected]:codingapi/tx-lcn.git

2、修改application.properties配置文件

spring.application.name=TransactionManagerserver.port=7970spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://192.168.1.81:3306/tx-manager?characterEncoding=UTF-8spring.datasource.username=rootspring.datasource.password=rootspring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect#第一次運行項目,初始化用create創建表,以後用none或者updatespring.jpa.hibernate.ddl-auto=none #TxManager Ip,默認為127.0.0.1tx-lcn.manager.host=127.0.0.1#TM監聽scoket端口,默認為8070tx-lcn.manager.port=8070#TM後臺登錄密碼tx-lcn.manager.admin-key=123456 #心跳檢測時間(ms)tx-lcn.manager.heart-time=15000#分佈式事務執行總時間(ms)tx-lcn.manager.dtx-time=30000#參數延遲刪除時間mstx-lcn.message.netty.attr-delay-time=10000#事務處理併發等級,默認為機器核心數5倍tx-lcn.manager.concurrent-level=150 #redis地址spring.redis.host=192.168.1.81#redis端口spring.redis.port=6379#redis密碼#spring.redis.password=123456 ## redis集群配置#spring.redis.cluster.nodes=192.168.1.81:7000,192.168.1.81:7001,192.168.1.81:7002## 連接超時時間(毫秒) #spring.redis.timeout=60000## 連接池最大連接數(使用負值表示沒有限制) #spring.redis.pool.max-active=300## 連接池最大阻塞等待時間(使用負值表示沒有限制) #spring.redis.pool.max-wait=-1## 連接池中的最大空閒連接 #spring.redis.pool.max-idle=100## 連接池中的最小空閒連接 #spring.redis.pool.min-idle=20 #是否啟用日誌#tx-lcn.logger.enabled=true#設置日誌級別#logging.level.com.codingapi=debug # 雪花算法的sequence位長度,默認為12位tx-lcn.manager.seq-len=12 # 異常回調開關。開啟時請制定ex-urltx-lcn.manager.ex-url-enabled=false # 事務異常通知(任何http協議地址。未指定協議時,為TM提供內置功能接口)。默認是郵件通知tx-lcn.manager.ex-url=/provider/email-to/***@**.com 

3、修改完配置文件,使用啟動類啟動LCN事務管理器

springboot2.0集成lcn5.0.2分佈式事務

4、LCN的TransactionManager服務端,訪問地址 http://127.0.0.1:7970/admin/index.html

springboot2.0集成lcn5.0.2分佈式事務

5、springboot2.0 微服務項目,客戶端集成LCN

第一步:pom.xml引入依賴

<dependency>            <groupid>com.codingapi.txlcn/<groupid>            <artifactid>txlcn-tc/<artifactid>            <version>5.0.2.RELEASE/<version>        /<dependency>        <dependency>            <groupid>com.codingapi.txlcn/<groupid>            <artifactid>txlcn-txmsg-netty/<artifactid>            <version>5.0.2.RELEASE/<version>        /<dependency>

第二步:所有需要使用LCN分佈式事務的微服務項目,無論是調用方,還是被調用方的啟動類都需要加上@EnableDistributedTransaction註解,否則分佈式事務不生效

springboot2.0集成lcn5.0.2分佈式事務

springboot2.0集成lcn5.0.2分佈式事務

第三步:所有需要使用LCN分佈式事務的微服務項目,application.yml配置文件都要引入如下配置

#lcn分佈式事務   tx-lcn:  client:    manager-address: 127.0.0.1:8070 

第四步:會員微服務,調用方法開啟分佈式事務

package com.it.member.service.impl; import org.apache.commons.lang.StringUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.transaction.annotation.Transactional;import org.springframework.web.bind.annotation.RestController; import com.alibaba.fastjson.JSONObject;import com.codingapi.txlcn.tc.annotation.LcnTransaction;import com.it.base.BaseApiService;import com.it.base.BaseResponse;import com.it.member.TestLcnService;import com.it.member.entity.TestLcn;import com.it.member.feign.PayTestLcnServiceFeign;import com.it.member.mapper.TestLcnMapper; /** *  * @ClassName: TestLcnServiceImpl  * @description: 測試LCN分佈式事務集成 * @author 互聯網架構師-liuxk   * @date 2019年5月23日 下午2:34:14  * @version V1.0 */@RestControllerpublic class TestLcnServiceImpl extends BaseApiService<jsonobject> implements TestLcnService{\t\t@Autowired\tprivate TestLcnMapper testLcnMapper;\t@Autowired\tprivate PayTestLcnServiceFeign payTestLcnServiceFeign; \t@Override    @LcnTransaction //分佈式事務註解    @Transactional  //本地事務註解\tpublic BaseResponse<jsonobject> testlcn(String name) {\t\tif(StringUtils.isEmpty(name)) {\t\t\treturn setResultError("name不能為空!!");\t\t}\t\tTestLcn testLcn=new TestLcn();\t\ttestLcn.setName(name);\t\tint result=testLcnMapper.insert(name);\t\tif(!toDaoResult(result)) {\t\t\treturn setResultError("Member項目保存失敗!!");\t\t}\t\tBaseResponse<jsonobject> payResp=payTestLcnServiceFeign.saveTestLcn(name);//\t\tint i=1/0; //設置異常,看看分佈式事務是否生效\t\tif(payResp.getCode()!=200) {\t\t\treturn setResultError(payResp.getMsg());\t\t}\t\treturn setResultSuccess();\t} }/<jsonobject>/<jsonobject>/<jsonobject>

第五步:支付微服務,被調用方法開啟分佈式事務

package com.it.pay.service.impl; import org.apache.commons.lang.StringUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.transaction.annotation.Transactional;import org.springframework.web.bind.annotation.RestController; import com.alibaba.fastjson.JSONObject;import com.codingapi.txlcn.tc.annotation.LcnTransaction;import com.it.base.BaseApiService;import com.it.base.BaseResponse;import com.it.pay.mapper.TestLcnMapper;import com.it.pay.service.PayTestLcnService; /** *  * @ClassName: TestLcnServiceImpl  * @description: 測試LCN分佈式事務集成 * @author 互聯網架構師-liuxk   * @date 2019年5月23日 下午2:34:14  * @version V1.0 */@RestControllerpublic class PayTestLcnServiceImpl extends BaseApiService<jsonobject> implements PayTestLcnService{\t\t@Autowired\tprivate TestLcnMapper testLcnMapper; \t@Override    @LcnTransaction //分佈式事務註解    @Transactional  //本地事務註解\tpublic BaseResponse<jsonobject> saveTestLcn(String name) {\t\tif(StringUtils.isEmpty(name)) {\t\t\treturn setResultError("name不能為空!!");\t\t}\t\tint result=testLcnMapper.insert(name);\t\tif(!toDaoResult(result)) {\t\t\treturn setResultError("Pay項目保存失敗!!");\t\t}\t\treturn setResultSuccess();\t} }/<jsonobject>/<jsonobject>

特別注意:無論是調用方,還是被調用方,都需要設置開啟分佈式事務,如果有任何一方不開啟,則分佈式事務不生效

第六步:兩個不同的數據庫,分別創建測試表

CREATE TABLE `test_lcn` (  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主鍵ID',  `name` varchar(80) CHARACTER SET utf8 DEFAULT NULL COMMENT '姓名',  `create_time` datetime DEFAULT NULL COMMENT '創建時間',  PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

第七步:執行調用測試,兩個數據都不保存,LCN分佈式事務配置成功


分享到:


相關文章: