{"record":{"id":"8bdc4243c383b023","repo":"go-kratos/kratos","slug":"service-s-not-found-in-registry","errorCode":null,"errorMessage":"service %s not found in registry","messagePattern":"service (.+?) not found in registry","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"contrib/registry/consul/registry.go","lineNumber":169,"sourceCode":"\t\tservices, _, err := r.cli.Service(ctx, name, 0, true)\n\t\tif err == nil && len(services) > 0 {\n\t\t\treturn services\n\t\t}\n\t\treturn nil\n\t}\n\n\tif set == nil {\n\t\tif s := getRemote(); len(s) > 0 {\n\t\t\treturn s, nil\n\t\t}\n\t\treturn nil, fmt.Errorf(\"service %s not resolved in registry\", name)\n\t}\n\tss, _ := set.services.Load().([]*registry.ServiceInstance)\n\tif ss == nil {\n\t\tif s := getRemote(); len(s) > 0 {\n\t\t\treturn s, nil\n\t\t}\n\t\treturn nil, fmt.Errorf(\"service %s not found in registry\", name)\n\t}\n\treturn ss, nil\n}\n\n// ListServices return service list.\nfunc (r *Registry) ListServices() (allServices map[string][]*registry.ServiceInstance, err error) {\n\tr.lock.RLock()\n\tdefer r.lock.RUnlock()\n\tallServices = make(map[string][]*registry.ServiceInstance)\n\tfor name, set := range r.registry {\n\t\tss, _ := set.services.Load().([]*registry.ServiceInstance)\n\t\tif ss == nil {\n\t\t\tcontinue\n\t\t}\n\t\tservices := make([]*registry.ServiceInstance, 0, len(ss))\n\t\tservices = append(services, ss...)\n\t\tallServices[name] = services\n\t}","sourceCodeStart":151,"sourceCodeEnd":187,"githubUrl":"https://github.com/go-kratos/kratos/blob/668db92c2c001e9552594ba5a8aede8456af6d7e/contrib/registry/consul/registry.go#L151-L187","documentation":"Consul registry GetService with an existing watch set whose snapshot is currently empty: the code falls back to a live Consul query and, still finding nothing, returns 'not found in registry'. The service was known before but has zero available endpoints right now.","triggerScenarios":"GetService(ctx, name) where the watch set exists but its atomic services snapshot is nil (all instances deregistered) and the fallback live query also returns nothing.","commonSituations":"Rolling restarts deregistering all instances briefly; flapping health checks; scale-to-zero; maintenance windows; network partition between app and Consul agents.","solutions":["Retry with backoff — usually transient while instances re-register during deploys","Verify instance health in Consul and investigate why all checks or registrations dropped","Add consumer-side circuit breaking and fallback so empty resolution degrades gracefully"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// pre-flight: verify healthy instances exist before depending on the service\nsvcs, _, err := consulClient.Health().Service(name, \"\", true, nil)\nif err != nil {\n    return err\n}\nif len(svcs) == 0 {\n    return fmt.Errorf(\"no healthy instances for %s yet\", name)\n}","typeGuard":null,"tryCatchPattern":"for attempt := 0; attempt < maxAttempts; attempt++ {\n    ins, err := reg.GetService(ctx, name)\n    if err == nil {\n        return ins\n    }\n    if strings.Contains(err.Error(), \"not found in registry\") {\n        time.Sleep(backoff(attempt)) // transient during re-registration\n        continue\n    }\n    return nil, err\n}","preventionTips":["Avoid scale-to-zero for critical dependencies","Keep last-known endpoints in the consumer's connection pool during blips","Watch registry events instead of polling to react to re-registration faster"],"tags":["consul","registry","service-discovery","availability","go"],"backgroundTag":null,"analyzedSha":"668db92c2c001e9552594ba5a8aede8456af6d7e","analyzedAt":"2026-08-16T02:07:20.704Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}