目录
前言
注:因版本不同,对应的配置也有所区别,请参照下面链接
https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E
1 安装 nacos2.1.0
官网下载:https://github.com/alibaba/nacos/releases
百度网盘链接: https://pan.baidu.com/s/1J2SLS9M_P-B9tbiNrhXRhw
提取码:yyds
1.1 找到2.1.0版本,点击下载
1.2 进行解压
1.3 创建数据库
创建 nacos 数据库,进到 conf 目录下,打开 nacos-mysql.sql 文件,创建表
1.4 更改配置
进到 bin 目录下,编辑 startup.cmd ,把 cluster 改为 standalone
进到 conf 目录下,打开 application.properties 文件
打开数据库的注释,更改数据库配置
1.5 启动
进入 bin 目录下,双击 startup.cmd ,启动 nacos
输入账号和密码,至此 nacos 安装成功
2 安装 seata1.5.2
官网下载:https://github.com/apache/incubator-seata/tags
百度网盘链接:https://pan.baidu.com/s/1OD50fKb-0tF3lPoZ1tA47Q
提取码:yyds
2.1 找到1.5.2版本,点击下载
2.2 进行解压
2.3 创建数据库
创建 seata 数据库,进入 script\server\db 目录下,打开 mysql.sql 文件,创建表
为自己项目的数据库创建 undo_log 表
DROP TABLE IF EXISTS `undo_log`;
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`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2.4 更改配置
进到 conf 目录下,打开 application.yml 文件,复制下面配置
server:
port: 7091
spring:
application:
name: seata-server
logging:
config: classpath:logback-spring.xml
file:
path: ${user.home}/logs/seata
extend:
logstash-appender:
destination: 127.0.0.1:4560
kafka-appender:
bootstrap-servers: 127.0.0.1:9092
topic: logback_to_logstash
console:
user:
username: seata
password: seata
seata:
config:
# support: nacos 、 consul 、 apollo 、 zk 、 etcd3
type: nacos
nacos:
server-addr: http://127.0.0.1:8848
# namespace: 7392baed-d98b-48a4-8676-34e1b38eade6
namespace:
group: SEATA_GROUP
username: nacos
password: nacos
##if use MSE Nacos with auth, mutex with username/password attribute
#access-key: ""
#secret-key: ""
data-id: seataServer.properties
registry:
# support: nacos 、 eureka 、 redis 、 zk 、 consul 、 etcd3 、 sofa
type: nacos
preferred-networks: 30.240.*
nacos:
application: seata-server
server-addr: http://127.0.0.1:8848
group: SEATA_GROUP
namespace:
# namespace: 7392baed-d98b-48a4-8676-34e1b38eade6
cluster: default
username: nacos
password: nacos
##if use MSE Nacos with auth, mutex with username/password attribute
#access-key: ""
#secret-key: ""
security:
secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
tokenValidityInMilliseconds: 1800000
ignore:
urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login
2.5 创建配置
进入 nacos
- Data ID:seataServer.properties
- Group:SEATA_GROUP
- 配置格式:properties
- 配置内容:
#Transaction storage configuration, only for the server. The file, DB, and redis configuration values are optional.
store.mode=db
store.lock.mode=db
store.session.mode=db
#These configurations are required if the `store mode` is `db`. If `store.mode,store.lock.mode,store.session.mode` are not equal to `db`, you can remove the configuration block.
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://1.14.165.93:3306/seata?useUnicode=true&rewriteBatchedStatements=true
store.db.user=root
store.db.password=123456
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.distributedLockTable=distributed_lock
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000
#Transaction routing rules configuration, only for the client
#注意:一定要配置【超级大坑】 mall_tx_group这个可以自己命名
service.vgroupMapping.mall_tx_group=default
2.6 启动
进入 bin 目录下,双击 seata-server.bat ,启动 seata
查看 nacos 服务列表,至此 seata 启动成功
3 SpringCloud整合
注:以下版本必须完全相同
SpringBoot 2.3.12.RELEASE
SpringCloud Hoxton.SR12
SpringCloud Alibaba 2.2.9.RELEASE
nacos 2.1.0.RELEASE
seata 1.5.2
<!-- 注意一定要引入对版本,要引入spring-cloud版本seata,而不是springboot版本的seata-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<!-- 排除掉springcloud默认的seata版本,以免版本不一致出现问题-->
<exclusions>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
</exclusion>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 上面排除掉了springcloud默认色seata版本,此处引入和seata-server版本对应的seata包-->
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.5.2</version>
</dependency>
4 各种问题
yml文件配置
问题一
ERROR 14668 --- [ restartedMain] i.s.c.r.netty.NettyClientChannelManager : can not get cluster name in registry config 'service.vgroupMapping.default_tx_group', please make sure registry config correct
配置以下解决,mall_tx_group 就是 2.5 那个超级大坑,自己配的什么名字,就用自己的
把 mall_tx_group 换成自己的就行了,其它不用变
seata:
tx-service-group: mall_tx_group
service:
vgroup-mapping:
mall_tx_group: default
grouplist:
default: 127.0.0.1:8091
问题二
配了上面还是项目还是启动失败,报了很多错
Caused by: java.lang.IllegalAccessError: class com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceWrapper$$EnhancerBySpringCGLIB$$94ab1087 cannot access its superclass com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceWrapper
在启动类上配置
exclude = DruidDataSourceAutoConfigure.class
结语
至此SpringCloud整合seata成功!!!
解决各种bug花了一天,制作不易
最后本文章如果对您有一点点帮助的话,求赞 求收藏 求关注,您的支持是我创作的最大动力!
文章粗浅,希望对大家有帮助!