alibaba/nacos · error · IllegalStateException

no host to srv for serviceInfo: {}

Error message

no host to srv for serviceInfo: {}

What it means

Thrown by Balancer.RandomByWeight.selectAll when serviceInfo.getHosts() is null or empty. This is used during instance selection (e.g. selectHost) for load balancing; if no instances are available (all unhealthy, none registered, or filtered out), there is nothing to route to and an IllegalStateException is thrown.

Source

Thrown at client/src/main/java/com/alibaba/nacos/client/naming/core/Balancer.java:48

/**
 * Balancer.
 *
 * @author xuanyin
 */
public class Balancer {
    
    public static class RandomByWeight {
        
        /**
         * Select all instance.
         *
         * @param serviceInfo service information
         * @return all instance of services
         */
        public static List<Instance> selectAll(ServiceInfo serviceInfo) {
            List<Instance> hosts = serviceInfo.getHosts();
            if (CollectionUtils.isEmpty(hosts)) {
                throw new IllegalStateException(
                    "no host to srv for serviceInfo: " + serviceInfo.getName());
            }
            return hosts;
        }
        
        /**
         * Random select one instance from service.
         *
         * @param dom service
         * @return random instance
         */
        public static Instance selectHost(ServiceInfo dom) {
            return getHostByRandomWeight(selectAll(dom));
        }
    }
    
    /**
     * Return one host from the host list by random-weight.

View on GitHub (pinned to 9b989acdf1)

Solutions

  1. Check serviceInfo.getHosts()/getHosts() emptiness (or selectInstances with healthyOnly) before calling selectHost/selectAll.
  2. Register/health-check instances so at least one healthy instance exists.
  3. Handle the case of no healthy instances with a fallback or error response rather than calling the balancer.
  4. Verify the cluster/group filters match at least one registered instance.

Example fix

// before
Instance host = Balancer.RandomByWeight.selectHost(serviceInfo); // throws if empty

// after
List<Instance> hosts = serviceInfo.getHosts();
if (hosts == null || hosts.isEmpty()) {
    throw new IllegalStateException("No instances for " + serviceInfo.getName());
}
Instance host = Balancer.RandomByWeight.selectHost(serviceInfo);
Defensive patterns

Strategy: validation

Validate before calling

List<Instance> hosts = serviceInfo.getHosts();
if (hosts == null || hosts.isEmpty()) {
    throw new IllegalStateException("No instances for " + serviceInfo.getName());
}
Instance host = Balancer.RandomByWeight.selectHost(serviceInfo);

Try / catch

try { Instance h = Balancer.RandomByWeight.selectHost(serviceInfo); } catch (IllegalStateException e) { // no healthy instances; degrade LOGGER.warn("No instances available for {}", serviceInfo.getName()); }

Prevention

When it happens

Trigger: Requesting a host from a service that has zero healthy instances; the service exists but all instances are marked unhealthy or have been deregistered; a subscribe returned a ServiceInfo before any instances populated; selectors/clusters filtered out every instance.

Common situations: Service with instances down/unhealthy; clients routing before the first push of instances arrives; cluster or selector filters that exclude all hosts; ephemeral instances all expired.

Related errors


AI-assisted analysis of alibaba/nacos@9b989acdf1 (2026-08-14). Data as JSON: /api/errors/482bf963a97c8e3f. Report an issue: GitHub.