docker-compose安装部署seata-server(nacos做注册、配置中心)

安装好nacos

1.启动一个seata的临时容器,提取配置文件备用

  • 用-rm启动一个seata的临时容器,方便提取配置文件,stop的时候容器也会被删除。
docker run --rm --name seata-server -d -p 8091:8091 seataio/seata-server:1.4.0 
  • 提取配置文件到宿主机
#在宿主机对应路径下创建conf文件夹,放置导出的配置文件
docker cp [容器id]:/seata-server/resources/* /data/seata-server/conf
#复制完即可stop停止容器,自动删除掉

2.修改registry.conf文件,nacos做注册中心、配置中心

  • 修改registry.conf文件,使用nacos作为seata server端的注册配置中心

registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  #  将seata服务器注册到nacos 
  type = "nacos"

  # nacos注册配置
 nacos {
    # nacos服务的显示名称(seata-server是一个java服务)
    application = "seata-server"
    # Nacos服务器的注册地址
    serverAddr = "192.168.0.144:8848"
    # Nacos的服务组
    group = "SEATA_GROUP"
    namespace = ""
    cluster = "default"
    username = "nacos"
    password = "nacos"
  }
}

# seata-server的配置信息,seata-server服务会读取配置
config {
  # file、nacos 、apollo、zk、consul、etcd3
  # 配置方式为Nacos,也就是seata服务的配置信息会从Nacos动态读取
  type = "nacos"

  # Nacos配置服务的信息
  nacos {
    # 配置服务器地址
    serverAddr = "192.168.0.144:8848"
    namespace = ""
    group = "SEATA_GROUP"
    username = ""
    password = ""
    # 配置信息的data-id
    dataId = "seata-server.properties"
  }
}

3.配置docker-compose文件

version: '3'
services:
  seata-server:
    restart: always
    image: seataio/seata-server:1.4.0
    environment:
     #seata-server启动的IP,用于向注册中心注册使用,默认使用容器IP可能无法与客户端通讯;
     #此ip后续连过来的client需要用到,也就是上面第2点中client端连接的seata服务的配置信息。
     #我这里配置的是seata所在的docker容器之外的linux虚拟机ip
     - SEATA_IP=192.168.0.125
     - SEATA_CONFIG_NAME=file:/root/seata-config/registry
    volumes:
      - /data/seataServer/conf:/root/seata-config
      - /data/seataServer/logs:/root/logs
	  - /data/seataServer/mysql-connector-java-8.0.29.jar:/
    ports:
      - "8091:8091"
    container_name: "seata-server"

4.上传seata配置信息到nacos

service.vgroupMapping.default_tx_group=default
store.mode=db
store.db.datasource=druid
store.db.dbType=mysql
#需要看seata容器内的mysql版本对应驱动版本
store.db.driverClassName=com.mysql.jdbc.Driver #这个是mysql8以下的驱动
#store.db.driverClassName=com.mysql.cj.jdbc.Driver #这个是mysql8的驱动
store.db.url=jdbc:mysql://192.168.0.126:3306/seata?useUnicode=true
store.db.user=root
store.db.password=root
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000
#默认config.txt在nacos-config.sh的上一层文件夹即可,也可修改sh内的路径
  • 运行nacos-config.sh脚本上传配置到nacos
#sh命令无法识别"[[]]"表达式,所以使用bash
#参考https://github.com/seata/seata/blob/develop/script/config-center/README.md编写此命令
bash ./nacos-config.sh -h 192.168.0.144 -p 8848 -g SEATA_GROUP  -u nacos -w nacos

5. 首先,需要将 nacos-client 的 Maven 依赖添加到您的项目 pom.xml 文件中

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
            <version>2021.0.1.0</version>
            <exclusions>
                <!-- 要与seata服务端版本一直,所以把自带的替换掉 -->
                <exclusion>
                    <groupId>io.seata</groupId>
                    <artifactId>seata-spring-boot-starter</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.seata</groupId>
            <artifactId>seata-spring-boot-starter</artifactId>
            <version>1.4.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-discovery -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2021.0.1.0</version>
        </dependency>

6. 修改application.yml 文件

seata:
  enabled: true
  enable-auto-data-source-proxy: true #是否开启数据源自动代理,默认为true
  tx-service-group: default_tx_group  #要与配置文件中的vgroupMapping一致
  registry: #registry根据seata服务端的registry配置
    type: nacos #默认为file
    nacos:
      application: seata-server
      server-addr: 192.168.0.144:8848
      group: "SEATA_GROUP"
      namespace: ""
      username: "nacos"
      password: "nacos"
      cluster: default # 配置自己的seata服务cluster, 默认为 default
  config:
    type: nacos #默认file,如果使用file不配置下面的nacos,直接配置seata.service
    nacos:
      server-addr: 192.168.0.144:8848
      group: "SEATA_GROUP"
      namespace: ""
      username: "nacos"
      password: "nacos"
      dataId: seataServer.properties #配置自己的dataId,由于搭建服务端时把客户端的配置也写在了seataServer.properties,所以这里用了和服务端一样的配置文件,实际客户端和服务端的配置文件分离出来更好
  • 需要与registry.conf文件一一对应,否则会出现
io.seata.common.exception.FrameworkException: No available service

7.在启动类上添加注解@EnableAutoDataSourceProxy

@EnableAutoDataSourceProxy

版权声明:本文为liwwnwen原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。