springboot整合seata

springboot整合seata,第1张

1、maven依赖


    com.alibaba.cloud
    spring-cloud-starter-alibaba-seata
    2.2.7.RELEASE
    
        
            io.seata
            seata-all
        
    

    io.seata
    seata-spring-boot-starter
    1.4.2


    com.esotericsoftware
    kryo
    4.0.2


    de.javakaffee
    kryo-serializers
    0.42

2、添加配置

spring.main.allow-bean-definition-overriding= true

spring.application.name=order-service

seata.enabled=true

seata.application-id=stock-seata-example

seata.tx-service-group=my_test_tx_group

seata.client.rm-report-success-enable=true

seata.client.rm-table-meta-check-enable=false

seata.client.rm-report-retry-count=5

seata.client.rm-async-commit-buffer-limit=10000

seata.client.rm.lock.lock-retry-internal=10

seata.client.rm.lock.lock-retry-times=30

seata.client.rm.lock.lock-retry-policy-branch-rollback-on-conflict=true

seata.client.tm-commit-retry-count=3

seata.client.tm-rollback-retry-count=3

seata.client.undo.undo-data-validation=true

seata.client.undo.undo-log-serialization=jackson

seata.client.undo.undo-log-table=undo_log

seata.log.exception-rate=100

seata.client.support.spring.datasource-autoproxy=true

seata.service.vgroup-mapping.my_test_tx_group=default

seata.service.enable-degrade=false

seata.service.disable-global-transaction=false

seata.service.grouplist.default=127.0.0.1:8091

seata.transport.shutdown.wait=3

seata.transport.thread-factory.boss-thread-prefix=NettyBoss

seata.transport.thread-factory.worker-thread-prefix=NettyServerNIOWorker

seata.transport.thread-factory.server-executor-thread-prefix=NettyServerBizHandler

seata.transport.thread-factory.share-boss-worker=false

seata.transport.thread-factory.client-selector-thread-prefix=NettyClientSelector

seata.transport.thread-factory.client-selector-thread-size=1

seata.transport.thread-factory.client-worker-thread-prefix=NettyClientWorkerThread

seata.transport.type=TCP

seata.transport.server=NIO

seata.transport.heartbeat=true

seata.transport.serialization=seata

seata.transport.compressor=none

seata.transport.enable-client-batch-send-request=true

seata.registry.type=eureka

seata.registry.eureka.service-url=eureka的地址

seata.registry.eureka.application=default

seata.registry.eureka.weight=1

seata.client.undo.logSerialization=kryo

3、修改数据源

package cn.com.dragonpass.infra.account.config;

import com.alibaba.druid.pool.DruidDataSource;

import io.seata.rm.datasource.DataSourceProxy;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration

public class DataSourceProxyConfig {

@Autowired

private DataSourceProperties dataSourceProperties;

// @Bean

// @ConfigurationProperties(prefix = "spring.datasource")

// public DataSource dataSource() {

// return ;

// }

@Bean

public DataSourceProxy dataSourceProxy() {

DruidDataSource druidDataSource = new DruidDataSource();

druidDataSource.setUrl(dataSourceProperties.getUrl());

druidDataSource.setUsername(dataSourceProperties.getUsername());

druidDataSource.setPassword(dataSourceProperties.getPassword());

druidDataSource.setDriverClassName(dataSourceProperties.getDriverClassName());

druidDataSource.setInitialSize(0);

druidDataSource.setMaxActive(180);

druidDataSource.setMaxWait(60000);

druidDataSource.setMinIdle(0);

druidDataSource.setValidationQuery("Select 1 from DUAL");

druidDataSource.setTestOnBorrow(false);

druidDataSource.setTestOnReturn(false);

druidDataSource.setTestWhileIdle(true);

druidDataSource.setTimeBetweenEvictionRunsMillis(60000);

druidDataSource.setMinEvictableIdleTimeMillis(25200000);

druidDataSource.setRemoveAbandoned(true);

druidDataSource.setRemoveAbandonedTimeout(1800);

druidDataSource.setLogAbandoned(true);

return new DataSourceProxy(druidDataSource);

}

}

4、创建undo_log表

mysql:

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

5、使用

在方法上添加@GlobalTransactional即可开启事务

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/923104.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存