{"record":{"id":"4f92daa6109e30a7","repo":"yudaocode/SpringBoot-Labs","slug":"error-4f92da","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-demo02-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-demo02-consumer/src/main/java/cn/iocoder/springcloudalibaba/labx01/nacosdemo/consumer/DemoConsumerApplication.java#L41-L70","documentation":"Same guard as demo01, in demo02-consumer of the Nacos discovery lab (this variant mixes load-balancer examples): DiscoveryClient.getInstances(\"demo-provider\") returns an empty list and the code throws IllegalStateException before building the target URL. Empty result = no healthy instance of that service name visible to this consumer in its Nacos namespace/group.","triggerScenarios":"Calling the consumer's echo endpoint while no demo-provider instance is registered (or only demo02/demo03 providers with different service names are up, since each demo registers its own name).","commonSituations":"Provider for this specific demo not started; consumer and provider in different namespaces (e.g. one set to a dev namespace, the other to public); provider still starting up and registration not yet propagated; Nacos server address differs between the two apps.","solutions":["Start this demo's provider module and confirm 'demo-provider' is listed and healthy in the Nacos console.","Align server-addr, namespace and group across consumer and provider configuration.","Retry the consumer request a few seconds after provider startup — registration propagation is not instantaneous."],"exampleFix":"// before\ninstance = instances.size() > 0 ? instances.get(0) : null;\nif (instance == null) {\n    throw new IllegalStateException(\"获取不到实例\");\n}\n\n// after — actionable error including a health hint\nif (instances.isEmpty()) {\n    throw new IllegalStateException(\n        \"demo-provider has no live instances; verify Nacos registration, namespace and group\");\n}","handlingStrategy":"fallback","validationCode":"if (discoveryClient.getInstances(\"demo-provider\").isEmpty()) {\n    return ServiceResult.unavailable(\"demo-provider has no instances\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    ServiceInstance instance = pickInstance(\"demo-provider\");\n    return restTemplate.getForObject(instance.getUri() + \"/echo?name=\" + name, String.class);\n} catch (IllegalStateException | RestClientException e) {\n    return \"degraded: \" + e.getMessage();\n}","preventionTips":["Health-check the registry (provider listed and healthy) before serving traffic.","Match namespace and group between consumer and provider.","Prefer client-side load balancing so a single dead instance doesn't fail the call."],"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"}