1. 程式人生 > >springCloud分布式事務實戰(九)改造ThemeMicroService 支持分布式事務

springCloud分布式事務實戰(九)改造ThemeMicroService 支持分布式事務

actor dep per clas pri transacti artifact eth return

(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;

    }

springCloud分布式事務實戰(九)改造ThemeMicroService 支持分布式事務