go-kratos/kratos · error
kratos/nacos: ServiceInstance.Name can not be empty
Error message
kratos/nacos: ServiceInstance.Name can not be empty
What it means
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.
Source
Thrown at contrib/registry/nacos/registry.go:20
import (
"context"
"errors"
"fmt"
"maps"
"math"
"net"
"net/url"
"strconv"
"github.com/nacos-group/nacos-sdk-go/v2/clients/naming_client"
"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
"github.com/nacos-group/nacos-sdk-go/v2/vo"
"github.com/go-kratos/kratos/v3/registry"
)
var ErrServiceInstanceNameEmpty = errors.New("kratos/nacos: ServiceInstance.Name can not be empty")
const (
defaultKind = "grpc"
kindKey = "kind"
versionKey = "version"
)
var (
_ registry.Registrar = (*Registry)(nil)
_ registry.Discovery = (*Registry)(nil)
)
type options struct {
prefix string
weight float64
cluster string
group string
kind stringView on GitHub (pinned to 668db92c2c)
Solutions
- Set ServiceInstance.Name to the nacos service name before Register, e.g. ®istry.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)
Example fix
// before
err := reg.Register(ctx, ®istry.ServiceInstance{
Endpoints: []string{"grpc://127.0.0.1:9000"},
}) // kratos/nacos: ServiceInstance.Name can not be empty
// after
err := reg.Register(ctx, ®istry.ServiceInstance{
Name: "user.service",
Version: "v1.0.0",
Endpoints: []string{"grpc://127.0.0.1:9000"},
}) Defensive patterns
Strategy: validation
Validate before calling
func validInstance(svc *registry.ServiceInstance) error {
if svc == nil || svc.Name == "" {
return errors.New("service name required for nacos registration")
}
if len(svc.Endpoints) == 0 {
return errors.New("at least one endpoint required")
}
return nil
} Prevention
- 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
When it happens
Trigger: reg.Register(ctx, ®istry.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.
Common situations: 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.
Related errors
- unable to parse repo url
- register failed: instance duplicated:
- Discovery.GetService fetch failed
- iterator closed
- watcher stopped
AI-assisted analysis of go-kratos/kratos@668db92c2c (2026-08-16).
Data as JSON: /api/errors/986d904b67b64ab9.
Report an issue: GitHub.