知乎专栏 |
当 Maven 引入了 nacos 依赖,启动就会要求配置 Nacos,可以通过下面方法禁用 Nacos
spring.cloud.nacos.config.enabled=false spring.cloud.nacos.discovery.enabled=false spring.cloud.nacos.config.refresh-enabled=false spring.cloud.nacos.discovery.instance-enabled=false
有时我们希望只获取配置,并不希望服务注册
register-enabled: false
spring: profiles: active: dev application: name: netkiller main: allow-bean-definition-overriding: true cloud: nacos: username: dev password: passw0rd config: server-addr: http://${spring.profiles.active}.netkiller.cn:8848 file-extension: yaml enabled: true namespace: ${spring.profiles.active} enable-remote-sync-config: true discovery: server-addr: http://${spring.profiles.active}.netkiller.cn:8848 namespace: ${spring.profiles.active} register-enabled: true
环境 Docker 安装 nacos 提示 server.tomcat.basedir 错误
nacos | *************************** nacos | APPLICATION FAILED TO START nacos | *************************** nacos | nacos | Description: nacos | nacos | Failed to bind properties under 'server.tomcat.basedir' to java.io.File: nacos | nacos | Property: server.tomcat.basedir nacos | Value: nacos | Origin: InputStream resource [resource loaded through InputStream] - 34:0 nacos | Reason: failed to convert java.lang.String to java.io.File (caused by java.lang.IllegalStateException: Could not retrieve file for class path resource []: class path resource [] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/classes!/) nacos | nacos | Action: nacos | nacos | Update your application's configuration
解决方案
进入容器复制 application.properties 文件到本地,修改 server.tomcat.basedir= 配置,改为 server.tomcat.basedir=. 然后在挂载到容器卷中。
[root@cloud ops.sfzito.com]# docker run -it --entrypoint sh nacos/nacos-server sh-4.2# cat /home/nacos/conf/application.properties # spring server.servlet.contextPath=${SERVER_SERVLET_CONTEXTPATH:/nacos} server.contextPath=/nacos server.port=${NACOS_APPLICATION_PORT:8848} spring.datasource.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.url.1=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=${MYSQL_SERVICE_USER} db.password=${MYSQL_SERVICE_PASSWORD} ### 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:SecretKey012345678901234567890123456789012345678901234567890123456789} ### 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:serverIdentity} nacos.core.auth.server.identity.value=${NACOS_AUTH_IDENTITY_VALUE:security} server.tomcat.accesslog.enabled=${TOMCAT_ACCESSLOG_ENABLED:false} server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D # default current work dir server.tomcat.basedir= ## 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 是一个独立项目,并不依赖 Spring Cloud,如果只是想使用配置中心,在 Springboot 中引入 Nacos 依赖,你会发现 Springboot 不读取 bootstrap.yaml。
解决方法
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> <version>3.1.4</version> </dependency>
出错信息如下,故障是因为每个节点的时间不同步造成的。
[2022-10-14 09:44:51.289 [com.alibaba.nacos.naming.push.receiver] WARN [com.alibaba.nacos.client.naming:177] [,] - out of date data received, old-t: 1665711914993, new-t: 1665711902390
解决方法,安装时间同步软件。例如 NTP
nacos | 09:22:23.431 [main] ERROR org.springframework.boot.SpringApplication - Application run failed nacos | com.alibaba.nacos.api.exception.runtime.NacosRuntimeException: ErrCode:500, ErrMsg:User limit of inotify instances reached or too many open files nacos | at com.alibaba.nacos.core.listener.StartingApplicationListener.loadPreProperties(StartingApplicationListener.java:161) nacos | at com.alibaba.nacos.core.listener.StartingApplicationListener.environmentPrepared(StartingApplicationListener.java:100) nacos | at com.alibaba.nacos.core.code.SpringApplicationRunListener.environmentPrepared(SpringApplicationRunListener.java:60) nacos | at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:66) nacos | at java.util.ArrayList.forEach(ArrayList.java:1259) nacos | at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:120) nacos | at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:114) nacos | at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:65) nacos | at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:343) nacos | at org.springframework.boot.SpringApplication.run(SpringApplication.java:301) nacos | at org.springframework.boot.SpringApplication.run(SpringApplication.java:1317) nacos | at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) nacos | at com.alibaba.nacos.Nacos.main(Nacos.java:35) nacos | at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) nacos | at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) nacos | at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) nacos | at java.lang.reflect.Method.invoke(Method.java:498) nacos | at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) nacos | at org.springframework.boot.loader.Launcher.launch(Launcher.java:108) nacos | at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) nacos | at org.springframework.boot.loader.PropertiesLauncher.main(PropertiesLauncher.java:467) nacos | Caused by: com.alibaba.nacos.api.exception.NacosException: java.io.IOException: User limit of inotify instances reached or too many open files nacos | at com.alibaba.nacos.sys.file.WatchFileCenter$WatchDirJob.<init>(WatchFileCenter.java:184) nacos | at com.alibaba.nacos.sys.file.WatchFileCenter.registerWatcher(WatchFileCenter.java:92) nacos | at com.alibaba.nacos.core.listener.StartingApplicationListener.registerWatcher(StartingApplicationListener.java:167) nacos | at com.alibaba.nacos.core.listener.StartingApplicationListener.loadPreProperties(StartingApplicationListener.java:159) nacos | ... 20 common frames omitted nacos | Caused by: java.io.IOException: User limit of inotify instances reached or too many open files nacos | at sun.nio.fs.LinuxWatchService.<init>(LinuxWatchService.java:64) nacos | at sun.nio.fs.LinuxFileSystem.newWatchService(LinuxFileSystem.java:47) nacos | at com.alibaba.nacos.sys.file.WatchFileCenter$WatchDirJob.<init>(WatchFileCenter.java:179) nacos | ... 23 common frames omitted nacos | 09:22:23.435 [Thread-4] WARN com.alibaba.nacos.common.executor.ThreadPoolManager - [ThreadPoolManager] Start destroying ThreadPool nacos | 09:22:23.435 [Thread-4] WARN com.alibaba.nacos.common.executor.ThreadPoolManager - [ThreadPoolManager] Destruction of the end
加大 fs.inotify.max_user_instances 配置即可,默认是 128
sysctl fs.inotify.max_user_watches sudo sysctl -w fs.inotify.max_user_watches=50889300 sysctl fs.inotify.max_user_instances sudo sysctl -w fs.inotify.max_user_instances=65535
当Nacos开启了认证配置nacos.core.auth.enabled=true时,当前账号没有该命名空间的权限,就会出现下面的错误提示。
2022-11-07 18:42:04,370 [,,,] INFO com.alibaba.nacos.client.config.impl.LocalConfigInfoProcessor:212 [main] - LOCAL_SNAPSHOT_PATH:/root/nacos/config Mon, Nov 7 2022 6:42:04 pm 2022-11-07 18:42:04,393 [,,,] ERROR com.alibaba.nacos.client.config.impl.ClientWorker:304 [main] - [fixed-nacos.default.svc.cluster.local_8848-test] [sub-server-error] no right, dataId=netkiller, group=DEFAULT_GROUP, tenant=test Mon, Nov 7 2022 6:42:04 pm 2022-11-07 18:42:04,393 [,,,] ERROR com.alibaba.cloud.nacos.client.NacosPropertySourceBuilder:104 [main] - get data from Nacos error,dataId:netkiller Mon, Nov 7 2022 6:42:04 pm com.alibaba.nacos.api.exception.NacosException: <html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Mon Nov 07 18:42:04 CST 2022</div><div>There was an unexpected error (type=Forbidden, status=403).</div></body></html>
解决方案,在权限控制->权限管理中「添加权限」可以解决。
spring: cloud: nacos: discovery: server-addr: 127.0.0.1:8848 metadata: preserved.heart.beat.interval: 1000 # 客户端上报心跳的间隔时间。(单位:毫秒) preserved.heart.beat.timeout: 3000 # 不发送心跳后,从健康到不健康的时间。(单位:毫秒) preserved.ip.delete.timeout: 3000 # 不发送心跳后,被nacos下掉该实例的时间。(单位:毫秒)
Spring cloud 网关 Gateway 的 ribbion 配置
#ribbon config,Interval to refresh the server list from the source ribbon: ServerListRefreshInterval: 3000
最终 Openfeign 获得注册服务的时间是 Nacos + Gateway = 6ms