目录
上篇: 安装seata 并启动成功的传送门
1. 前言:
2. springCloud 使用seata at 的步骤如下
第一步 查看springCloud版本
第二步添加maven依赖
第三步 添加yml配置
第四步: 配置数据源(druid)
第五步 修复一个警告
第六步: 启动后 看看日志是否成功
上篇: 安装seata 并启动成功的传送门
传送门===> 微服务: Seata AT 分布式事务以及配置方式(上篇)
1. 前言:
事务概念:
TC((Transaction Coordinator)): 事务协调者
维护全局和分支事务的状态,驱动全局事务提交或回滚。
TM(Transaction Manager):事务管理器
定义全局事务的范围:开始全局事务、提交或回滚全局事务。
RM (Resource Manager):资源管理器
管理分支事务处理的资源,与TC交谈以注册分支事务和报告分支事务的状态,并驱动分支事务提交或回滚。
2. springCloud 使用seata at 的步骤如下
第一步 查看springCloud版本
第二步添加maven依赖
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-seata</artifactId><exclusions><exclusion><groupId>io.seata</groupId><artifactId>seata-all</artifactId></exclusion></exclusions></dependency><dependency><groupId>io.seata</groupId><artifactId>seata-spring-boot-starter</artifactId><version>1.3.0</version></dependency>
第三步 添加yml配置
#pzy 配置分布式事务
seata:enabled: true # 1.0新特性,需要依赖seata-spring-boot-starter 默认为trueenable-auto-data-source-proxy: true # 牵扯到回滚tx-service-group: seata_group # 需要与config.txt中的 service.vgroupMapping.seata_group=default保持一致server:vgroup-mapping:seata_group: default # 需要与config.txt中的 service.vgroupMapping.seata_group=default 保持一致#grouplist:# default: 127.0.0.1:8091disable-global-transaction: falseregistry: ## 注册中心type: nacos #注册nacosnacos:application: seata-server #nacos中seata-server启动注册成功后的服务名称server-addr: *:8848username: *password: *group: SEATA_GROUPnamespace: 49b38634-8a78-4216-a80c-7181976301c5config: ## 配置中心 与register.conf文件中的保持一致type: nacosnacos:server-addr: *:8848group: *#与register.conf文件中的保持一致username: *password: *namespace: 49b38634-8a78-4216-a80c-7181976301c5
第四步: 配置数据源(druid)
import com.alibaba.druid.pool.DruidDataSource;
import io.seata.rm.datasource.DataSourceProxy;
import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;import javax.sql.DataSource;/*** 配置数据源,使用seata对数据源做代理*/
@Slf4j
@Configuration
public class DataSourcesConfig {// V1版本
// @Value("${mybatis-plus.mapper-locations}")
// private String mapperLocations;
//
// @Bean
// @ConfigurationProperties(prefix = "spring.datasource")
// public DataSource druidDataSource() {
// return new DruidDataSource();
// }
//
// /**
// * 使用 io.seata.rm.datasource.DataSourceProxy
// * @param druidDataSource
// * @return
// */
// @Bean
// public DataSourceProxy dataSourceProxy(DataSource druidDataSource) {
// return new DataSourceProxy(druidDataSource);
// }
//
// @Bean
// public SqlSessionFactory sqlSessionFactoryBean(DataSourceProxy dataSourceProxy) throws Exception {
// MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
// bean.setDataSource(dataSourceProxy);
// ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
// bean.setMapperLocations(resolver.getResources(mapperLocations));
// return bean.getObject();
// }//V2 版本@Autowiredprivate DataSourceProperties dataSourceProperties;@Bean(name = "dataSource") // 声明其为Bean实例@Primary // 在同样的DataSource中,首先使用被标注的DataSourcepublic DataSource druidDataSource() {DruidDataSource druidDataSource = new DruidDataSource();log.info("dataSourceProperties.getUrl():{}", dataSourceProperties.getUrl());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);log.info("装载dataSource........");return new DataSourceProxy(druidDataSource);}}
[如果是mybatis plus 分页失效的, 部分组件失效的, 请使用这套]
第五步 修复一个警告
Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be....
undertow 设置一下buffer pool,不然他就使用默认,启动时会出现警告
import io.undertow.server.DefaultByteBufferPool;
import io.undertow.websockets.jsr.WebSocketDeploymentInfo;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.stereotype.Component;/***** 解决方案一: 配置文件* 解决方案二: application.properties的配置** @author: pzy* @date: 2023-05-19* @description: 解决启动io.undertow.websockets.jsr UT026010: Buffer pool was not* set on WebSocketDeploymentInfo, the default pool will be used的警告*/
@Component
public class CustomizationBean implements WebServerFactoryCustomizer<UndertowServletWebServerFactory> {@Overridepublic void customize(UndertowServletWebServerFactory factory) {factory.addDeploymentInfoCustomizers(deploymentInfo -> {WebSocketDeploymentInfo webSocketDeploymentInfo = new WebSocketDeploymentInfo();webSocketDeploymentInfo.setBuffers(new DefaultByteBufferPool(false, 1024)
// new DefaultByteBufferPool(false, 1024,20,4));deploymentInfo.addServletContextAttribute("io.undertow.websockets.jsr.WebSocketDeploymentInfo", webSocketDeploymentInfo);});}
}