springCloud分布式事务实战(九)改造ThemeMicroService 支持分布式事务

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

    }

猜你喜欢

转载自blog.51cto.com/14048134/2312468