Nacos, seata 1.5.2 installation integration and error message, Exception in thread “main” java.lang.reflect.InvocationTargetException.

seata download website download center (seata.io)

The version I installed this time is seata.1.5.2 because the version of spring-cloud used in my project is 2021.0.4. I originally wanted to use seata1.6.1, but after my search, it is not compatible with the version I am currently using. My version is:

<spring-cloud.version>2021.0.4</spring-cloud.version>
<spring-cloud-alibaba.version>2021.0.4.0</spring-cloud-alibaba.version>

After downloading, I uploaded it to the /usr/local/project via XFTP,

Use the name command tar -zxvf to decompress. After decompression, you will get a seata directory. Go to /usr/local/seata/conf to modify application.xml. Here I modify the default namespace. Before modifying this, first create a name in NACOS space,

After creation, you will get the ID of the namespace. The difference from the previous version is that seata1.5.2 can set the data-id in the config

The previous version needs to execute the script to publish the config.txt to nacos.

The data-id here can replace the previous script, as shown in the figure below, we can see data-id:seataServer.properties

Here we are looking at nacos, which is defined in the namespace of seata152.

seataServer.properties, only part of the code is added here.

#Transaction routing rules configuration, only for the client
service.vgroupMapping.zhny-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 storage configuration, only for the server. The file, DB, and redis configuration values are optional.
store.mode=db
store.lock.mode=db
store.session.mode=db
#Used for password encryption
store.publicKey=


store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.cj.jdbc.Driver
store.db.url=jdbc:mysql://127.0.0.1:3306/zhny_seata?useUnicode=true & rewriteBatchedStatements=true
store.db.user=root
store.db.password=123456
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

The group transaction here is named zhny-tx-group

Then there is the yml of the client in nacos, the tx-service-group inside: zhny-tx-group and

vgroup-mapping:

zhny-tx-group: default

The names of these two places must be consistent, because if there are two clients here, they belong to the same group of transactions.

# spring configuration
spring:
  redis:
    host: localhost
    port: 6379
    password:
  datasource:
    druid:
      stat-view-servlet:
        enabled: true
        loginUsername: admin
        loginPassword: 123456
    dynamic:
      druid:
        initial-size: 5
        min-idle: 5
        maxActive: 20
        maxWait: 60000
        timeBetweenEvictionRunsMillis: 60000
        minEvictableIdleTimeMillis: 300000
        validationQuery: SELECT 1 FROM DUAL
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        poolPreparedStatements: true
        maxPoolPreparedStatementPerConnectionSize: 20
        filters: stat, slf4j
        connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
      datasource:
          # Main database data source
          master:
            driver-class-name: com.mysql.cj.jdbc.Driver
            url: jdbc:mysql://127.0.01:3306/zhny_cloud?useUnicode=true &characterEncoding=utf8 &zeroDateTimeBehavior=convertToNull &useSSL=true &allowMultiQueries=true &serverTimezone=GMT+ 8
            username: root
            password: 123456
          # From the database data source
          # slave:
            # username:
            # password:
            #url:
            # driver-class-name:
      seata: true


# seata configuration
seata:
  enabled: true
  # Seata application ID, defaults to ${spring.application.name}
  application-id: zhny-basic
  # Seata transaction group number, used for TC cluster name
  tx-service-group: zhny-tx-group
  # Turn off autoproxy
  enable-auto-data-source-proxy: false
  # service configuration item
  service:
    # Mapping of virtual groups and groups
    vgroup-mapping:
      zhny-tx-group: default
    # Mapping of groups and Seata services
    grouplist:
      default: 127.0.0.1:8091
  config:
    # support: nacos, consul, apollo, zk, etcd3
    type: nacos
    nacos:
      server-addr: 127.0.0.1:8848
      namespace: 84d5f86a-7581-4f3b-af2e-82992cf0a31c
      group: SEATA_GROUP
      username: nacos
      password: nacos
      context-path:
      ##if use MSE Nacos with auth, mutex with username/password attribute
      #access-key:
      #secret-key:
      data-id: 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: 84d5f86a-7581-4f3b-af2e-82992cf0a31c
      cluster: default
      username: nacos
      password: nacos
  

# mybatis configuration
mybatis:
    # Search for the specified package alias
    typeAliasesPackage: com.zhny.basic.domain
    # Configure mapper scan to find all mapper.xml mapping files
    mapperLocations: classpath:mapper/**/*.xml
    # console print sql
    #configuration:
      #log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

The seata detailed code code is as follows

seat:
  config:
    # support: nacos, consul, apollo, zk, etcd3
    type: nacos
    nacos:
      server-addr: 127.0.0.1:8848
      namespace: 84d5f86a-7581-4f3b-af2e-82992cf0a31c
      group: SEATA_GROUP
      username: nacos
      password: nacos
      context-path:
      ##if use MSE Nacos with auth, mutex with username/password attribute
      #access-key:
      #secret-key:
      data-id: 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: 84d5f86a-7581-4f3b-af2e-82992cf0a31c
      cluster: default
      username: nacos
      password: nacos
  store:
    # support: file, db, redis
    mode: db
    db:
      datasource: druid
      db-type: mysql
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://127.0.0.1:3306/zhny_seata?rewriteBatchedStatements=true
      user: root # mysql user name
      password: 123456 #mysql password
      min-conn: 5
      max-conn: 100
      global-table: global_table
      branch-table: branch_table
      lock-table: lock_table
      distributed-lock-table: distributed_lock
      query-limit: 100
      max-wait: 5000
# 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

After the operation is completed, we can execute sh seata-server.sh under seata, and we can see that the operation is successful.

Then I want seata to perform automatic startup at boot, so I wrote a script.

# Write the service script
vim /lib/systemd/system/seata.service

[Unit]
Description=seat
After=nacos.service

[Service]
Type=forking

ExecStart= /usr/local/seata/bin/seata-server.sh
ExecStop=/usr/local/seata/bin/stop.sh
PrivateTmp=true

[Install]
WantedBy=multi-user. target

According to the script written by others, the shutdown script stop.sh is changed, and the process is killed according to the PID started.

#!/bin/sh
cd /usr/local/seata/bin
PID=$(ps -ef | grep "seata-server" | grep -v grep | awk '{print $2}')
if [ ${PID} ];
then
    echo 'Application is stpping...'
    echo kill $PID DONE
    kill $PID
else
    echo 'Application is already stopped...'
fi

# Reload all service services
systemctl daemon-reload
# boot
systemctl enable seata.service
# Check if the service is enabled
systemctl is-enabled seata.service
# start the service
systemctl start seata.service
# Check the status of the service
systemctl status seata.service

# restart service

systemctl restart seata.service

Executing systemctl start seata.service found that an error was reported. I searched for this problem on the Internet for a day and a half. It is very strange. It can run normally in the bin directory, but it fails when added to the boot self-starting service!

Let’s take a look at the error message Exception in thread “main” java.lang.reflect.InvocationTargetException, we are rookie-level, after searching for a day to solve the information, we finally found a big cow in the blog.

The startup command in seata-server.sh, -Dloader.path=../lib uses relative path,

Only by executing seata-server.sh under the bin/ path of the seata package can the jar package in lib be correctly started

The packaging and startup methods of 1.5.2 have changed and the directory structure is also different, so I prefer his recommended method one

Method 1: Modify the seata-server.sh script (recommended)

vim user/local/seata/bin/seata-server.sh

Find the -Dloader.path=../lib configuration and change it to an absolute path: -Dloader.path=${BASEDIR}/lib

https://blog.csdn.net/weixin_43582081/article/details/125949859

So far, the problem has been solved, and the boot is successful!