yudaocode/SpringBoot-Labs · error · java.lang.IllegalStateException

获取不到实例

Error message

获取不到实例

What it means

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.

Source

Thrown at 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: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

  1. Start this demo's provider and wait for it to register in the Nacos console before calling the consumer.
  2. Check namespace/group/server-addr parity between the two apps' configuration files.
  3. If Nacos was restarted, restart the provider so it re-registers (ephemeral instances do not survive server restart without persistence).

Example fix

// before
if (instance == null) {
    throw new IllegalStateException("获取不到实例");
}

// after — also cover the empty-list case with a specific message
if (instance == null) {
    throw new IllegalStateException(
        "No instance of 'demo-provider' found in Nacos (namespace=" + namespace + ")");
}
Defensive patterns

Strategy: fallback

Validate before calling

List<ServiceInstance> instances = discoveryClient.getInstances("demo-provider");
if (instances.isEmpty()) return "provider unavailable, retry later";
ServiceInstance instance = instances.get(0);

Try / catch

try {
    return "consumer:" + restTemplate.getForObject(targetUrl, String.class);
} catch (IllegalStateException e) {
    return "no instance available — is demo-provider registered?";
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of yudaocode/SpringBoot-Labs@6c12efaed0 (2026-08-14). Data as JSON: /api/errors/b42422651707c5d0. Report an issue: GitHub.