{"record":{"id":"b42422651707c5d0","repo":"yudaocode/SpringBoot-Labs","slug":"error-b42422","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-demo03-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-demo03-consumer/src/main/java/cn/iocoder/springcloudalibaba/labx01/nacosdemo/consumer/DemoConsumerApplication.java#L41-L70","documentation":"Identical pattern in demo03-consumer of the Nacos discovery lab: it looks up instances of 'demo-provider' through DiscoveryClient and throws IllegalStateException when none exist. The throw means the consumer's view of the Nacos registry contains zero entries for that service name.","triggerScenarios":"Invoking the consumer endpoint while demo03's provider is down, unregistered, registered under a different name, or in a different namespace/group; also fires immediately after provider startup before its registration heartbeats reach the consumer's cached catalogue.","commonSituations":"Running consumers of demo01/02/03 interchangeably while only one demo's provider is up; namespace mismatch in application.yaml; Nacos server restarted and ephemeral instances were evicted; provider crashed without deregistering and the lease has expired.","solutions":["Start this demo's provider and wait for it to register in the Nacos console before calling the consumer.","Check namespace/group/server-addr parity between the two apps' configuration files.","If Nacos was restarted, restart the provider so it re-registers (ephemeral instances do not survive server restart without persistence)."],"exampleFix":"// before\nif (instance == null) {\n    throw new IllegalStateException(\"获取不到实例\");\n}\n\n// after — also cover the empty-list case with a specific message\nif (instance == null) {\n    throw new IllegalStateException(\n        \"No instance of 'demo-provider' found in Nacos (namespace=\" + namespace + \")\");\n}","handlingStrategy":"fallback","validationCode":"List<ServiceInstance> instances = discoveryClient.getInstances(\"demo-provider\");\nif (instances.isEmpty()) return \"provider unavailable, retry later\";\nServiceInstance instance = instances.get(0);","typeGuard":null,"tryCatchPattern":"try {\n    return \"consumer:\" + restTemplate.getForObject(targetUrl, String.class);\n} catch (IllegalStateException e) {\n    return \"no instance available — is demo-provider registered?\";\n}","preventionTips":["Register startup dependency: provider first, consumer second.","Verify Nacos console shows the instance before invoking the consumer.","Restart providers after a Nacos server restart so they re-register."],"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"}