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

获取不到实例

Error message

获取不到实例

What it means

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.

Source

Thrown at 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: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 module and confirm 'demo-provider' is listed and healthy in the Nacos console.
  2. Align server-addr, namespace and group across consumer and provider configuration.
  3. Retry the consumer request a few seconds after provider startup — registration propagation is not instantaneous.

Example fix

// before
instance = instances.size() > 0 ? instances.get(0) : null;
if (instance == null) {
    throw new IllegalStateException("获取不到实例");
}

// after — actionable error including a health hint
if (instances.isEmpty()) {
    throw new IllegalStateException(
        "demo-provider has no live instances; verify Nacos registration, namespace and group");
}
Defensive patterns

Strategy: fallback

Validate before calling

if (discoveryClient.getInstances("demo-provider").isEmpty()) {
    return ServiceResult.unavailable("demo-provider has no instances");
}

Try / catch

try {
    ServiceInstance instance = pickInstance("demo-provider");
    return restTemplate.getForObject(instance.getUri() + "/echo?name=" + name, String.class);
} catch (IllegalStateException | RestClientException e) {
    return "degraded: " + e.getMessage();
}

Prevention

When it happens

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

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

Related errors


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