nacos官网:
https://nacos.io/zh-cn/docs/what-is-nacos.html
问题nacos版本:2.0.3
https://github.com/alibaba/nacos/issues/5343
原因:由于之前使用的nacos版本是1.4.x,会出k8s中的服务启动后挂掉然后又被拉起又挂掉这种奇葩的问题,后面领导升级到2.0.3之后就出了好几次线上的问题,都是用于部署在k8s中的nacos集群的节点重启之后导致pod的ip改变了,nacos2.x的JRaft选主失败了,导致nacos脑裂成多个个集群,集群数据不同步,最后应用服务发现不了服务,业务系统就挂了一上午或者一下午,临时的解决办法就是重新启动nacos集群和k8s集群中报错的服务。
官方已经关闭了这个两个issues,1.4.x和2.0.x都有这个问题。
官方的修复思路:
下面这位大佬提交了一段代码,被官方合并到最新的分支上去了,看说明就知道这个是一个极限的操作,降低集群失败的几率
/**
* resetPeers. ## nacos-enhance ##.
* 只有在非常紧急并且可用性更为重要的情况下使用:https://www.bookstack.cn/read/sofa-jraft/3.md#6.3%20多数节点故障
*/
RESET_PEERS(JRaftConstants.RESET_PEERS) {
@Override
public RestResult<String> execute(CliService cliService, String groupId, Node node, Map<String, String> args) {
final Configuration newConf = new Configuration();
String peers = args.get(JRaftConstants.COMMAND_VALUE);
for (String peer : peers.split(",")) {
newConf.addPeer(PeerId.parsePeer(peer.trim()));
}
final PeerId nodePeerId = node.getNodeId().getPeerId();
Status status = cliService.resetPeer(groupId, nodePeerId, newConf);
if (status.isOk()) {
return RestResultUtils.success();
}
return RestResultUtils.failed(status.getErrorMsg());
}
};
由此办法只有一个就是升级nacos的版本,这个是改动最小的,如果把nacos换成zk/cousle或者其它注册中心的话,那么这种代价是非常的大,学习成本和运维成本都将增大,项目上的改动来说是非常的大。
首先要参看nacos官方版本对应关系,不然又是一堆的坑
https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E
上面SpringCloudAlibaba2.2.9. 2.2.9.RELEASE 对应的Dubbo的版本没有写,我还纳闷了,在项目demo中试了好久,是支持的,之前是一个dubbo的包没有写版本pom依赖导不会自动拉取这个包导致的maven报错。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.dygroupId>
<artifactId>dubbo-demo-producerartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>dubbo-demo-producername>
<description>dubbo-demo-producerdescription>
<packaging>pompackaging>
<modules>
<module>producer-apimodule>
<module>producer-servicemodule>
modules>
<repositories>
<repository>
<id>nexusid>
<url>xxxxxxxxurl>
<releases>
<enabled>trueenabled>
<updatePolicy>alwaysupdatePolicy>
releases>
<snapshots>
<enabled>trueenabled>
<updatePolicy>alwaysupdatePolicy>
snapshots>
repository>
repositories>
<distributionManagement>
<repository>
<id>nexus-snapshotsid>
<name>Nexus snapshotsname>
<url>xxxxxxurl>
repository>
<snapshotRepository>
<id>nexus-snapshotsid>
<url>xxxxxxxurl>
snapshotRepository>
distributionManagement>
<properties>
<java.version>1.8java.version>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<spring.boot.version>2.3.12.RELEASEspring.boot.version>
<spring-cloud.version>Hoxton.SR12spring-cloud.version>
<spring-cloud-alibaba.version>2.2.9.RELEASEspring-cloud-alibaba.version>
<dubbo.version>2.7.15dubbo.version>
<mybatisplus-version>3.4.2mybatisplus-version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>${spring.boot.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>${spring-cloud-alibaba.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>${mybatisplus-version}version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>${dubbo.version}version>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-surefire-pluginartifactId>
<version>2.22.2version>
<configuration>
<skipTests>trueskipTests>
configuration>
plugin>
<plugin>
<artifactId>maven-source-pluginartifactId>
<version>3.0.1version>
<configuration>
<attach>trueattach>
configuration>
<executions>
<execution>
<phase>compilephase>
<goals>
<goal>jargoal>
goals>
execution>
executions>
plugin>
plugins>
build>
project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.dygroupId>
<artifactId>dubbo-demo-consumerartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>dubbo-demo-consumername>
<description>dubbo-demo-consumerdescription>
<packaging>pompackaging>
<modules>
<module>consumer-apimodule>
<module>consumer-servicemodule>
modules>
<repositories>
<repository>
<id>nexusid>
<url>xxxxxurl>
<releases>
<enabled>trueenabled>
<updatePolicy>alwaysupdatePolicy>
releases>
<snapshots>
<enabled>trueenabled>
<updatePolicy>alwaysupdatePolicy>
snapshots>
repository>
repositories>
<distributionManagement>
<repository>
<id>nexus-snapshotsid>
<name>Nexus snapshotsname>
<url>xxxxxxurl>
repository>
<snapshotRepository>
<id>nexus-snapshotsid>
<url>xxxxxxxxurl>
snapshotRepository>
distributionManagement>
<properties>
<java.version>1.8java.version>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<spring.boot.version>2.3.12.RELEASEspring.boot.version>
<spring-cloud.version>Hoxton.SR12spring-cloud.version>
<spring-cloud-alibaba.version>2.2.9.RELEASEspring-cloud-alibaba.version>
<dubbo.version>2.7.15dubbo.version>
<mybatisplus-version>3.4.2mybatisplus-version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>${spring.boot.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>${spring-cloud-alibaba.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>${mybatisplus-version}version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>${dubbo.version}version>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-surefire-pluginartifactId>
<version>2.22.2version>
<configuration>
<skipTests>trueskipTests>
configuration>
plugin>
<plugin>
<artifactId>maven-source-pluginartifactId>
<version>3.0.1version>
<configuration>
<attach>trueattach>
configuration>
<executions>
<execution>
<phase>compilephase>
<goals>
<goal>jargoal>
goals>
execution>
executions>
plugin>
plugins>
build>
project>
server:
port: 9998
dubbo:
consumer:
check: false
cloud:
subscribed-services: 'dubbo-demo-producer'
scan:
base-packages: com.dy.producer.service.service.dubbo
protocol:
name: dubbo
port: -1
registry:
address: spring-cloud://localhost
application:
version: 1.0.0
spring:
cloud:
config:
#相同配置 本地优先
override-none: true
redis:
host: xxxxxx
port: 6379
pass: xxxxx
database: 5
jedis:
pool:
max-active: 200
max-idle: 20
max-wait: 2000
min-idle: 5
servlet:
multipart:
max-file-size: 50MB
max-request-size: 50MB
management:
health:
rabbit:
enabled: false
logger:
level:
root: info
oos:
endpoint: xxxxx
accessKeyId: xxxxxxx
accessKeySecret: xxxxxxx
bucketName: xxxxxx
server:
port: 9999
dubbo:
consumer:
check: false
cloud:
subscribed-services: 'dubbo-demo-producer'
scan:
base-packages: com.dy.consumer.service.service.dubbo
protocol:
name: dubbo
port: -1
registry:
address: spring-cloud://localhost
application:
version: 1.0.0
spring:
cloud:
config:
#相同配置 本地优先
override-none: true
redis:
host: xxxxx
port: 6379
pass: xxxxxx
database: 5
jedis:
pool:
max-active: 200
max-idle: 20
max-wait: 2000
min-idle: 5
servlet:
multipart:
max-file-size: 50MB
max-request-size: 50MB
management:
health:
rabbit:
enabled: false
logger:
level:
root: info
oos:
endpoint: xxxxxxxxxxx
accessKeyId: xxxxxxxx
accessKeySecret: xxxxxxxxx
bucketName: xxxxxxx
链接:https://pan.baidu.com/s/1EzG8aasLOVRzbeO1JDAWEg
提取码:z6mm
https://github.com/alibaba/nacos/releases
-Xmx1024m -XX:MetaspaceSize=1024m -XX:MaxMetaspaceSize=1024m -Xss2m -Dmaven.test.skip=true -Dmaven.compile.fork=true
console模块的配置
# nacos.core.member.lookup.type=[file,address-server]
nacos.core.member.lookup.type=file
distribution中有nacos的mysql库文件要事先导入自己的mysql库中
单机启动参数
-Dnacos.standalone=true
这个几个目录是各个节点启动的家目录的位置,集群启动的时候从cluster.conf里面读取各自节点的ip和端口信息然后用来组建集群
启动参数
-Dserver.port=8848 -Dnacos.home=D:\Downloads\n8848
-Dserver.port=8847 -Dnacos.home=D:\Downloads\n8847
-Dserver.port=8846 -Dnacos.home=D:\Downloads\n8846
然后同时点击配置好的Nacos8848/Nacos8847/Nacos8846的debug启动启动集群,最后启动后集群是可以正常使用,但是有个很奇怪的问题就是,第二个几点启动会失败,只用两个节点启动起来了,报错绑定ip绑定失败了,不知道咋个搞的,估计是个bug,要不要给官方提个bug?哈哈
https://nacos.io/zh-cn/docs/quick-start-docker.html
参考官网,先clone项目
git clone https://github.com/nacos-group/nacos-docker.git
cd nacos-docker
然后进入到build目录下执行:xxx是版本号
docker build -t nacos/nacos-server-xxxx .
这个构建千万不要在windows环境下安装的docker环境里构建,否则,构建好的镜像会报找不到sh启动脚本的错误,所以要在Linux环境下构建,最好是centOs上,其它Linux也行。
如果不想构建可以去docker官网搜索,就是会很慢:
docker search nacos/nacos-server:v2.1.0
docker pull nacos/nacos-server:v2.1.0
我为大家准备了两个镜像,可以直接拉取使用,并且拉取速度很快:
registry.cn-hangzhou.aliyuncs.com/bigfei/zlf:nacos-server2.1.1
registry.cn-hangzhou.aliyuncs.com/bigfei/zlf:nacos-server2.1.0
启动命令:
# nacos-server2.1.0
docker run -d --name nacos2.1.0 -e MYSQL_SERVICE_HOST=xxxxx -e MYSQL_SERVICE_PORT=3306 -e MYSQL_SERVICE_USER=root -e MYSQL_SERVICE_PASSWORD=xxx!@#xxx -e MYSQL_SERVICE_DB_NAME=nacos -p 8848:8848 -p 9848:9848 -p 9849:9849 registry.cn-hangzhou.aliyuncs.com/bigfei/zlf:nacos-server2.1.0
#nacos-server2.1.1
docker run -d --name nacos2.1.0 -e MYSQL_SERVICE_HOST=xxxxx -e MYSQL_SERVICE_PORT=3306 -e MYSQL_SERVICE_USER=root -e MYSQL_SERVICE_PASSWORD=xxx!@#xxx -e MYSQL_SERVICE_DB_NAME=nacos -p 8848:8848 -p 9848:9848 -p 9849:9849 registry.cn-hangzhou.aliyuncs.com/bigfei/zlf:nacos-server2.1.1
在云服务上启动MYSQL_SERVICE_PASSWORD=xxx!@#xxx数据库的密码含有特殊字符需要转义处理:
MYSQL_SERVICE_PASSWORD=xxx\!\@\#xxx
不然执行不成功,在windows环境中可以不用转义可以正常运行。
https://nacos.io/zh-cn/docs/quick-start-docker.html
官网docker章节有如下启动配置参数可以给我们配置,参考配置即可:
属性名称 | 描述 | 选项 |
---|---|---|
MODE | 系统启动方式: 集群/单机 | cluster/standalone默认 cluster |
NACOS_SERVERS | 集群地址 | p1:port1空格ip2:port2 空格ip3:port3 |
PREFER_HOST_MODE | 支持IP还是域名模式 | hostname/ip 默认 ip |
NACOS_SERVER_PORT | Nacos 运行端口 | 默认 8848 |
NACOS_SERVER_IP | 多网卡模式下可以指定IP | |
SPRING_DATASOURCE_PLATFORM | 单机模式下支持MYSQL数据库 | mysql / 空 默认:空 |
MYSQL_SERVICE_HOST | 数据库 连接地址 | |
MYSQL_SERVICE_PORT | 数据库端口 | 默认 : 3306 |
MYSQL_SERVICE_DB_NAME | 数据库库名 | |
MYSQL_SERVICE_USER | 数据库用户名 | |
MYSQL_SERVICE_PASSWORD | 数据库用户密码 | |
MYSQL_SERVICE_DB_PARAM | 数据库连接参数 | default : characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false |
MYSQL_DATABASE_NUM | 数据库编号 | 默认 :1 |
JVM_XMS | -Xms | 默认 :1g |
JVM_XMX | -Xmx | 默认 :1g |
JVM_XMN | -Xmn | 默认 :512m |
JVM_MS | -XX:MetaspaceSize | 默认 :128m |
JVM_MMS | -XX:MaxMetaspaceSize | 默认 :320m |
NACOS_DEBUG | 是否开启远程DEBUG | y/n 默认 :n |
TOMCAT_ACCESSLOG_ENABLED | server.tomcat.accesslog.enabled | 默认 :false |
NACOS_AUTH_SYSTEM_TYPE | 权限系统类型选择,目前只支持nacos类型 | 默认 :nacos |
NACOS_AUTH_ENABLE | 是否开启权限系统 | 默认 :false |
NACOS_AUTH_TOKEN_EXPIRE_SECONDS | token 失效时间 | 默认 :18000 |
NACOS_AUTH_TOKEN | token | 默认 :SecretKey012345678901234567890123456789012345678901234567890123456789 |
NACOS_AUTH_CACHE_ENABLE | 权限缓存开关 ,开启后权限缓存的更新默认有15秒的延迟 | 默认 : false |
MEMBER_LIST | 通过环境变量的方式设置集群地址 | 例子:192.168.16.101:8847?raft_port=8807,192.168.16.101?raft_port=8808,192.168.16.101:8849?raft_port=8809 |
EMBEDDED_STORAGE | 是否开启集群嵌入式存储模式 | embedded 默认 : none |
NACOS_AUTH_CACHE_ENABLE | nacos.core.auth.caching.enabled | default : false |
NACOS_AUTH_USER_AGENT_AUTH_WHITE_ENABLE | nacos.core.auth.enable.userAgentAuthWhite | default : false |
NACOS_AUTH_IDENTITY_KEY | nacos.core.auth.server.identity.key | default : serverIdentity |
NACOS_AUTH_IDENTITY_VALUE | nacos.core.auth.server.identity.value | default : security |
NACOS_SECURITY_IGNORE_URLS | nacos.security.ignore.urls | default : /,/error,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/v1/auth/**,/v1/console/health/**,/actuator/**,/v1/console/server/** |
k8s相关的可以参考k8s章节
https://nacos.io/zh-cn/docs/use-nacos-with-kubernetes.html