pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.codingapi.txlcn</groupId>
<artifactId>txlcn-tc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.codingapi.txlcn</groupId>
<artifactId>txlcn-txmsg-netty</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
application.yml
# LCN
tx-lcn:
client:
manager-address: 127.0.0.1:8070
logger:
enabled: false
feign:
hystrix:
enabled: true
LcnClientApplication.java
package com.mshine.cloud;
import com.codingapi.txlcn.tc.config.EnableDistributedTransaction;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
@EnableDistributedTransaction
public class LcnClientApplication {
public static void main(String[] args) {
SpringApplication.run(LcnClientApplication.class, args);
}
}
TestController.java
package com.mshine.cloud.controller;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.mshine.cloud.service.TestAService;
import com.mshine.cloud.service.TestBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
TestAService testAService;
@Autowired
TestBService testBService;
@LcnTransaction //lcn的事务注解
@RequestMapping("/test")
public String test() {
System.out.println(testAService.test());
System.out.println(testBService.test());
return "成功";
}
}
TestAService.java
package com.mshine.cloud.service;
import com.mshine.cloud.service.fallback.TestAServiceHystric;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient(value = "lcn-service-a", fallback = TestAServiceHystric.class)
public interface TestAService {
@RequestMapping(value = "/test")
String test();
}
TestBService.java
package com.mshine.cloud.service;
import com.mshine.cloud.service.fallback.TestBServiceHystric;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient(value = "lcn-service-b", fallback = TestBServiceHystric.class)
public interface TestBService {
@RequestMapping(value = "/test")
String test();
}
TestAServiceHystric.java
package com.mshine.cloud.service.fallback;
import com.codingapi.txlcn.tc.support.DTXUserControls;
import com.codingapi.txlcn.tracing.TracingContext;
import com.mshine.cloud.service.TestAService;
import org.springframework.stereotype.Component;
@Component
public class TestAServiceHystric implements TestAService {
@Override
public String test() {
DTXUserControls.rollbackGroup(TracingContext.tracing().groupId());
return "失败";
}
}
TestBServiceHystric.java
package com.mshine.cloud.service.fallback;
import com.codingapi.txlcn.tc.support.DTXUserControls;
import com.codingapi.txlcn.tracing.TracingContext;
import com.mshine.cloud.service.TestBService;
import org.springframework.stereotype.Component;
@Component
public class TestBServiceHystric implements TestBService {
@Override
public String test() {
DTXUserControls.rollbackGroup(TracingContext.tracing().groupId());
return "失败";
}
}
pom.xml
<dependency>
<groupId>com.mingshine</groupId>
<artifactId>cloud-mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.codingapi.txlcn</groupId>
<artifactId>txlcn-tc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.codingapi.txlcn</groupId>
<artifactId>txlcn-txmsg-netty</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
application.yml
---
# LCN
tx-lcn:
client:
manager-address: 127.0.0.1:8070
logger:
enabled: false
LcnServiceAApplication.java
package com.mshine.cloud;
import com.codingapi.txlcn.tc.config.EnableDistributedTransaction;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
@MapperScan("com.mshine.cloud.mybatis.mapper") //扫描的mapper
@EnableDistributedTransaction
public class LcnServiceAApplication {
public static void main(String[] args) {
SpringApplication.run(LcnServiceAApplication.class, args);
}
}
TestController.java
package com.mshine.cloud.controller;
import com.alibaba.fastjson.JSON;
import com.codingapi.txlcn.tc.annotation.DTXPropagation;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.mshine.cloud.mybatis.entity.TbUser;
import com.mshine.cloud.mybatis.mapper.TbUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
TbUserMapper tbUserMapper;
@RequestMapping("/test")
@LcnTransaction(propagation = DTXPropagation.SUPPORTS)
@Transactional
public String test() {
TbUser tbUser = new TbUser();
tbUser.setName("AAAAAAA" + System.currentTimeMillis());
tbUserMapper.insert(tbUser);
return JSON.toJSONString(tbUser);
}
}
pom.xml
<dependency>
<groupId>com.mingshine</groupId>
<artifactId>cloud-mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.codingapi.txlcn</groupId>
<artifactId>txlcn-tc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.codingapi.txlcn</groupId>
<artifactId>txlcn-txmsg-netty</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
application.yml
---
# LCN
tx-lcn:
client:
manager-address: 127.0.0.1:8070
logger:
enabled: false
LcnServiceBApplication.java
package com.mshine.cloud;
import com.codingapi.txlcn.tc.config.EnableDistributedTransaction;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
@MapperScan("com.mshine.cloud.mybatis.mapper") //扫描的mapper
@EnableDistributedTransaction
public class LcnServiceBApplication {
public static void main(String[] args) {
SpringApplication.run(LcnServiceBApplication.class, args);
}
}
TestController.java
package com.mshine.cloud.controller;
import com.alibaba.fastjson.JSON;
import com.codingapi.txlcn.tc.annotation.DTXPropagation;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.codingapi.txlcn.tc.config.EnableDistributedTransaction;
import com.mshine.cloud.mybatis.entity.TbUser;
import com.mshine.cloud.mybatis.mapper.TbUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableDistributedTransaction
public class TestController {
@Autowired
TbUserMapper tbUserMapper;
@RequestMapping("/test")
@LcnTransaction(propagation = DTXPropagation.SUPPORTS)
@Transactional
public String test() {
TbUser tbUser = new TbUser();
tbUser.setName("BBBBBBB" + System.currentTimeMillis());
tbUserMapper.insert(tbUser);
int i = 1 / 0;
return JSON.toJSONString(tbUser);
}
}
备注:事物发起方使用REQUIRED,事物参与方使用SUPPORTS