{"record":{"id":"ec195cbb822c1857","repo":"alibaba/spring-ai-alibaba","slug":"restclient","errorCode":null,"errorMessage":"创建RestClient失败","messagePattern":"创建RestClient失败","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"critical","filePath":"spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-start/src/main/java/com/alibaba/cloud/ai/studio/admin/config/ElasticsearchConfig.java","lineNumber":35,"sourceCode":"\n    @Bean\n    public RestClient restClient(ElasticsearchProperties properties) {\n        try {\n            URL url = new URL(properties.getUrl());\n            \n            return RestClient.builder(\n                    new HttpHost(url.getHost(), url.getPort(), url.getProtocol()))\n                .setRequestConfigCallback(requestConfigBuilder -> \n                    requestConfigBuilder\n                        .setConnectTimeout(properties.getConnectTimeout())\n                        .setSocketTimeout(properties.getSocketTimeout()))\n                .setHttpClientConfigCallback(httpClientBuilder -> \n                    httpClientBuilder\n                        .setMaxConnTotal(properties.getConnectionPool().getMaxConnections())\n                        .setMaxConnPerRoute(properties.getConnectionPool().getMaxIdleConnections()))\n                .build();\n        } catch (Exception e) {\n            throw new RuntimeException(\"创建RestClient失败\", e);\n        }\n    }\n\n    @Bean\n    public RestClientTransport elasticsearchTransport(RestClient restClient) {\n        return new RestClientTransport(restClient, new JacksonJsonpMapper());\n    }\n\n    @Bean\n    public ElasticsearchClient elasticsearchClient(RestClientTransport transport) {\n        return new ElasticsearchClient(transport);\n    }\n}\n","sourceCodeStart":17,"sourceCodeEnd":49,"githubUrl":"https://github.com/alibaba/spring-ai-alibaba/blob/f82da0b50f35744c13968191be2b1cd2452ef550/spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-start/src/main/java/com/alibaba/cloud/ai/studio/admin/config/ElasticsearchConfig.java#L17-L49","documentation":"ElasticsearchConfig.restClient() wraps all failures during construction of the Elasticsearch low-level RestClient in a RuntimeException with the Chinese message \"创建RestClient失败\" (\"Failed to create RestClient\"). This happens while building the client from the configured host, credentials, and connection-pool settings; any exception (bad address, IO error during init, invalid pool config) is rethrown as this Spring bean-creation failure. Because it runs as a @Bean factory, the application context fails to start.","triggerScenarios":"Calling restClient() with an unreachable/malformed Elasticsearch address, invalid connection-pool properties (e.g. maxConnections <= 0), or when the underlying apache HttpClient builder throws while applying setMaxConnTotal/setMaxConnPerRoute from ElasticsearchProperties.ConnectionPool.","commonSituations":"Wrong ES host/port in application.yml, Elasticsearch not running or unreachable at startup, mistyped connection-pool numbers in config, missing credentials causing connection setup failure, or config property binding producing zero/negative pool sizes.","solutions":["Verify the Elasticsearch host/port and that the cluster is reachable (curl http://host:9200) before starting the app.","Check the 'cause' chained in the RuntimeException — it contains the real failure (UnknownHost, ConnectException, etc.).","Validate connection-pool settings in ElasticsearchProperties: maxConnections and maxIdleConnections must be positive and sane.","Confirm config keys bind correctly (prefix, property names) so the pool values aren't null/0.","If credentials/SSL are involved, confirm username/password and truststore configuration."],"exampleFix":"// before\nproperties.getConnectionPool().getMaxConnections() // may be 0/null from bad config\n// after\nint max = properties.getConnectionPool().getMaxConnections();\nif (max <= 0) { max = 30; } // sane default or fail fast with a clear validation message","handlingStrategy":"try-catch","validationCode":"// verify ES reachability before startup\ncurl -fsS http://es-host:9200/_cluster/health || echo \"ES unreachable\"\n// check pool config\nassert properties.getConnectionPool().getMaxConnections() > 0;","typeGuard":null,"tryCatchPattern":"try { RestClient client = restClient(); } catch (RuntimeException e) { log.error(\"RestClient init failed: {}\", e.getCause(), e); throw e; }","preventionTips":["Smoke-test ES connectivity (curl /_cluster/health) as part of deployment readiness checks","Validate connection-pool properties at config load time with sane minimums","Keep host/port/credentials in checked-in example config and review diffs","Always log the cause chain when wrapping bean-init failures"],"tags":["elasticsearch","connection","startup","configuration","network"],"backgroundTag":"connection-refused","analyzedSha":"f82da0b50f35744c13968191be2b1cd2452ef550","analyzedAt":"2026-09-09T15:32:42.421Z","contentChangedAt":"2026-09-09T15:32:42.421Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}