{"record":{"id":"fb8b4c0f3f16109b","repo":"alibaba/Sentinel","slug":"nacos-config-service-has-not-been-initialized-or-e","errorCode":null,"errorMessage":"Nacos config service has not been initialized or error occurred","messagePattern":"Nacos config service has not been initialized or error occurred","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"sentinel-extension/sentinel-datasource-nacos/src/main/java/com/alibaba/csp/sentinel/datasource/nacos/NacosDataSource.java","lineNumber":139,"sourceCode":"            RecordLog.warn(\"[NacosDataSource] Error when loading initial config\", ex);\n        }\n    }\n\n    private void initNacosListener() {\n        try {\n            this.configService = NacosFactory.createConfigService(this.properties);\n            // Add config listener.\n            configService.addListener(dataId, groupId, configListener);\n        } catch (Exception e) {\n            RecordLog.warn(\"[NacosDataSource] Error occurred when initializing Nacos data source\", e);\n            e.printStackTrace();\n        }\n    }\n\n    @Override\n    public String readSource() throws Exception {\n        if (configService == null) {\n            throw new IllegalStateException(\"Nacos config service has not been initialized or error occurred\");\n        }\n        return configService.getConfig(dataId, groupId, DEFAULT_TIMEOUT);\n    }\n\n    @Override\n    public void close() {\n        if (configService != null) {\n            configService.removeListener(dataId, groupId, configListener);\n            try {\n                configService.shutDown();\n            } catch (Exception e) {\n                RecordLog.warn(\"[NacosDataSource] Error occurred when closing Nacos data source\", e);\n                e.printStackTrace();\n            }\n        }\n        pool.shutdownNow();\n    }\n","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/alibaba/Sentinel/blob/a3f40ba8e900c8489bd520274739f17235a7721c/sentinel-extension/sentinel-datasource-nacos/src/main/java/com/alibaba/csp/sentinel/datasource/nacos/NacosDataSource.java#L121-L157","documentation":"Thrown by NacosDataSource.readSource() when the internal Nacos ConfigService is null. Construction swallows exceptions from NacosFactory.createConfigService (it only logs via RecordLog.warn and printStackTrace), so a bad address/credentials leave configService null; every later read (initial load and timer refresh) then hits this IllegalStateException.","triggerScenarios":"Nacos server address wrong/unreachable at startup, missing required properties (serverAddr), bad credentials, or a Nacos client version conflict causing createConfigService to throw — the exception is logged and swallowed, then loadConfig()/readSource() throws this error.","commonSituations":"Typo in serverAddr (e.g. wrong port); Nacos not up yet when the app starts (race); namespace/credential properties misconfigured; mixing incompatible nacos-client versions on the classpath. The app appears to start, then repeatedly logs this exception from the refresh thread while rules never load.","solutions":["Check the startup logs for '[NacosDataSource] Error occurred when initializing Nacos data source' — the swallowed cause (connect refused, auth failure, etc.) is printed there and tells you the real problem.","Fix the connection properties: correct serverAddr host:port, valid namespace/username/password, reachable network route.","Ensure the Nacos server is up before the app starts, or add startup ordering/health checks; once properties are fixed, restart the application (the client is not retried).","Prefer constructing ConfigService yourself and verifying connectivity before creating the data source, so failures surface at boot instead of on every read."],"exampleFix":"// before\nnew NacosDataSource<>(props, groupId, dataId, parser); // fails silently, readSource() throws later\n\n// after\nConfigService cs = NacosFactory.createConfigService(props); // throws at boot if unreachable\ncs.getConfig(dataId, groupId, 3000); // fail fast on connectivity\nnew NacosDataSource<>(props, groupId, dataId, parser);","handlingStrategy":"try-catch","validationCode":"// pre-verify connectivity before creating the data source\nConfigService cs = NacosFactory.createConfigService(props);\nString probe = cs.getConfig(dataId, groupId, 3000); // throws with a real cause if unreachable\nnew NacosDataSource<>(props, groupId, dataId, parser);","typeGuard":null,"tryCatchPattern":"// wrap reads from the data source\ntry {\n    T cfg = dataSource.loadConfig();\n} catch (Exception e) {\n    Throwable cause = (e.getCause() != null) ? e.getCause() : e;\n    if (cause instanceof IllegalStateException && cause.getMessage().contains(\"not been initialized\")) {\n        log.error(\"Nacos client failed to init; check serverAddr/credentials in properties and restart\", cause);\n    }\n    throw e;\n}","preventionTips":["Check startup logs for '[NacosDataSource] Error occurred when initializing' — the real cause is printed there.","Verify Nacos server reachability (curl http://<addr>/nacos) and credentials before deploying.","Fail fast at boot by creating/validating the ConfigService yourself."],"tags":["sentinel","datasource","nacos","initialization","runtime-error","java"],"backgroundTag":null,"analyzedSha":"a3f40ba8e900c8489bd520274739f17235a7721c","analyzedAt":"2026-08-14T11:10:30.678Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}