{"record":{"id":"482bf963a97c8e3f","repo":"alibaba/nacos","slug":"no-host-to-srv-for-serviceinfo","errorCode":null,"errorMessage":"no host to srv for serviceInfo: {}","messagePattern":"no host to srv for serviceInfo: (.+?)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"client/src/main/java/com/alibaba/nacos/client/naming/core/Balancer.java","lineNumber":48,"sourceCode":"/**\n * Balancer.\n *\n * @author xuanyin\n */\npublic class Balancer {\n    \n    public static class RandomByWeight {\n        \n        /**\n         * Select all instance.\n         *\n         * @param serviceInfo service information\n         * @return all instance of services\n         */\n        public static List<Instance> selectAll(ServiceInfo serviceInfo) {\n            List<Instance> hosts = serviceInfo.getHosts();\n            if (CollectionUtils.isEmpty(hosts)) {\n                throw new IllegalStateException(\n                    \"no host to srv for serviceInfo: \" + serviceInfo.getName());\n            }\n            return hosts;\n        }\n        \n        /**\n         * Random select one instance from service.\n         *\n         * @param dom service\n         * @return random instance\n         */\n        public static Instance selectHost(ServiceInfo dom) {\n            return getHostByRandomWeight(selectAll(dom));\n        }\n    }\n    \n    /**\n     * Return one host from the host list by random-weight.","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/alibaba/nacos/blob/9b989acdf181d00898f2e8839257bb2b2a3cefe3/client/src/main/java/com/alibaba/nacos/client/naming/core/Balancer.java#L30-L66","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check serviceInfo.getHosts()/getHosts() emptiness (or selectInstances with healthyOnly) before calling selectHost/selectAll.","Register/health-check instances so at least one healthy instance exists.","Handle the case of no healthy instances with a fallback or error response rather than calling the balancer.","Verify the cluster/group filters match at least one registered instance."],"exampleFix":"// before\nInstance host = Balancer.RandomByWeight.selectHost(serviceInfo); // throws if empty\n\n// after\nList<Instance> hosts = serviceInfo.getHosts();\nif (hosts == null || hosts.isEmpty()) {\n    throw new IllegalStateException(\"No instances for \" + serviceInfo.getName());\n}\nInstance host = Balancer.RandomByWeight.selectHost(serviceInfo);","handlingStrategy":"validation","validationCode":"List<Instance> hosts = serviceInfo.getHosts();\nif (hosts == null || hosts.isEmpty()) {\n    throw new IllegalStateException(\"No instances for \" + serviceInfo.getName());\n}\nInstance host = Balancer.RandomByWeight.selectHost(serviceInfo);","typeGuard":null,"tryCatchPattern":"try { Instance h = Balancer.RandomByWeight.selectHost(serviceInfo); } catch (IllegalStateException e) { // no healthy instances; degrade LOGGER.warn(\"No instances available for {}\", serviceInfo.getName()); }","preventionTips":["Check host list emptiness before balancing.","Ensure at least one healthy instance is registered for the service.","Verify cluster/group filters match a registered instance."],"tags":["naming","load-balancing","no-instances","service-discovery","illegal-state"],"backgroundTag":null,"analyzedSha":"9b989acdf181d00898f2e8839257bb2b2a3cefe3","analyzedAt":"2026-08-14T07:17:31.569Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}