{"record":{"id":"e409d12e429297ea","repo":"yudaocode/SpringBoot-Labs","slug":"error-e409d1","errorCode":null,"errorMessage":"获取不到实例","messagePattern":"获取不到实例","errorType":"exception","errorClass":"IllegalStateException","httpStatus":500,"severity":"error","filePath":"labx-22/labx-22-scn-eureka-demo03-consumer/src/main/java/cn/iocoder/springcloudalibaba/labx22/consumerdemo/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-22/labx-22-scn-eureka-demo03-consumer/src/main/java/cn/iocoder/springcloudalibaba/labx22/consumerdemo/DemoConsumerApplication.java#L41-L70","documentation":"Identical pattern in the labx-22 demo03 consumer: it queries Eureka for 'demo-provider' via DiscoveryClient, selects the first instance, and throws IllegalStateException(\"获取不到实例\") at DemoConsumerApplication.java:59 when none exist. Zero registered-and-UP instances for the requested name at the moment of the call. demo03 in this lab typically demonstrates Feign or registry-detail variations, but this controller path uses raw DiscoveryClient, so the failure mode is the registry contents, not the Feign layer.","triggerScenarios":"/echo request while getInstances(\"demo-provider\") returns []: demo03 provider not running, wrong/missing spring.application.name, Eureka server down or wrong zone URL in either app, registration/lease-propagation delay, or the instance evicted after heartbeat loss.","commonSituations":"Running only the consumer module of the multi-module lab; renaming services between demo01/02/03 modules while the consumer literal stays 'demo-provider'; local Eureka not started (check the server process/port, default 8761); firewall/port conflict preventing the provider's registration; consumer's eureka.client.enabled or fetch-registry settings disabled in its yaml.","solutions":["Verify on the Eureka dashboard that 'demo-provider' is registered and UP; start the demo03 provider if it is not.","Fix name mismatch: provider's spring.application.name must equal the getInstances argument exactly.","Give registration time (start provider, wait for UP, then call) — first calls right after startup commonly fail.","Check eureka.client.serviceUrl.defaultZone in both apps and eureka.client.fetch-registry=true in the consumer.","Replace the bare throw with explicit empty-list handling or a load-balanced RestTemplate if you want automatic retry across instances."],"exampleFix":"// before\ninstance = instances.size() > 0 ? instances.get(0) : null;\nif (instance == null) {\n    throw new IllegalStateException(\"获取不到实例\");\n}\n\n// after\nif (instances.isEmpty()) {\n    throw new ResponseStatusException(HttpStatus.SERVICE_UNAVAILABLE,\n        \"demo-provider has no instances; check Eureka registration\");\n}\nServiceInstance instance = instances.get(0);","handlingStrategy":"validation","validationCode":"if (!discoveryClient.getInstances(\"demo-provider\").isEmpty()) {\n    ServiceInstance instance = discoveryClient.getInstances(\"demo-provider\").get(0);\n    // proceed with the call\n} else {\n    throw new ResponseStatusException(HttpStatus.SERVICE_UNAVAILABLE, \"demo-provider 暂无可用实例\");\n}","typeGuard":"boolean providerAvailable(DiscoveryClient dc) {\n    List<ServiceInstance> list = dc.getInstances(\"demo-provider\");\n    return list != null && !list.isEmpty() && list.get(0).getUri() != null;\n}","tryCatchPattern":"catch (IllegalStateException e) {\n    if (\"获取不到实例\".equals(e.getMessage())) {\n        // empty registry result: report degraded dependency, schedule retry\n        return ResponseEntity.status(503).body(\"demo-provider not registered yet\");\n    }\n    throw e;\n}","preventionTips":["Automate lab startup order with docker-compose/make targets: eureka server, provider, consumer, with health waits between.","Assert eureka.client.fetch-registry=true (default) is not disabled in the consumer's yaml.","Centralize the service name string in configuration and reuse it for both spring.application.name and lookups.","Probe discovery in an actuator health indicator so orchestration blocks traffic until instances exist."],"tags":["eureka","service-discovery","spring-cloud","registration","labx-22"],"backgroundTag":null,"analyzedSha":"6c12efaed06d12907a0f40dd2ad1f7020aec8798","analyzedAt":"2026-08-14T13:06:31.500Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}