微服務分佈式事務實戰(九)改造ThemeMicroService 支持分佈式事務

(1) 添加jar

     <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

tm.manager.url=http://127.0.0.1:7000/tx/manager/

(3) 添加文件TxManagerTxUrlServiceImpl

package com.jh.service.impl;

import com.codingapi.tx.config.service.TxManagerTxUrlService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


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

    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章