yudaocode/SpringBoot-Labs · error · java.lang.IllegalStateException
获取不到实例
Error message
获取不到实例
What it means
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.
Source
Thrown at 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:59
private RestTemplate restTemplate;
@Autowired
private LoadBalancerClient loadBalancerClient;
@GetMapping("/hello")
public String hello(String name) {
// 获得服务 `demo-provider` 的一个实例
ServiceInstance instance;
if (true) {
// 获取服务 `demo-provider` 对应的实例列表
List<ServiceInstance> instances = discoveryClient.getInstances("demo-provider");
// 选择第一个
instance = instances.size() > 0 ? instances.get(0) : null;
} else {
instance = loadBalancerClient.choose("demo-provider");
}
// 发起调用
if (instance == null) {
throw new IllegalStateException("获取不到实例");
}
String targetUrl = instance.getUri() + "/echo?name=" + name;
String response = restTemplate.getForObject(targetUrl, String.class);
// 返回结果
return "consumer:" + response;
}
}
}
View on GitHub (pinned to 6c12efaed0)
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.
Example fix
// before
List<ServiceInstance> instances = discoveryClient.getInstances("demo-provider");
instance = instances.size() > 0 ? instances.get(0) : null;
if (instance == null) {
throw new IllegalStateException("获取不到实例");
}
// after — clear message naming the missing service
if (instances.isEmpty()) {
throw new IllegalStateException(
"No instances available for 'demo-provider' in Nacos; check provider status, namespace and group");
} Defensive patterns
Strategy: fallback
Validate before calling
// Check availability before calling the consumer endpoint
List<ServiceInstance> instances = discoveryClient.getInstances("demo-provider");
if (instances.isEmpty()) {
return "demo-provider is down; start it and check Nacos console";
} Try / catch
try {
return restTemplate.getForObject(targetUrl, String.class);
} catch (IllegalStateException e) {
return "service unavailable: " + e.getMessage(); // degrade gracefully
} catch (RestClientException e) {
return "call to provider failed: " + e.getMessage();
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
AI-assisted analysis of yudaocode/SpringBoot-Labs@6c12efaed0 (2026-08-14).
Data as JSON: /api/errors/96b5822c26bb7887.
Report an issue: GitHub.