This document covers xDS (discovery service) support in gRPC-Go for advanced service mesh and traffic management features.
xDS enables gRPC to integrate with service mesh control planes like Istio, providing dynamic configuration for load balancing, routing, security, and more.
gRPC-Go supports the following xDS resources:
import (
"google.golang.org/grpc"
_ "google.golang.org/grpc/xds" // Import to enable xDS
)
// Use xds:/// scheme to enable xDS-based name resolution
conn, err := grpc.NewClient("xds:///myservice",
grpc.WithTransportCredentials(creds))import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/xds"
)
// xDS credentials with fallback
fallback, _ := credentials.NewClientTLSFromFile("ca.pem", "")
xdsCreds, err := xds.NewClientCredentials(xds.ClientOptions{
FallbackCreds: fallback,
})
conn, err := grpc.NewClient("xds:///myservice",
grpc.WithTransportCredentials(xdsCreds))import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
xdscreds "google.golang.org/grpc/credentials/xds"
"google.golang.org/grpc/xds"
)
// Create xDS-enabled server
fallback, _ := credentials.NewServerTLSFromFile("server.pem", "server.key")
xdsCreds, err := xdscreds.NewServerCredentials(xdscreds.ServerOptions{
FallbackCreds: fallback,
})
server, err := xds.NewGRPCServer(grpc.Creds(xdsCreds))
if err != nil {
log.Fatal(err)
}
pb.RegisterMyServiceServer(server, &myServiceImpl{})
// Listen on xDS-configured address
lis, _ := net.Listen("tcp", ":50051")
server.Serve(lis)import "google.golang.org/grpc/connectivity"
type ServingMode int
const (
// ServingModeStarting: server is starting up
ServingModeStarting ServingMode = iota
// ServingModeServing: server has configuration and serving RPCs
ServingModeServing
// ServingModeNotServing: server not accepting new connections
ServingModeNotServing
)gRPC reads xDS configuration from bootstrap file specified by GRPC_XDS_BOOTSTRAP environment variable:
{
"xds_servers": [
{
"server_uri": "xds-control-plane:18000",
"channel_creds": [{"type": "insecure"}],
"server_features": ["xds_v3"]
}
],
"node": {
"id": "my-service-node",
"cluster": "my-cluster",
"locality": {
"zone": "us-central1-a"
},
"metadata": {
"INSTANCE_IP": "10.0.0.1"
}
}
}GRPC_XDS_BOOTSTRAP: Path to xDS bootstrap configuration fileGRPC_XDS_BOOTSTRAP_CONFIG: xDS bootstrap configuration as JSON stringGRPC_XDS_EXPERIMENTAL_SECURITY_SUPPORT: Enable xDS security featuresFor detailed xDS configuration and features, see: gRPC xDS Documentation