我使用的版本:java
SpringCloudAlibaba : 2.1.1.RELEASEnode
Nacos : 1.1.4mysql
Seata : 0.9git
版本很是重要,有時版本不對,項目啓動會報一些莫名其妙的錯誤.github
下載地址:https://github.com/alibaba/nacos/releasesredis
解壓以後,若是是單機部署的話,能夠點擊修改startup.sh中的MODE爲standalone.spring
export SERVER="nacos-server" # 修改MODE export MODE="standalone" export FUNCTION_MODE="all"
注意若是電腦ip變化了,須要刪除/nacos/data文件夾,而後在重啓.不然會提示服務downsql
啓動命令數據庫
startup.shapache
改動點:
transport { # tcp udt unix-domain-socket type = "TCP" #NIO NATIVE server = "NIO" #enable heartbeat heartbeat = true #thread factory for netty thread-factory { boss-thread-prefix = "NettyBoss" worker-thread-prefix = "NettyServerNIOWorker" server-executor-thread-prefix = "NettyServerBizHandler" share-boss-worker = false client-selector-thread-prefix = "NettyClientSelector" client-selector-thread-size = 1 client-worker-thread-prefix = "NettyClientWorkerThread" # netty boss thread size,will not be used for UDT boss-thread-size = 1 #auto default pin or 8 worker-thread-size = 8 } shutdown { # when destroy server, wait seconds wait = 3 } serialization = "seata" compressor = "none" } service { #vgroup->rgroup # 1.自定義事務組名稱 vgroup_mapping.my_test_tx_group = "xiangjiao_tx_group" #only support single node default.grouplist = "127.0.0.1:8091" #degrade current not support enableDegrade = false #disable disable = false #unit ms,s,m,h,d represents milliseconds, seconds, minutes, hours, days, default permanent max.commit.retry.timeout = "-1" max.rollback.retry.timeout = "-1" } client { async.commit.buffer.limit = 10000 lock { retry.internal = 10 retry.times = 30 } report.retry.count = 5 tm.commit.retry.count = 1 tm.rollback.retry.count = 1 } ## transaction log store store { ## store mode: file、db # 2.修改成db mode = "db" ## file store file { dir = "sessionStore" # branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions max-branch-session-size = 16384 # globe session size , if exceeded throws exceptions max-global-session-size = 512 # file buffer size , if exceeded allocate new buffer file-write-buffer-cache-size = 16384 # when recover batch read size session.reload.read_size = 100 # async, sync flush-disk-mode = async } ## database store db { ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp) etc. datasource = "dbcp" ## mysql/oracle/h2/oceanbase etc. db-type = "mysql" driver-class-name = "com.mysql.jdbc.Driver" url = "jdbc:mysql://127.0.0.1:3306/seata" # 3.修改數據庫信息 user = "root" password = "root" min-conn = 1 max-conn = 3 global.table = "global_table" branch.table = "branch_table" lock-table = "lock_table" query-limit = 100 } } lock { ## the lock store mode: local、remote mode = "remote" local { ## store locks in user's database } remote { ## store locks in the seata's server } } recovery { #schedule committing retry period in milliseconds committing-retry-period = 1000 #schedule asyn committing retry period in milliseconds asyn-committing-retry-period = 1000 #schedule rollbacking retry period in milliseconds rollbacking-retry-period = 1000 #schedule timeout retry period in milliseconds timeout-retry-period = 1000 } transaction { undo.data.validation = true undo.log.serialization = "jackson" undo.log.save.days = 7 #schedule delete expired undo_log in milliseconds undo.log.delete.period = 86400000 undo.log.table = "undo_log" } ## metrics settings metrics { enabled = false registry-type = "compact" # multi exporters use comma divided exporter-list = "prometheus" exporter-prometheus-port = 9898 } support { ## spring spring { # auto proxy the DataSource bean datasource.autoproxy = false } }
新建一個數據庫seata,並執行conf/db_store.sql文件,創建所需的表
另外還須要在你的每一個業務數據庫中執行conf/db_undo_log.sql
5. 修改conf/registry.conf配置文件
修改type爲nacos
registry { # file 、nacos 、eureka、redis、zk、consul、etcd三、sofa type = "nacos" nacos { serverAddr = "localhost:8848" namespace = "" cluster = "default" username="nacos" password="nacos" }
seata-server.sh
啓動完成後,能夠登錄nacos,127.0.0.1/8848/nacos看看服務是否註冊成功
我出現過登錄時用戶名密碼不對的狀況,後來從新配置了JDK環境變量解決了.
重點的是看application.properties中關於seata的配置,以及兩個配置文件file.conf,registry.conf
另外除了使用全局事務註解@GlobalTransactional(name = "createReport",rollbackFor = Exception.class)外,還須要配置數據源,不然全局事務不會生效
package com.mmc.reward.config; import com.alibaba.druid.pool.DruidDataSource; import io.seata.rm.datasource.DataSourceProxy; import org.apache.commons.lang3.StringUtils; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.boot.autoconfigure.MybatisProperties; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; import javax.sql.DataSource; import java.io.IOException; /** * Description: * author: yu.hb * Date: 2019-11-01 */ @Configuration @EnableConfigurationProperties({MybatisProperties.class}) public class DataSourceConfiguration { @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource dataSource() { return new DruidDataSource(); } @Bean public DataSourceProxy dataSourceProxy(DataSource dataSource) { return new DataSourceProxy(dataSource); } @Bean public SqlSessionFactoryBean sqlSessionFactoryBean(DataSourceProxy dataSourceProxy, MybatisProperties mybatisProperties) { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSourceProxy); ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); try { Resource[] mapperLocaltions = resolver.getResources(mybatisProperties.getMapperLocations()[0]); bean.setMapperLocations(mapperLocaltions); if (StringUtils.isNotBlank(mybatisProperties.getConfigLocation())) { Resource[] resources = resolver.getResources(mybatisProperties.getConfigLocation()); bean.setConfigLocation(resources[0]); } } catch (IOException e) { e.printStackTrace(); } return bean; } }
注意: 分佈式事務分組名是對應的seata/conf/file.conf中的,3個地方都是配同樣的,我這裏配的是xiangjiao_tx_group
vgroup_mapping.my_test_tx_group = "xiangjiao_tx_group"
對應項目中application.properties中的
spring.cloud.alibaba.seata.tx-service-group=xiangjiao_tx_group
對應項目中的file.conf文件中的
vgroup_mapping.xiangjiao_tx_group = "default"