目录
1.父pom配置:springboot,springCloud版本管理
实际上只是部署一个单例的Seata,但是仍然按照集群的方式去配置。Seata的高可用依赖于注册中心、配置中心和数据库来实现;因此我们需要修改相关的配置。下面我们以nacos(注册中心和配置中心)和MySQL为例子,Seata-Server使用注册中心进行集群间的通信,同时将事务数据保存到数据库中让整个集群统一使用事务信息。
关于nacos安装,请参考:centos7 部署nacos_wdquan19851029的专栏-CSDN博客
1.安装mysql数据库
本文使用yum安装
首先添加mysql 的yum repo信息:
这个根据你想要安装的mysql版本
rpm -ivh https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm
安装mysql:
yum install mysql-server -y --nogpgcheck
加 --nogpgcheck是为了解决这个错误:Public key for mysql-community-server-5.7.37-1.el7.x86_64.rpm is not installed
warning: /var/cache/yum/x86_64/7/mysql57-community/packages/mysql-community-common-5.7.37-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY
Public key for mysql-community-common-5.7.37-1.el7.x86_64.rpm is not installed
(2/6): mysql-community-common-5.7.37-1.el7.x86_64.rpm | 311 kB 00:00:02
(3/6): mysql-community-libs-5.7.37-1.el7.x86_64.rpm | 2.4 MB 00:00:01
(4/6): mysql-community-libs-compat-5.7.37-1.el7.x86_64.rpm | 1.2 MB 00:00:01
(5/6): mysql-community-client-5.7.37-1.el7.x86_64.rpm | 25 MB 00:00:11
(6/6): mysql-community-server-5.7.37-1.el7.x86_64.rpm | 174 MB 00:00:48
----------------------------------------------------------------------------------------------------------------------------------------------------------
Total 3.8 MB/s | 203 MB 00:00:53
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Importing GPG key 0x5072E1F5:
Userid : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
Fingerprint: a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
Package : mysql57-community-release-el7-11.noarch (installed)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Public key for mysql-community-server-5.7.37-1.el7.x86_64.rpm is not installed
Failing package is: mysql-community-server-5.7.37-1.el7.x86_64
GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
启动mysql服务:
service mysqld restart
重置密码:
如果用空密码登录的话mysql -uroot -p,他会报错ERROR 1045 (28000): Access denied
for user 'root'@'localhost' (using password: NO),因为从mysql5.6开始就不支持空密码登录了,
但mysql会自动生成一个随机的初始密码,可以在/var/log/mysqld.log这个文件中找到,
如下就是我的初始密码 <pDSfw)pT4#o
知道密码就可以登录进mysql了,在命令行输入mysql -u root -p,然后输入密码,就可以进入mysql命令行。
然后在mysql命令行中,输入下面命令修改密码,结果有问题(下面的指令在mysql5.6的时候是没问题的,但是现在的版本是5.7)
set password for root@localhost = password('root@123456');
结果发现修改的密码无效,而使用初始密码也不行了,怎么回事?参考文章解决问题:centos7.4修改mysql5.7 root密码_即墨丹青的博客-CSDN博客
应该使用下面的命令进行修改:
use mysql;
update mysql.user set authentication_string=password('newsys') where user='root' and Host = 'localhost';
创建seata数据库:
退出mysql数据库(怎样进入的? use mysql;),然后创建utf8的数据库
create database `seata` default character set utf8mb4 collate utf8mb4_unicode_ci;
创建表:
seata运行,需要下面的数据库表,可以将内容拷贝到sql文件中,然后导入seata数据库
mysql -uroot -pnewsys -Dseata < seata-db.sql
drop table if exists `global_table`;
create table `global_table` (
`xid` varchar(128) not null,
`transaction_id` bigint,
`status` tinyint not null,
`application_id` varchar(32),
`transaction_service_group` varchar(32),
`transaction_name` varchar(128),
`timeout` int,
`begin_time` bigint,
`application_data` varchar(2000),
`gmt_create` datetime,
`gmt_modified` datetime,
primary key (`xid`),
key `idx_gmt_modified_status` (`gmt_modified`, `status`),
key `idx_transaction_id` (`transaction_id`)
);
-- the table to store BranchSession data
drop table if exists `branch_table`;
create table `branch_table` (
`branch_id` bigint not null,
`xid` varchar(128) not null,
`transaction_id` bigint ,
`resource_group_id` varchar(32),
`resource_id` varchar(256) ,
`lock_key` varchar(128) ,
`branch_type` varchar(8) ,
`status` tinyint,
`client_id` varchar(64),
`application_data` varchar(2000),
`gmt_create` datetime,
`gmt_modified` datetime,
primary key (`branch_id`),
key `idx_xid` (`xid`)
);
-- the table to store lock data
drop table if exists `lock_table`;
create table `lock_table` (
`row_key` varchar(128) not null,
`xid` varchar(96),
`transaction_id` long ,
`branch_id` long,
`resource_id` varchar(256) ,
`table_name` varchar(32) ,
`pk` varchar(36) ,
`gmt_create` datetime ,
`gmt_modified` datetime,
primary key(`row_key`)
);
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)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
--seata新版本加的锁表
CREATE TABLE IF NOT EXISTS `distributed_lock`
(
`lock_key` CHAR(20) NOT NULL,
`lock_value` VARCHAR(20) NOT NULL,
`expire` BIGINT,
PRIMARY KEY (`lock_key`)
) ENGINE = INNODB DEFAULT CHARSET = utf8mb4;
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('AsyncCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryRollbacking', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('TxTimeoutCheck', ' ', 0);
2.安装nacos
不管是Seata还是我们的应用(springboot工程),都要作为服务注册到nacos,应用想要与seata服务交货,要通过nacos获取到seata服务器的地址。
可以直接从网上下载合适的zip包,解压后直接使用。
如果无法下载合适的zip包,那么就需要自己下载合适版本的源码,然后进行编译了
编译nacos源码
直接下载源码,自己编译,就下载版本2.0.4的代码,代码下载地址如下:
https://github.com/alibaba/nacos
在代码的BUILDING文件中,有代码编译指令:
(4) Build distribution packages
Execute the following command in order to build the tar.gz packages and install JAR into local repository:
#build nacos
$ mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U
编译好之后,在distribution/target目录下得到编译好的zip包nacos-server-2.1.0-SNAPSHOT.zip,解压之后运行nacos
具体安装nacos的文章:
centos7 部署nacos_centos7 nacos-CSDN博客
3.安装Seata:
seata下载地址:下载中心 (seata.io)
关于seata的使用,参考:分布式事务Seata -- Seata、Dubbo和Nacos来保证业务数据的一致性_wdquan19851029的专栏-CSDN博客
Seata 1.4.0 + nacos配置和使用,超详细_蓝天-CSDN博客_nacos seata配置
1.解压seata压缩包:
tar -zxvf seata-server-1.4.1-myself.zip
2.修改配置文件:
(1).旧的配置方式
cd /seata/conf
虽然只是部署单机版,但是我们使用集群的方式去配置,配置信息中使用到nacos和mysql。
首先配置注册中心、配置中心,修改conf/registry.conf
其中的namespace,要先去nacos创建,然后得到Id后,贴在这里。但是默认的public(保留空间)的Id是空的,所以这里默认使用public命名空间。
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos"
loadBalance = "RandomLoadBalance"
loadBalanceVirtualNodes = 10
nacos {
application = "seata-server"
serverAddr = "10.17.64.177:8848"
group = "SEATA_GROUP"
namespace = ""
cluster = "default"
username = ""
password = ""
}
..................
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "nacos"
nacos {
serverAddr = "10.17.64.177:8848"
namespace = ""
group = "SEATA_GROUP"
username = ""
password = ""
}
数据库配置 conf/file.conf
seata-server的存储模式有file和db两种,可以通过store.mode属性配置,默认的存储方式是file。
file模式下,seata的事务相关信息会走内存,并持久化到root.data文件中,这种模式性能较高。
db模式是一种高可用的模式,seata的全局事务,分支事务和锁都在数据库中存储,相关表已经在上面创建数据库的时候做好了。
store {
## store mode: file、db、redis
mode = "db"
## file store property
file {
## store location dir
dir = "sessionStore"
# branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
maxBranchSessionSize = 16384
# globe session size , if exceeded throws exceptions
maxGlobalSessionSize = 512
# file buffer size , if exceeded allocate new buffer
fileWriteBufferCacheSize = 16384
# when recover batch read size
sessionReloadReadSize = 100
# async, sync
flushDiskMode = async
}
## database store property
db {
## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp)/HikariDataSource(hikari) etc.
datasource = "druid"
## mysql/oracle/postgresql/h2/oceanbase etc.
dbType = "mysql"
driverClassName = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://127.0.0.1:3306/seata"
user = "root"
password = "newsys"
minConn = 5
maxConn = 100
globalTable = "global_table"
branchTable = "branch_table"
lockTable = "lock_table"
queryLimit = 100
(2)新的配置方式
新的版本(2.X)只有一个配置文件
内容如下:用nacos做为配置中心和注册中心,为什么没有配置mysql或者文件的地方???不需要存储seata本身的数据吗?如上所述,seata本身的数据有2种存储方式,file或者db,不能在seata的配置文件中配置,那么在哪里配置呢?在nacos配置中心中配置就可以了,具体可以看下面第3部分的内容,这里只介绍怎样设置配置中心(seata配置数据)、注册中心(seata微服务注册)
[root@ecs-zhihuichewu-prod-0001 conf]# cat 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: 127.0.0.1:8948
group : "SEATA_GROUP"
namespace: "b220e325-baca-4e7a-bbd8-3a28e2ad30af"
username: "nacos"
password: "KeQry6FV"
registry:
# support: nacos, eureka, redis, zk, consul, etcd3, sofa
type: nacos
nacos:
application: "seata-server"
server-addr: 127.0.0.1:8948
group: "SEATA_GROUP"
namespace: "b220e325-baca-4e7a-bbd8-3a28e2ad30af"
username: "nacos"
password: "KeQry6FV"
cluster: "default"
# server:
# service-port: 8091 #If not configured, the default is '${server.port} + 1000'
security:
secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
tokenValidityInMilliseconds: 1800000
ignore:
urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login
Note:上面的配置中,如果注释了seata 的 server.service-port 端口配置(springboot微服务访问seata的端口),那么这个值就会被默认设置为'${server.port} + 1000',这里就是7091+1000=8091端口
# server:
# service-port: 8091 #If not configured, the default is '${server.port} + 1000'
3. 将seata配置数据导入到nacos配置中心
(1).旧的处理方式
具体介绍,请参考下面的官方文档。
https://github.com/seata/seata/tree/develop/script/config-center
1.创建config.txt文件,在里面添加seata配置
配置信息如下:
#For details about configuration items, see https://seata.io/zh-cn/docs/user/configurations.html
#Transport configuration, for client and server
transport.type=TCP
transport.server=NIO
transport.heartbeat=true
transport.enableTmClientBatchSendRequest=false
transport.enableRmClientBatchSendRequest=true
transport.enableTcServerBatchSendResponse=false
transport.rpcRmRequestTimeout=30000
transport.rpcTmRequestTimeout=30000
transport.rpcTcRequestTimeout=30000
transport.threadFactory.bossThreadPrefix=NettyBoss
transport.threadFactory.workerThreadPrefix=NettyServerNIOWorker
transport.threadFactory.serverExecutorThreadPrefix=NettyServerBizHandler
transport.threadFactory.shareBossWorker=false
transport.threadFactory.clientSelectorThreadPrefix=NettyClientSelector
transport.threadFactory.clientSelectorThreadSize=1
transport.threadFactory.clientWorkerThreadPrefix=NettyClientWorkerThread
transport.threadFactory.bossThreadSize=1
transport.threadFactory.workerThreadSize=default
transport.shutdown.wait=3
transport.serialization=seata
transport.compressor=none
#Transaction routing rules configuration, only for the client
service.vgroupMapping.default_tx_group=default
#If you use a registry, you can ignore it
service.default.grouplist=127.0.0.1:8091
service.enableDegrade=false
service.disableGlobalTransaction=false
#Transaction rule configuration, only for the client
client.rm.asyncCommitBufferLimit=10000
client.rm.lock.retryInterval=10
client.rm.lock.retryTimes=30
client.rm.lock.retryPolicyBranchRollbackOnConflict=true
client.rm.reportRetryCount=5
client.rm.tableMetaCheckEnable=false
client.rm.tableMetaCheckerInterval=60000
client.rm.sqlParserType=druid
client.rm.reportSuccessEnable=false
client.rm.sagaBranchRegisterEnable=false
client.rm.sagaJsonParser=fastjson
client.rm.tccActionInterceptorOrder=-2147482648
client.tm.commitRetryCount=5
client.tm.rollbackRetryCount=5
client.tm.defaultGlobalTransactionTimeout=60000
client.tm.degradeCheck=false
client.tm.degradeCheckAllowTimes=10
client.tm.degradeCheckPeriod=2000
client.tm.interceptorOrder=-2147482648
client.undo.dataValidation=true
client.undo.logSerialization=jackson
client.undo.onlyCareUpdateColumns=true
server.undo.logSaveDays=7
server.undo.logDeletePeriod=86400000
client.undo.logTable=undo_log
client.undo.compress.enable=true
client.undo.compress.type=zip
client.undo.compress.threshold=64k
#For TCC transaction mode
tcc.fence.logTableName=tcc_fence_log
tcc.fence.cleanPeriod=1h
#Log rule configuration, for client and server
log.exceptionRate=100
#Transaction storage configuration, only for the server. The file, DB, and redis configuration values are optional.
store.mode=file
store.lock.mode=file
store.session.mode=file
#Used for password encryption
store.publicKey=
#If `store.mode,store.lock.mode,store.session.mode` are not equal to `file`, you can remove the configuration block.
store.file.dir=file_store/data
store.file.maxBranchSessionSize=16384
store.file.maxGlobalSessionSize=512
store.file.fileWriteBufferCacheSize=16384
store.file.flushDiskMode=async
store.file.sessionReloadReadSize=100
#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://127.0.0.1:3306/seata?useUnicode=true&rewriteBatchedStatements=true
store.db.user=root
store.db.password=newsys
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
#These configurations are required if the `store mode` is `redis`. If `store.mode,store.lock.mode,store.session.mode` are not equal to `redis`, you can remove the configuration block.
store.redis.mode=single
store.redis.single.host=127.0.0.1
store.redis.single.port=6379
store.redis.sentinel.masterName=
store.redis.sentinel.sentinelHosts=
store.redis.maxConn=10
store.redis.minConn=1
store.redis.maxTotal=100
store.redis.database=0
store.redis.password=
store.redis.queryLimit=100
#Transaction rule configuration, only for the server
server.recovery.committingRetryPeriod=1000
server.recovery.asynCommittingRetryPeriod=1000
server.recovery.rollbackingRetryPeriod=1000
server.recovery.timeoutRetryPeriod=1000
server.maxCommitRetryTimeout=-1
server.maxRollbackRetryTimeout=-1
server.rollbackRetryTimeoutUnlockEnable=false
server.distributedLockExpireTime=10000
server.session.branchAsyncQueueSize=5000
server.session.enableBranchAsyncRemove=true
#Metrics configuration, only for the server
metrics.enabled=false
metrics.registryType=compact
metrics.exporterList=prometheus
metrics.exporterPrometheusPort=9898
Note:上面内容中主要是修改属性 store.mode= ,这个属性决定了seata自身的业务数据(非配置数据)存储到哪里,可以配置为file,db和redis,如果想存储到db中,这里就改为db,然后在下面的db配置项中配置正确的db信息。
脚本nacos-config.sh,将上述配置文件中配置信息导入到nacos
#!/bin/sh
# Copyright 1999-2019 Seata.io Group.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at、
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
while getopts ":h:p:g:t:u:w:" opt
do
case $opt in
h)
host=$OPTARG
;;
p)
port=$OPTARG
;;
g)
group=$OPTARG
;;
t)
tenant=$OPTARG
;;
u)
username=$OPTARG
;;
w)
password=$OPTARG
;;
?)
echo " USAGE OPTION: $0 [-h host] [-p port] [-g group] [-t tenant] [-u username] [-w password] "
exit 1
;;
esac
done
if [ -z ${host} ]; then
host=localhost
fi
if [ -z ${port} ]; then
port=8848
fi
if [ -z ${group} ]; then
group="SEATA_GROUP"
fi
if [ -z ${tenant} ]; then
tenant=""
fi
if [ -z ${username} ]; then
username=""
fi
if [ -z ${password} ]; then
password=""
fi
nacosAddr=$host:$port
contentType="content-type:application/json;charset=UTF-8"
echo "set nacosAddr=$nacosAddr"
echo "set group=$group"
urlencode() {
length="${#1}"
i=0
while [ $length -gt $i ]; do
char="${1:$i:1}"
case $char in
[a-zA-Z0-9.~_-]) printf $char ;;
*) printf '%%%02X' "'$char" ;;
esac
i=`expr $i + 1`
done
}
failCount=0
tempLog=$(mktemp -u)
function addConfig() {
dataId=`urlencode $1`
content=`urlencode $2`
curl -X POST -H "${contentType}" "http://$nacosAddr/nacos/v1/cs/configs?dataId=$dataId&group=$group&content=$content&tenant=$tenant&username=$username&password=$password" >"${tempLog}" 2>/dev/null
if [ -z $(cat "${tempLog}") ]; then
echo " Please check the cluster status. "
exit 1
fi
if [ "$(cat "${tempLog}")" == "true" ]; then
echo "Set $1=$2 successfully "
else
echo "Set $1=$2 failure "
failCount=`expr $failCount + 1`
fi
}
count=0
COMMENT_START="#"
for line in $(cat $(dirname "$PWD")/config.txt | sed s/[[:space:]]//g); do
if [[ "$line" =~ ^"${COMMENT_START}".* ]]; then
continue
fi
count=`expr $count + 1`
key=${line%%=*}
value=${line#*=}
addConfig "${key}" "${value}"
done
echo "========================================================================="
echo " Complete initialization parameters, total-count:$count , failure-count:$failCount "
echo "========================================================================="
if [ ${failCount} -eq 0 ]; then
echo " Init nacos config finished, please start seata-server. "
else
echo " init nacos config fail. "
fi
导入seata配置信息到nacos:
sh ${SEATAPATH}/bin/nacos-config.sh -h localhost -p 8848 -g SEATA_GROUP -t 5a3c7d6c-f497-4d68-a71a-2e5e3340b3ca -u nacos-w nacos
参数说明:
Parameter Description:
-h: host, the default value is localhost.
-p: port, the default value is 8848.
-g: Configure grouping, the default value is 'SEATA_GROUP'.
-t: Tenant information, corresponding to the namespace ID field of Nacos, the default value is ''.
-u: username, nacos 1.2.0+ on permission control, the default value is ''.
-w: password, nacos 1.2.0+ on permission control, the default value is ''.
(2)新的处理方式:
上述方式会在nacos配置中心对应的namespace中生成多个data id,基本是config.txt文件中的每个key对应一个data id,这样是不是有问题???
既然是key-value的格式,那么直接放到一个配置文件(对应一个data id)中是不是更合适?
所以直接在nacos配置中心对应的namespace中创建一个data id(seataServer.properties),将上面config.txt文件中的内容都拷贝进去。
Note:配置文件中的属性store.mode=, 要特别注意
上面内容中主要是修改属性 store.mode= ,这个属性决定了seata自身的业务数据存储到哪里,可以配置为file,db和redis,如果想存储到db中,这里就改为db,然后在下面的db配置项中配置正确的db信息。
4. 启动seata:
cd /seata/bin
nohup sh seata-server.sh -p 8091 -h 127.0.0.1 -m db &
启动成功信息:
进程信息:
[root@ecs-zhihuiyingxiao-dev-0002 bin]# ps -ef | grep seata
root 32470 24689 2 14:46 pts/2 00:00:03 /usr/local/jdk1.8.0_181/bin/java -server -Xmx2048m -Xms2048m -Xmn1024m -Xss512k -XX:SurvivorRatio=10 -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=256m -XX:MaxDirectMemorySize=1024m -XX:-OmitStackTraceInFastThrow -XX:-UseAdaptiveSizePolicy -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/usr/local/seata/logs/java_heapdump.hprof -XX:+DisableExplicitGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=75 -Xloggc:/usr/local/seata/logs/seata_gc.log -verbose:gc -Dio.netty.leakDetectionLevel=advanced -Dlogback.color.disable-for-bat=true -classpath /usr/local/seata/conf:/usr/local/seata/lib/* -Dapp.name=seata-server -Dapp.pid=32470 -Dapp.repo=/usr/local/seata/lib -Dapp.home=/usr/local/seata -Dbasedir=/usr/local/seata io.seata.server.Server -p 8091 -h 127.0.0.1 -m db
root 32525 24689 0 14:48 pts/2 00:00:00 grep --color=auto seata
5.Springboot中配置seata:
如果采用3.2的方式在nacos中进行配置,那么springboot中使用seata的时候,可以这样配置
在其中指定:dataId: "seataServer.properties"
seata:
config:
type: nacos
nacos:
server-addr: 127.0.0.1:8948
group : "SEATA_GROUP"
namespace: "b220e325-baca-4e7a-bbd8-3a28e2ad30af"
dataId: "seataServer.properties"
registry:
type: nacos
nacos:
application: seata-server
server-addr: 127.0.0.1:8948
group : "SEATA_GROUP"
namespace: "b220e325-baca-4e7a-bbd8-3a28e2ad30af"
cluster: "default"
tx-service-group: default_tx_group
service:
vgroup-mapping:
default_tx_group: default
springboot微服务通过nacos找到seata服务,seata本身在这里也是做为一个微服务注册到了nacos中
3. springboot配置:
Seata需要注册到nacos,做为一个微服务,所以springboot工程也要通过nacos去访问seata,因次springboot工程也要配置nacos,但是因为nacos依赖包版本问题,导致工程启动失败,所以需要nacos依赖包的版本与nacos服务器的版本匹配,springboot工程pom配置如下:
1.父pom配置:springboot,springCloud版本管理
<properties>
<maven.build.timestamp.format>HHmmss</maven.build.timestamp.format>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-boot.version>2.6.11</spring-boot.version>
<spring-cloud.version>2021.0.4</spring-cloud.version>
<spring-cloud-alibaba.version>2021.0.4.0</spring-cloud-alibaba.version>
<mybatis.plus.version>3.4.1</mybatis.plus.version>
<sharding-sphere.version>4.1.1</sharding-sphere.version>
</properties><!--继承SpringCloud和SpringCloudAlibaba-->
<dependencyManagement>
<dependencies>
<!-- springCloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- SpringCloudAlibaba -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency><!-- SpringBoot 依赖配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2.子pom配置:nacos版本配置:
<parent> <artifactId>springcloud-seata-sharding-jdbc-mybatis-plus-samples</artifactId> <groupId>io.seata</groupId> <version>1.1.0</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>seata-order-sample</artifactId> <dependencies> <!--注册中心--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
上面没有定义具体版本,那么根据父pom的springboot,springCloud配置,这里nacos的默认版本是什么呢?查看jar包信息如下:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <version>2.6.11</version> <scope>compile</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> <version>2.6.11</version> <scope>compile</scope> <optional>true</optional> </dependency> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> <version>2.0.4</version> <scope>compile</scope> </dependency>
springboot 2.6.11对应的nacos版本是2.0.4,可是没从网上下载到合适的nacos安装包,怎么办?
按照第2节说的,自己下载对应版本的nacos源码进行编译就好了。