springboot+feign+nacos+seata+docker整合踏坑实录

一、版本

springboot:2.7.2

feign:3.1.5

jdk:19

seata:1.5.2

nacos:V2.1.2

二、docker部署

1.配置文件实列

配置说明

下述配置为该当前最新镜像,seata的配置不需要区分成网上所说的registry.conffile等配置文件。

nacosseata的配置文件可先启动容器之后从容器中拿出配置文件再做响应的修改。

docker-compose.yml配置文件:

version: '3'
services:
    two_mysql:
        image: mysql:8.0.29
        container_name: two_mysql
        ports:
            - 3316:3306
        environment:
            MYSQL_ROOT_PASSWORD: root #设置root帐号密码
            TZ: Asia/Shanghai
        volumes:
            - F:\docker-install-path\native_volume\mysql\mysql_data_db:/var/lib/mysql #数据文件挂载
            - F:\docker-install-path\native_volume\mysql\mysql_log:/var/log/mysql #日志文件挂载
            - F:\docker-install-path\native_volume\mysql\mysql_config\my.cnf:/etc/my.cnf
        networks:
            rmq:
                aliases:
                    - two_mysql 
    seata_server:
        image: seataio/seata-server:1.6.0
        container_name: seata_server
        depends_on:
            - nacos_server
        ports:
            - 8091:8091
            - 7091:7091
        volumes:
            # 假设我们通过docker cp命令把资源文件拷贝到相对路径`./seata-server/resources`中
            - F:\docker-install-path\native_volume\seata\resources\:/seata-server/resources/
        networks:
            rmq:
                aliases:
                    - seata_server
    nacos_server:
        image: nacos/nacos-server:v2.1.2
        container_name: nacos_server
        depends_on: 
            - two_mysql
        environment:
            - MODE=standalone
        ports:
            - 8848:8848
            - 9848:9848
            - 9849:9849
        volumes:
            # 假设我们通过docker cp命令把资源文件拷贝到相对路径`./seata-server/resources`中
            - F:\docker-install-path\native_volume\nacos\conf:/home/nacos/conf
            - F:\docker-install-path\native_volume\nacos\data:/home/nacos/data
        networks:
            rmq:
                aliases:    
                    - nacos_server
networks:
    rmq:
        name: rmq
        driver: bridge

nacos挂载出来的配置文件application.properties

server.servlet.contextPath=/nacos
server.port=8848

spring.datasource.platform=mysql
server.error.include-message=ALWAYS

db.num=1

db.url.0=jdbc:mysql://two_mysql:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=root
db.password=root

management.metrics.export.elastic.enabled=false

management.metrics.export.influx.enabled=false

server.tomcat.accesslog.enabled=true

server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D %{User-Agent}i %{Request-Source}i

server.tomcat.basedir=/

nacos.security.ignore.urls=/,/error,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/v1/auth/**,/v1/console/health/**,/actuator/**,/v1/console/server/**

nacos.core.auth.system.type=nacos
# 该配置设置false取消nacos权限验证
nacos.core.auth.enabled=false

nacos.core.auth.enable.userAgentAuthWhite=false

nacos.core.auth.server.identity.key=serverIdentity
nacos.core.auth.server.identity.value=security

nacos.core.auth.default.token.expire.seconds=18000

nacos.core.auth.default.token.secret.key=SecretKey012345678901234567890123456789012345678901234567890123456789

nacos.istio.mcp.server.enabled=false

spring.cloud.nacos.username=nacos
spring.cloud.nacos.password=nacos


seata-server挂载配置文件application.yml

#  Copyright 1999-2019 Seata.io Group.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

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

console:
  user:
    username: seata
    password: seata

seata:
  config:
    # support: nacos, consul, apollo, zk, etcd3
    type: nacos
    nacos:
      server-addr: 192.168.3.29:8848
      namespace: test01
      group: SEATA_GROUP
      username: nacos
      password: nacos
  registry:
    # support: nacos, eureka, redis, zk, consul, etcd3, sofa
    type: nacos
    nacos:
      application: seata-server
      server-addr: 192.168.3.29:8848
      group: SEATA_GROUP
      namespace: test01
      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://two_mysql:3306/seata?useUnicode=true&characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true
      user: root
      password: root
#  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

2.问题记录

1.当用docker-compose启动该实列seata-server启动会失败,原因是因为depends_on配置的编排虽然等待了nacos容器的启动但是nacos实列还未完全启动成功,所以导致seata-server启动失败。

解决方案为重新再次启动一次seata-server

2.docker-compose.yml文件中个镜像启动配置一定配置好container_name

3.seata-serverapplication.yml配置文件注册中心地址一定使用宿主机ip,不要使用docker配置的域名,会导致注册不上去。报**failed to req API:/nacos/v1/ns/instance after all servers([nacos_server:8848]) **错误!

在这里插入图片描述

三、springboot客户端配置

启动异常问题:

1.异常信息: org/springframework/boot/Bootstrapper,缺少spring-cloud-starter-bootstrap包,引入即可;

2.如果是springboot3.0.0版本讲读不到nacos的配置文件所以最好降下springboot的版本;

3.启动 Unable to make field protected java.lang.reflect.InvocationHandler java.lang.reflect.Proxy.h accessible: module java.base does not "opens java.lang.reflect" to unnamed module @3eb738bb Unable to make field protected java.lang.reflect.InvocationHandler java.lang.reflect.Proxy.h accessible: module java.base does not "opens java.lang.reflect" to unnamed module @3eb738bb,需要设置启动参数--add-opens java.base/java.lang.reflect=ALL-UNNAMED如果是报错其它包就换其它包
4.网络不通问题,宿主机无法pingdocker容器中的ip,报错异常信息为can not register RM,err:can not connect to services-server.。解决方案参考http://t.csdn.cn/taJtC

application.yml配置

server:
  port: 1210
spring:
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

  datasource:
    url: jdbc:mysql://localhost:3316/seata-client1?useUnicode=true&characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
  application:
    name: seata-client1
  main:
    allow-circular-references: true
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        group: SEATA_GROUP
        username: nacos
        password: nacos
        namespace: test01

    alibaba:
      seata:
        tx-service-group: default_tx_group
seata:
  application-id: ${spring.application.name}
#  tx-service-group: seataClient1
  enabled: true
  config:
    type: nacos
    nacos:
      server-addr: 127.0.0.1:8848
      group: SEATA_GROUP
      username: nacos
      password: nacos
      namespace: test01
  registry:
    type: nacos
    nacos:
      server-addr: 127.0.0.1:8848
      group: SEATA_GROUP
      username: nacos
      password: nacos
      namespace: test01
logging:
  level:
    root: info

代码仓库地址: https://gitee.com/wdx-7929/seata-test1?_from=gitee_search


版权声明:本文为m0_37902494原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/m0_37902494/article/details/128263975