{"record":{"id":"96b5822c26bb7887","repo":"yudaocode/SpringBoot-Labs","slug":"error-96b582","errorCode":null,"errorMessage":"获取不到实例","messagePattern":"获取不到实例","errorType":"http","errorClass":"java.lang.IllegalStateException","httpStatus":500,"severity":"error","filePath":"labx-01-spring-cloud-alibaba-nacos-discovery/labx-01-sca-nacos-discovery-demo01-consumer/src/main/java/cn/iocoder/springcloudalibaba/labx01/nacosdemo/consumer/DemoConsumerApplication.java","lineNumber":59,"sourceCode":"        private RestTemplate restTemplate;\n        @Autowired\n        private LoadBalancerClient loadBalancerClient;\n\n        @GetMapping(\"/hello\")\n        public String hello(String name) {\n            // 获得服务 `demo-provider` 的一个实例\n            ServiceInstance instance;\n            if (true) {\n                // 获取服务 `demo-provider` 对应的实例列表\n                List<ServiceInstance> instances = discoveryClient.getInstances(\"demo-provider\");\n                // 选择第一个\n                instance = instances.size() > 0 ? instances.get(0) : null;\n            } else {\n                instance = loadBalancerClient.choose(\"demo-provider\");\n            }\n            // 发起调用\n            if (instance == null) {\n                throw new IllegalStateException(\"获取不到实例\");\n            }\n            String targetUrl = instance.getUri() + \"/echo?name=\" + name;\n            String response = restTemplate.getForObject(targetUrl, String.class);\n            // 返回结果\n            return \"consumer:\" + response;\n        }\n\n    }\n\n}\n","sourceCodeStart":41,"sourceCodeEnd":70,"githubUrl":"https://github.com/yudaocode/SpringBoot-Labs/blob/6c12efaed06d12907a0f40dd2ad1f7020aec8798/labx-01-spring-cloud-alibaba-nacos-discovery/labx-01-sca-nacos-discovery-demo01-consumer/src/main/java/cn/iocoder/springcloudalibaba/labx01/nacosdemo/consumer/DemoConsumerApplication.java#L41-L70","documentation":"Thrown by demo01-consumer of the Spring Cloud Alibaba Nacos discovery lab. The controller fetches all instances of service 'demo-provider' from Nacos via DiscoveryClient.getInstances(\"demo-provider\"), takes the first, and throws IllegalStateException when the list is empty. An empty list means Nacos knows of no healthy instance registered under that service name in the namespace/group the consumer queries.","triggerScenarios":"GET /echo?name=... on the consumer while demo-provider is not running, not yet registered (slow startup), registered under a different spring.application.name, or in a different Nacos namespace/group than the consumer.","commonSituations":"Starting the consumer before the provider; provider failed to register (Nacos server down at its startup, wrong nacos server-addr); mismatched namespace or group between consumer and provider bootstrap/application config; provider registered as a persistent/ephemeral instance that has expired after a crash.","solutions":["Start demo01-provider and wait for it to appear under 'Service List' in the Nacos console (http://<nacos>:8848/nacos).","Compare namespace and group settings in both applications' config — they must match exactly (default namespace 'public', group DEFAULT_GROUP).","Verify the provider's spring.application.name is exactly 'demo-provider' and its nacos server-addr points at the same Nacos the consumer uses.","Only call the consumer endpoint after the provider shows healthy in the console."],"exampleFix":"// before\nList<ServiceInstance> instances = discoveryClient.getInstances(\"demo-provider\");\ninstance = instances.size() > 0 ? instances.get(0) : null;\nif (instance == null) {\n    throw new IllegalStateException(\"获取不到实例\");\n}\n\n// after — clear message naming the missing service\nif (instances.isEmpty()) {\n    throw new IllegalStateException(\n        \"No instances available for 'demo-provider' in Nacos; check provider status, namespace and group\");\n}","handlingStrategy":"fallback","validationCode":"// Check availability before calling the consumer endpoint\nList<ServiceInstance> instances = discoveryClient.getInstances(\"demo-provider\");\nif (instances.isEmpty()) {\n    return \"demo-provider is down; start it and check Nacos console\";\n}","typeGuard":null,"tryCatchPattern":"try {\n    return restTemplate.getForObject(targetUrl, String.class);\n} catch (IllegalStateException e) {\n    return \"service unavailable: \" + e.getMessage(); // degrade gracefully\n} catch (RestClientException e) {\n    return \"call to provider failed: \" + e.getMessage();\n}","preventionTips":["Start the provider before the consumer; wait for its registration in the Nacos console.","Pin identical namespace/group/server-addr in both apps' configs.","Use LoadBalancerClient or @LoadBalanced RestTemplate for failover instead of picking index 0."],"tags":["nacos","spring-cloud-alibaba","service-discovery","environment"],"backgroundTag":null,"analyzedSha":"6c12efaed06d12907a0f40dd2ad1f7020aec8798","analyzedAt":"2026-08-14T13:06:31.500Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}