springCloud分散式事務實戰(九)改造ThemeMicroService 支援分散式事務
阿新 • • 發佈:2018-11-03
(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) 新增檔案TxManagerTxUrlServiceImpl(關鍵3)
package com.jh.service.impl; import com.codingapi.tx.config.service.TxManagerTxUrlService; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; /** * //關鍵點3: */ @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)服務層函式上加上@Transactional和@TxTransaction/(關鍵4)
@TxTransaction//關鍵點,非常關鍵,否則沒效果
@Transactional
public int saveTheme(String tName, String tDescription, Integer blockId) {
int rs1 = themeDao.saveTheme(tName, tDescription, blockId);// 儲存1
return rs1;
}