nacos从1.x升级到2.4.3问题记录
从dockerhup拉镜像
docker pull nacos/nacos-server:latest
创建宿主机数据目录
##存放数据,日志,配置文件的目录,为什么要挂载,想想如果是mysql
##不挂载持久化数据到宿主机,重启数据都没了,这跟删库有啥区别
mkdir -p /Applications/java_dev_util/dockerData/nacos/logs
mkdir -p /Applications/java_dev_util/dockerData/nacos/data
mkdir -p /Applications/java_dev_util/dockerData/nacos/config
#授权一下,不然没权限往宿主机目录同步数据
chmod o+rwx -R /Applications/java_dev_util/dockerData/nacos/logs
chmod o+rwx -R /Applications/java_dev_util/dockerData/nacos/data
chmod o+rwx -R /Applications/java_dev_util/dockerData/nacos/config
#在config目录下创建application.properties文件(也可以启动容器后从容器中复制(docker cp命令)出来),内容如下:
# spring
server.servlet.contextPath=${SERVER_SERVLET_CONTEXTPATH:/nacos}
server.contextPath=/nacos
server.port=${NACOS_APPLICATION_PORT:8848}
server.tomcat.accesslog.max-days=30
server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D %{User-Agent}i %{Request-Source}i
server.tomcat.accesslog.enabled=${TOMCAT_ACCESSLOG_ENABLED:false}
server.error.include-message=ALWAYS
# default current work dir
server.tomcat.basedir=file:.
#*************** Config Module Related Configurations ***************#
### Deprecated configuration property, it is recommended to use `spring.sql.init.platform` replaced.
#spring.datasource.platform=${SPRING_DATASOURCE_PLATFORM:}
spring.sql.init.platform=${SPRING_DATASOURCE_PLATFORM:}
nacos.cmdb.dumpTaskInterval=3600
nacos.cmdb.eventTaskInterval=10
nacos.cmdb.labelTaskInterval=300
nacos.cmdb.loadDataAtStart=false
db.num=${MYSQL_DATABASE_NUM:1}
db.url.0=jdbc:mysql://${MYSQL_SERVICE_HOST}:${MYSQL_SERVICE_PORT:3306}/${MYSQL_SERVICE_DB_NAME}?${MYSQL_SERVICE_DB_PARAM:characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false}
db.user.0=${MYSQL_SERVICE_USER}
db.password.0=${MYSQL_SERVICE_PASSWORD}
## DB connection pool settings
db.pool.config.connectionTimeout=${DB_POOL_CONNECTION_TIMEOUT:30000}
db.pool.config.validationTimeout=10000
db.pool.config.maximumPoolSize=20
db.pool.config.minimumIdle=2
### The auth system to use, currently only 'nacos' and 'ldap' is supported:
nacos.core.auth.system.type=${NACOS_AUTH_SYSTEM_TYPE:nacos}
### worked when nacos.core.auth.system.type=nacos
### The token expiration in seconds:
nacos.core.auth.plugin.nacos.token.expire.seconds=${NACOS_AUTH_TOKEN_EXPIRE_SECONDS:18000}
### The default token:
nacos.core.auth.plugin.nacos.token.secret.key=${NACOS_AUTH_TOKEN:}
### Turn on/off caching of auth information. By turning on this switch, the update of auth information would have a 15 seconds delay.
nacos.core.auth.caching.enabled=${NACOS_AUTH_CACHE_ENABLE:false}
nacos.core.auth.enable.userAgentAuthWhite=${NACOS_AUTH_USER_AGENT_AUTH_WHITE_ENABLE:false}
nacos.core.auth.server.identity.key=${NACOS_AUTH_IDENTITY_KEY:}
nacos.core.auth.server.identity.value=${NACOS_AUTH_IDENTITY_VALUE:}
## spring security config
### turn off security
nacos.security.ignore.urls=${NACOS_SECURITY_IGNORE_URLS:/,/error,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/v1/auth/**,/v1/console/health/**,/actuator/**,/v1/console/server/**}
# metrics for elastic search
management.metrics.export.elastic.enabled=false
management.metrics.export.influx.enabled=false
nacos.naming.distro.taskDispatchThreadCount=10
nacos.naming.distro.taskDispatchPeriod=200
nacos.naming.distro.batchSyncKeyCount=1000
nacos.naming.distro.initDataRatio=0.9
nacos.naming.distro.syncRetryDelay=5000
nacos.naming.data.warmup=true
nacos.console.ui.enabled=true
nacos.core.param.check.enabled=true
启动容器
docker run -it --name nacos -p 8848:8848 --restart=always -e MODE=standalone \
-v /Applications/java_dev_util/dockerData/nacos/logs:/home/nacos/logs \
-v /Applications/java_dev_util/dockerData/nacos/data:/home/nacos/data \
-v //Applications/java_dev_util/dockerData/nacos/conf/application.properties:/home/nacos/conf/application.properties \
-d nacos/nacos-server:latest
验证是否启动成功,命令行docker logs nacos 或者 localhost:8848/nacos
nacos2.4.3默认不需要密码登录的,如需要开启鉴权,修改application.properties配置文件里面对应的选项即可,如:
nacos.core.auth.enabled=true
nacos.core.auth.caching.enabled=true
nacos.core.auth.enable.userAgentAuthWhite=false
nacos.core.auth.server.identity.key=nacos
nacos.core.auth.server.identity.value=nacos
集成到项目中,这里是在父pom文件里做版本统一管理,同时注意springboot版本要与springcloud版本对应,父pom文件
<modelVersion>4.0.0</modelVersion>
<groupId>com.ming</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<name>ming_parent</name>
<packaging>pom</packaging>
<description>ming_parent,父级pom服务,定义版本和基本的依赖</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.6.13</spring-boot.version>
<spring-cloud.version>2021.0.4.0</spring-cloud.version>
<lombok.version>1.18.26</lombok.version>
<hutool.version>5.8.27</hutool.version>
<mybatis.plus.version>3.5.1</mybatis.plus.version>
<mybatis.generate.version>3.5.1</mybatis.generate.version>
<velocity.version>1.7</velocity.version>
<lombok.version>1.18.26</lombok.version>
<druid.version>1.2.16</druid.version>
<knife4j.version>2.0.8</knife4j.version>
<aliyun.oss.version>3.17.4</aliyun.oss.version>
<mysql.connetctor.version>8.0.31</mysql.connetctor.version>
<commons.lang3.version>3.12.0</commons.lang3.version>
<jwt.version>0.9.1</jwt.version>
<alibaba.json>1.2.78</alibaba.json>
<validation.version>2.6.13</validation.version>
</properties>
<!--子项目继承父pom时同时继承依赖-->
<dependencies>
<!--Nacos Config-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--Nacos Discovery-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--springboot 基本-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!--定义子项目依赖版本,子项目pom依赖时不需要声名版本-->
<dependencyManagement>
<dependencies>
<!--spring.cloud-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--springboot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--Bean校验-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>${validation.version}</version>
</dependency>
<!--MySQL-->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>${mysql.connetctor.version}</version>
</dependency>
<!--hutool-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
<!--MyBatis-Plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis.plus.version}</version>
</dependency>
<!-- MyBatis Generator -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>${mybatis.generate.version}</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>${velocity.version}</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<!--druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
<!--Knife4j文档生成-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>${knife4j.version}</version>
</dependency>
<!--阿里云对象存储-->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>${aliyun.oss.version}</version>
</dependency>
<!--commons-lang3-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons.lang3.version}</version>
</dependency>
<!--jwt-->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>${jwt.version}</version>
</dependency>
<!--json-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${alibaba.json}</version>
</dependency>
</dependencies>
</dependencyManagement>
子项目pom
<modelVersion>4.0.0</modelVersion>
<!--引入父pom定义的依赖版本-->
<parent>
<groupId>com.ming</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.ming</groupId>
<artifactId>content</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ming_content</name>
<description>ming_content</description>
<properties>
<spring-boot-maven-plugin>2.6.13</spring-boot-maven-plugin>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<!--父pom定义了依赖版本,此处就不需要指定了-->
<dependencies>
<!--公共entity-->
<dependency>
<groupId>com.ming</groupId>
<artifactId>entity</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--自定义日志starter-->
<dependency>
<groupId>com.ming</groupId>
<artifactId>log-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!--MyBatis-Plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<!-- MyBatis Generator -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
</dependency>
<!--MySQL-->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<!--Knife4j文档生成-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
</dependency>
<!--hutool-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
<!--jwt-->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
</dependency>
</dependencies>
<build>
<finalName>content</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--要指定版本-->
<version>${spring-boot-maven-plugin}</version>
<configuration>
<mainClass>com.ming.content.MingContentApplication</mainClass>
<layout>JAR</layout>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
application-local.yml文件配置:
server:
port: 9999
application:
name: content
servlet:
context-path: /content
spring:
#解决Failed to start bean 'documentationPluginsBootstrapper'nested exception is java.lang.NullPointerException的错误
mvc:
pathmatch:
matching-strategy: ant_path_matcher
#nacos
cloud:
nacos:
config:
server-addr: localhost:8848
import-check:
enabled: false
discovery:
server-addr: localhost:8848
此时启动项目发现会报错
原因是:nacos 2.x版本,新增了两个端口。这两个端口在nacos原先的端口上(默认8848),进行一定偏移量自动生成。
9848 1000 客户端gRPC请求服务端端口,用于客户端向服务端发起连接和请求
9849 1001 服务端gRPC请求服务端端口,用于服务间同步等
此时只需要在docker启动时同时指定相应的端口即可
#删除正在运行的nacos
docker rm -f nacos
#重启
docker run -it --name nacos -p 8848:8848 -p 9848:9848 -p 9849:9849 --restart=always -e MODE=standalone \
-v /Applications/java_dev_util/dockerData/nacos/logs:/home/nacos/logs \
-v /Applications/java_dev_util/dockerData/nacos/data:/home/nacos/data \
-v //Applications/java_dev_util/dockerData/nacos/conf/application.properties:/home/nacos/conf/application.properties \
现在端口都映射了
再次启动项目,发现报另外一个错:java.lang.IllegalArgumentException: Param ‘serviceName’ is illegal, serviceName is blank
在yml文件中显式指定服务名称
我看网上还有的人说是discovery.service没有设置,按理说这个不用设置,点进去会发现他默认就是取的${spring.application.name},这不知道是不是官方的一个bug,但显式指定后发现跑起来确实没报错,服务也能正常注册发现