分布式测试版4:seata搭建测试

1:官网教程
https://github.com/seata/seata-samples/tree/master/springcloud-nacos-seata

2:新建一个数据库
新建库,并执行seata/conf 中 的两个sql文件
db_undo_log.sql
db_store.sql
其中 db_undo_log.sql 每个业务库都需要执行

3:seata搭建补充
引入包:

>
    >com.alibaba.cloud>
    >spring-cloud-alibaba-seata>
    >2.1.0.RELEASE>
>

-----数据库依赖

  >
            >com.alibaba>
            >druid-spring-boot-starter>
            >1.1.10>
        >

        >
            >mysql>
            >mysql-connector-java>
            >5.1.39>
        >

        >
            >com.baomidou>
            >mybatis-plus-boot-starter>
            >3.1.1>
        >

        >
            >io.seata>
            >seata-all>
            >0.9.0>
        >

在git中运行(此处不运行)改用读取配置文件 file.conf(修改成自己的nacos, 和服务名)
file方式:以下操作不执行。
(sh nacos-config.sh 127.0.0.1
sh seata-server.sh -p 8091 -m file)

配置yml

server:
  port: 8082
spring:
  application:
    name: provider
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
    alibaba:
      seata:
        tx-service-group: provider_tx_group

在启动类添加:
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)

新增数据库连接池配置:配置类MyBatisConfig
package com.archer.consumer.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import io.seata.rm.datasource.DataSourceProxy;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import javax.sql.DataSource;

/**
 * Program Name: base-framework-mysql-support
 * 

* Description: MyBatisConfig *

* * @author zhangjianwei * @version 1.0 * @date 2019/6/12 5:17 PM */ @Configuration public class MyBatisConfig { /** * @param sqlSessionFactory SqlSessionFactory * @return SqlSessionTemplate */ @Bean public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } /** * 从配置文件获取属性构造datasource,注意前缀,这里用的是druid,根据自己情况配置, * 原生datasource前缀取"spring.datasource" * * @return */ @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource druidDataSource() { DruidDataSource druidDataSource = new DruidDataSource(); return druidDataSource; } /** * 构造datasource代理对象,替换原来的datasource * @param druidDataSource * @return */ @Primary @Bean("dataSource") public DataSourceProxy dataSourceProxy(DataSource druidDataSource) { return new DataSourceProxy(druidDataSource); } @Bean(name = "sqlSessionFactory") public SqlSessionFactory sqlSessionFactoryBean(DataSourceProxy dataSourceProxy) throws Exception { MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean(); bean.setDataSource(dataSourceProxy); ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); // bean.setConfigLocation(resolver.getResource("classpath:mybatis-config.xml")); bean.setMapperLocations(resolver.getResources("classpath*:mybatis/**/*-mapper.xml")); SqlSessionFactory factory = null; try { factory = bean.getObject(); } catch (Exception e) { throw new RuntimeException(e); } return factory; } /** * MP 自带分页插件 * @return */ @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor page = new PaginationInterceptor(); page.setDialectType("mysql"); return page; } }

yml增加: spring下方

datasource:
  url: jdbc:mysql://localhost:3306/provider?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=GMT%2B8
  username: root
  password: root
  driver-class-name: com.mysql.jdbc.Driver
  type: com.alibaba.druid.pool.DruidDataSource
  druid:
    initial-size: 8
    min-idle: 1
    max-active: 20
    max-wait: 60000
    time-between-eviction-runsMillis: 60000
    min-evictable-idle-timeMillis: 300000
    validation-query: select 'x' FROM DUAL
    test-while-idle: true
    test-on-borrow: false
    test-on-return: false
    pool-prepared-statements: true
    max-open-prepared-statements: 20
    max-pool-prepared-statement-per-connection-size: 20
    filters: stat
    connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    use-global-data-source-stat: true

关键注解:@GlobalTransactional

你可能感兴趣的:(分布式测试版4:seata搭建测试)