{"record":{"id":"986d904b67b64ab9","repo":"go-kratos/kratos","slug":"kratos-nacos-serviceinstance-name-can-not-be-empt","errorCode":null,"errorMessage":"kratos/nacos: ServiceInstance.Name can not be empty","messagePattern":"kratos/nacos: ServiceInstance\\.Name can not be empty","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"contrib/registry/nacos/registry.go","lineNumber":20,"sourceCode":"\nimport (\n\t\"context\"\n\t\"errors\"\n\t\"fmt\"\n\t\"maps\"\n\t\"math\"\n\t\"net\"\n\t\"net/url\"\n\t\"strconv\"\n\n\t\"github.com/nacos-group/nacos-sdk-go/v2/clients/naming_client\"\n\t\"github.com/nacos-group/nacos-sdk-go/v2/common/constant\"\n\t\"github.com/nacos-group/nacos-sdk-go/v2/vo\"\n\n\t\"github.com/go-kratos/kratos/v3/registry\"\n)\n\nvar ErrServiceInstanceNameEmpty = errors.New(\"kratos/nacos: ServiceInstance.Name can not be empty\")\n\nconst (\n\tdefaultKind = \"grpc\"\n\tkindKey     = \"kind\"\n\tversionKey  = \"version\"\n)\n\nvar (\n\t_ registry.Registrar = (*Registry)(nil)\n\t_ registry.Discovery = (*Registry)(nil)\n)\n\ntype options struct {\n\tprefix  string\n\tweight  float64\n\tcluster string\n\tgroup   string\n\tkind    string","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/go-kratos/kratos/blob/668db92c2c001e9552594ba5a8aede8456af6d7e/contrib/registry/nacos/registry.go#L2-L38","documentation":"ErrServiceInstanceNameEmpty (contrib/registry/nacos/registry.go:20) is returned by the nacos Registry when Register receives a *registry.ServiceInstance whose Name is empty. The instance Name becomes the nacos service name (GroupName/ServiceName) used in the RegisterInstance RPC, so an empty name cannot be registered and is rejected client-side before any nacos call.","triggerScenarios":"reg.Register(ctx, &registry.ServiceInstance{Endpoints: [...], Metadata: [...]}) with Name unset (zero value \"\"); building the instance from a config field that is empty in this environment; passing a nil-ish or partially initialized struct during tests.","commonSituations":"Service name meant to come from an env var / config key that is missing in a new deployment; constructor refactoring that stopped populating Name; using the same registrar for multiple services and forgetting to set the name on one of them.","solutions":["Set ServiceInstance.Name to the nacos service name before Register, e.g. &registry.ServiceInstance{Name: \"user.service\", ...}","If the name comes from config, fail fast at startup when it is empty rather than passing it through","Add a startup assertion/validator over required instance fields (Name, Endpoints)"],"exampleFix":"// before\nerr := reg.Register(ctx, &registry.ServiceInstance{\n    Endpoints: []string{\"grpc://127.0.0.1:9000\"},\n}) // kratos/nacos: ServiceInstance.Name can not be empty\n\n// after\nerr := reg.Register(ctx, &registry.ServiceInstance{\n    Name:      \"user.service\",\n    Version:   \"v1.0.0\",\n    Endpoints: []string{\"grpc://127.0.0.1:9000\"},\n})","handlingStrategy":"validation","validationCode":"func validInstance(svc *registry.ServiceInstance) error {\n    if svc == nil || svc.Name == \"\" {\n        return errors.New(\"service name required for nacos registration\")\n    }\n    if len(svc.Endpoints) == 0 {\n        return errors.New(\"at least one endpoint required\")\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Fail fast at bootstrap when the configured service name is empty","Source the name from config with a non-empty default (e.g. app name constant)","Add unit tests asserting the registration struct is fully populated"],"tags":["registry","nacos","validation"],"backgroundTag":null,"analyzedSha":"668db92c2c001e9552594ba5a8aede8456af6d7e","analyzedAt":"2026-08-16T02:07:20.704Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}