Seata使用nacos作为配置与注册中心

6/14/2023 微服务

# 前述

截止2023-06-14seata最新版1.6.1nacos最新版是2.2.3seata使用nacos作为注册中心以及配置中心的步骤同样发生了变化,所以此篇便是记录最新版seatanacos的整合。

# nacos配置

nacos的配置变化不是很大,主要是鉴权 (opens new window),下面直接给出新的有关配置,更多身份认证配置可以参考官方文档。

### If turn on auth system:
nacos.core.auth.enabled=true
### Since 1.4.1, worked when nacos.core.auth.enabled=true and nacos.core.auth.enable.userAgentAuthWhite=false.
### The two properties is the white list for auth and used by identity the request from other server.
nacos.core.auth.server.identity.key=authKey
nacos.core.auth.server.identity.value=nacosSecurty

### worked when nacos.core.auth.system.type=nacos
### The token expiration in seconds:
nacos.core.auth.plugin.nacos.token.cache.enable=false
nacos.core.auth.plugin.nacos.token.expire.seconds=18000
### The default token (Base64 String):
nacos.core.auth.plugin.nacos.token.secret.key=N2VqdElyVDhjdk04ZTdtZUE1eFIxMTJ1ZGctNzZ5e4Rk
1
2
3
4
5
6
7
8
9
10
11
12
13

# seata配置

# yml配置

  • seata 新版本将原先file.conf以及registry.conf去除,只需要配置conf下的application.yml文件
# seata可视化界面端口
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

# seata可视化web界面账号密码
console:
  user:
    username: seata
    password: seata

seata:
  # 配置中心
  config:
    # support: nacos, consul, apollo, zk, etcd3
    type: nacos  # 指定配置中心为nacos
    nacos:
      server-addr: 127.0.0.1:8848
      group : SEATA_GROUP
      namespace: f2b783fb-cd26-474b-aba2-d28e2a6fd657
      username: nacos
      password: nacos
      # nacos中存放seata的配置文件
      dataId: seataServer.properties
  # 注册中心
  registry:
    # support: nacos, eureka, redis, zk, consul, etcd3, sofa
    type: nacos
    nacos:
      application: seata-server
      server-addr: 127.0.0.1:8848
      group: SEATA_GROUP
      namespace: f2b783fb-cd26-474b-aba2-d28e2a6fd657
      cluster: default
      username: nacos
      password: nacos
      context-path:
      ##if use MSE Nacos with auth, mutex with username/password attribute
      #access-key:
      #secret-key:
  store:
    # support: file 、 db 、 redis
    mode: db
  # 默认配置,根据实际情况修改
  security:
    secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
    tokenValidityInMilliseconds: 1800000
    ignore:
      urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

# dataId配置

  • 上面配置中,seata.config.nacos中,有一个dataId配置,这是从v1.4.2版本开始,已支持从一个Nacos dataId中获取所有配置信息。

  • 更多信息请查看官网:Nacos 配置中心 (opens new window)

  • nacos中新建seataseataServer.properties配置文件

seata17

seata17

seata17

注意点

  • 1、修改service.vgroupMapping.mygroup=default该值,作用同(nacos作为Seata的配置中心)[seata_nacos.md]
  • 2、修改store.mode store.lock.mode store.session.mode这三个值为db,才能让seata连接到数据库中。
  • 3、修改store.db配置项下的配置,连接到自己的数据库。
  • 4、该配置源文件存放在seata目录下:seata/script/config-center/config.txt

# mysql配置

新建seata数据库,执行seata目录下sql文件即可:seata1.6.1/script/server/db/mysql.sql

# 启动

  • 优先将nacos启动,确保nacos启动成功
  • nacos启动完成后,在seatabin 目录下执行脚本
sh seata-server.sh -p 8091 -h 127.0.0.1
1

参数解释

  • -h:将地址暴露给注册中心,其他服务可以访问seata服务器

  • -p:要侦听的端口

  • 启动完成后,访问http://127.0.0.1:7091/#/login即可进入seata的可视化页面

seata17

seata17