关于springboot:SpringBootSpringCloudSeata配置

57次阅读

共计 7362 个字符,预计需要花费 19 分钟才能阅读完成。

记录 SpringBoot+SpringCloud+Seata 的配置。这里用的是 AT 模式。注册核心用的 Nacos。本文不蕴含 nacos 配置部署。如有须要请自行百度。(吐槽,官网文档几乎一言难尽。。)

我这里创立了两个我的项目,demo3 和 demo4.POM 配置是一样的,这里间接贴出。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.11.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo3</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo3</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR11</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--<dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.5.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>-->
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.2.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>io.seata</groupId>
            <artifactId>seata-spring-boot-starter</artifactId>
            <version>1.4.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
            <version>2.2.1.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>io.seata</groupId>
                    <artifactId>seata-spring-boot-starter</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

SpringBoot,SpringCloud 版本能够间接查看以上 pom。
我这里用的 Seata 最新版本,1.4.2.
配置 seata 的时候大家留神,客户端和服务端版本 必须统一
服务端下载地址:https://github-releases.githu…

下载完当前开始配置服务端。总结为以下几个步骤
1. 批改 file.conf 配置。我这里用的 db 模式
2. 相干数据库表初始化到数据库。(业务数据库和 seata 本人的数据库都须要配置并初始化表构造)参考文档:http://seata.io/zh-cn/docs/dev/mode/at-mode.html。https://github.com/seata/seata/tree/1.4.0/script/client/at/db。
3. 批改 registry.conf 配置。我这里用的 nacos 注册核心
4.registry.conf 中配置核心我也一并应用了 nacos。这里须要运行一段脚本,参考官网:https://github.com/seata/seata/tree/1.4.0/script/config-center。(脚本里相干配置改成本人的。此处深坑,service.vgroupMapping.xxx. 这里的 xxx 必须和上面 spring 中的配置对应)
5. 批改 spring 配置(此处深坑,seata.service.vgroup-mapping.xxx=default. 这里的 xxx 必须和下面配置核心的配置统一)
6. 须要分布式事务的服务加上 @GlobalTransactional 注解
服务端配置文件:
file.conf

## transaction log store, only used in seata-server
store {
  ## store mode: file、db、redis
  mode = "db"
  ## rsa decryption public key
  publicKey = ""
  ## 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"
    ## if using mysql to store the data, recommend add rewriteBatchedStatements=true in jdbc connection param
    url = "jdbc:mysql://you ip:3306/seata?rewriteBatchedStatements=true"
    user = "you username"
    password = "you password"
    minConn = 5
    maxConn = 100
    globalTable = "global_table"
    branchTable = "branch_table"
    lockTable = "lock_table"
    queryLimit = 100
    maxWait = 5000
  }

  ## redis store property
  redis {
    ## redis mode: single、sentinel
    mode = "single"
    ## single mode property
    single {
      host = "127.0.0.1"
      port = "6379"
    }
    ## sentinel mode property
    sentinel {masterName = ""## such as"10.28.235.65:26379,10.28.235.65:26380,10.28.235.65:26381"sentinelHosts =""}
    password = ""database ="0"
    minConn = 1
    maxConn = 10
    maxTotal = 100
    queryLimit = 100
  }
}

registry.conf

registry {
  # file、nacos、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos"

  nacos {
    application = "seata-server"
    serverAddr = "you ip:8848"
    group = "SEATA_GROUP"
    namespace = ""cluster ="default"username =""
    password = ""
  }
  eureka {
    serviceUrl = "http://localhost:8761/eureka"
    application = "default"
    weight = "1"
  }
  redis {
    serverAddr = "localhost:6379"
    db = 0
    password = ""cluster ="default"
    timeout = 0
  }
  zk {
    cluster = "default"
    serverAddr = "127.0.0.1:2181"
    sessionTimeout = 6000
    connectTimeout = 2000
    username = ""password =""
  }
  consul {
    cluster = "default"
    serverAddr = "127.0.0.1:8500"
    aclToken = ""
  }
  etcd3 {
    cluster = "default"
    serverAddr = "http://localhost:2379"
  }
  sofa {
    serverAddr = "127.0.0.1:9603"
    application = "default"
    region = "DEFAULT_ZONE"
    datacenter = "DefaultDataCenter"
    cluster = "default"
    group = "SEATA_GROUP"
    addressWaitTime = "3000"
  }
  file {name = "file.conf"}
}

config {
  # file、nacos、apollo、zk、consul、etcd3
  type = "nacos"

  nacos {
    serverAddr = "you ip:8848"
    namespace = ""group ="SEATA_GROUP"username =""
    password = ""dataId ="seataServer.properties"
  }
  consul {
    serverAddr = "127.0.0.1:8500"
    aclToken = ""
  }
  apollo {
    appId = "seata-server"
    ## apolloConfigService will cover apolloMeta
    apolloMeta = "http://192.168.1.204:8801"
    apolloConfigService = "http://192.168.1.204:8080"
    namespace = "application"
    apolloAccesskeySecret = ""cluster ="seata"
  }
  zk {
    serverAddr = "127.0.0.1:2181"
    sessionTimeout = 6000
    connectTimeout = 2000
    username = ""password =""
    nodePath = "/seata/seata.properties"
  }
  etcd3 {serverAddr = "http://localhost:2379"}
  file {name = "file.conf"}
}

客户端配置:

server.port=81
server.servlet.context-path=/demo3

spring.application.name=demo3
spring.datasource.druid.url=jdbc:mysql://you ip:3306/guangdayinhang?useUnicode=true&allowMultiQueries=true&useSSL=false
spring.datasource.druid.username=you username
spring.datasource.druid.password=you password
spring.datasource.druid.driver-class-name=com.mysql.jdbc.Driver

spring.cloud.nacos.discovery.server-addr=you ip 8848
spring.cloud.nacos.discovery.namespace=afb61b93-2515-45ab-aae6-6bc2b105437a

seata.enabled=true
seata.application-id=seata-server
seata.tx-service-group=demo3-group
seata.service.vgroup-mapping.demo3-group=default

seata.registry.type=nacos
seata.registry.nacos.server-addr=you ip:8848
seata.registry.nacos.group=SEATA_GROUP

seata.config.type=nacos
seata.config.nacos.server-addr=you ip:8848
seata.config.nacos.group=SEATA_GROUP

参考资料:
seata 官网
seata github

正文完
 0