springCloud分散式事務實戰(七)改造合服務BlockMicroService支援分散式事務
阿新 • • 發佈:2018-11-01
在BlockMicroService 工程 中加入
(1)加入jar
<!-- springCloud 事務 關鍵點1 --> <dependency> <groupId>com.codingapi</groupId> <artifactId>transaction-springcloud</artifactId> <version>${lcn.last.version}</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>*</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.codingapi</groupId> <artifactId>tx-plugins-db</artifactId> <version>${lcn.last.version}</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>*</artifactId> </exclusion> </exclusions> </dependency>
(2) 修改配置檔案application.properties
連線 事務伺服器
加上 關鍵點2
tm.manager.url=http://127.0.0.1:7000/tx/manager/
(3)新增2個程式碼檔案
1 http請求服務TxManagerHttpRequestServiceImpl.java package com.jh.service.impl; import com.codingapi.tx.netty.service.TxManagerHttpRequestService; import com.lorne.core.framework.utils.http.HttpUtils; import org.springframework.stereotype.Service; /** * http請求 //關鍵點3 * */ @Service public class TxManagerHttpRequestServiceImpl implements TxManagerHttpRequestService{ @Override public String httpGet(String url) { System.out.println("httpGet-start"); String res = HttpUtils.get(url); System.out.println("httpGet-end"); return res; } @Override public String httpPost(String url, String params) { System.out.println("httpPost-start"); String res = HttpUtils.post(url,params); System.out.println("httpPost-end"); return res; } }
2 獲取url TxManagerTxUrlServiceImpl.java
package com.jh.service.impl; import com.codingapi.tx.config.service.TxManagerTxUrlService; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; /** * 獲取url */ @Service public class TxManagerTxUrlServiceImpl implements TxManagerTxUrlService{ @Value("${tm.manager.url}") private String url; @Override public String getTxUrl() { System.out.println("load tm.manager.url "); return url; } }
(4) 改造整合服務
@TxTransaction(isStart=true) //關鍵 4
@Override
@Transactional
public int saveBlockTheme(Block block, Theme theme) {
int rs1 = blockDao.saveBlock("jwg1", "111");// 3 儲存1
int rs2 = themeClient.saveTheme("jwg2", "111", 1);// 4 儲存2
int v = 100/0; //故障
return rs1 + rs2;
}