SpringCloud Alibaba Seata处理分布式事务及示例Demo

创建undo_log表,此表为seata框架使用:

CREATE TABLE `undo_log` (
 `id` bigint(20) NOT NULL AUTO_INCREMENT,
 `branch_id` bigint(20) NOT NULL, `xid` varchar(100) NOT NULL,
 `context` varchar(128) NOT NULL, `rollback_info` longblob NOT NULL,
 `log_status` int(11) NOT NULL,
 `log_created` datetime NOT NULL,
 `log_modified` datetime NOT NULL,
 `ext` varchar(100) DEFAULT NULL,
 PRIMARY KEY (`id`),
 UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
 ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

2.启动TC(事务协调器)

下载seata服务器
下载地址: https://github.com/seata/seata/releases/download/v0.7.1/seata-server-0.7.1.zip
解压并启动
seata服务端解压路径: /bin/seata-server.bat
SpringCloud Alibaba Seata处理分布式事务及示例Demo_第1张图片

 如上图出现“Server started...”的字样则表示启动成功。

3.修改 registry.conf 配置文件(并添加到resources目录下)

registry {
  type = "nacos"
  nacos {
    serverAddr = "localhost"
    namespace = "public"
    cluster = "default"
  }
}

config {
  type = "nacos"
  nacos {
    serverAddr = "localhost"
    namespace = "public"
    cluster = "default"
  }
}

4.修改nacos-config.txt文件(改为服务名)

SpringCloud Alibaba Seata处理分布式事务及示例Demo_第2张图片

 5.在seata/config目录下在运行

sh nacos-config.sh 127.0.0.1

打开nacos查看配置文件是否导入

6.创建项目

导入依赖

    
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-seata
            2.1.1.RELEASE
        

        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-config
        
        
            mysql
            mysql-connector-java
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            org.springframework.boot
            spring-boot-starter-web
        
    

order controller

@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/save")
    @GlobalTransactional
    public String save(){
        //订单
        this.orderService.save();

        //支付
        this.restTemplate.getForObject("http://localhost:8020/save",String.class);
        int i = 10/0;
        return "success";
    }
}

service

@Service
public class OrderService {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void save(){
        this.jdbcTemplate.update("INSERT INTO `order`(`name`) VALUES ('张三')");
    }
}

application

@SpringBootApplication
@EnableDiscoveryClient
public class OrderApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource){
        return new JdbcTemplate(new DataSourceProxy(dataSource));
    }
}

yml

  application:
    name: order
  cloud:
    alibaba:
      seata:
        tx-service-group: ${spring.application.name}

pay controller

@RestController
public class PayController {

    @Autowired
    private PayService orderService;

    @GetMapping("/save")
    public String save(){
        //订单
        this.orderService.save();
        return "success";
    }
}

service

@Service
public class PayService {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void save(){
        this.jdbcTemplate.update("INSERT INTO `pay`(`name`) VALUES ('张三')");
    }
}

在两个服务中注入数据源

@Configuration
public class DataSourceProxyConfig {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {
        return new DruidDataSource();
    }

    @Bean
    public DataSourceProxy dataSourceProxy(DataSource dataSource) {
        return new DataSourceProxy(dataSource);
    }

    @Bean
    public SqlSessionFactory sqlSessionFactoryBean(DataSourceProxy dataSourceProxy) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSourceProxy);
        return sqlSessionFactoryBean.getObject();
    }
}

你可能感兴趣的:(详细代码,spring,cloud,分布式,java)