fix(consul): fix gRPC health checks and add API Gateway Consul registration

This commit is contained in:
2025-11-06 22:04:55 +01:00
parent 04022b835e
commit dbe29bfb82
8 changed files with 610 additions and 66 deletions

View File

@@ -69,12 +69,42 @@ func (r *ConsulRegistry) Register(ctx context.Context, service *registry.Service
Meta: service.Metadata,
}
// Determine health check type based on service metadata/tags or config
// Check if service is HTTP (has "http" tag or protocol metadata)
isHTTP := false
for _, tag := range service.Tags {
if tag == "http" {
isHTTP = true
break
}
}
if !isHTTP && service.Metadata != nil {
if protocol, ok := service.Metadata["protocol"]; ok && protocol == "http" {
isHTTP = true
}
}
// Add health check if configured
if r.config.HealthCheck.UseGRPC {
if isHTTP && r.config.HealthCheck.HTTP != "" {
// Use HTTP health check for HTTP services (e.g., API Gateway)
healthCheckURL := fmt.Sprintf("http://%s:%d%s", service.Address, service.Port, r.config.HealthCheck.HTTP)
registration.Check = &consulapi.AgentServiceCheck{
HTTP: healthCheckURL,
Interval: r.config.HealthCheck.Interval.String(),
Timeout: r.config.HealthCheck.Timeout.String(),
DeregisterCriticalServiceAfter: r.config.HealthCheck.DeregisterAfter.String(),
}
} else if !isHTTP && r.config.HealthCheck.UseGRPC {
// Use gRPC health check for gRPC services
// Format: host:port/service or host:port (uses default health service)
// Format: host:port (checks default service with empty string name)
// Or: host:port/service (checks specific service name)
// We use host:port to check the default service (empty string)
grpcAddr := fmt.Sprintf("%s:%d", service.Address, service.Port)
if r.config.HealthCheck.GRPC != "" {
// If a specific service name is provided, append it
// Otherwise, check the default service (empty string) which we set in each service
if r.config.HealthCheck.GRPC != "" && r.config.HealthCheck.GRPC != "grpc.health.v1.Health" {
// Only append if it's not the default health service name
// The GRPC field in Consul expects the application service name, not the proto service name
grpcAddr = fmt.Sprintf("%s:%d/%s", service.Address, service.Port, r.config.HealthCheck.GRPC)
}
registration.Check = &consulapi.AgentServiceCheck{
@@ -84,7 +114,7 @@ func (r *ConsulRegistry) Register(ctx context.Context, service *registry.Service
DeregisterCriticalServiceAfter: r.config.HealthCheck.DeregisterAfter.String(),
}
} else if r.config.HealthCheck.HTTP != "" {
// Use HTTP health check for HTTP services
// Fallback to HTTP if HTTP endpoint is configured and service is not explicitly gRPC
healthCheckURL := fmt.Sprintf("http://%s:%d%s", service.Address, service.Port, r.config.HealthCheck.HTTP)
registration.Check = &consulapi.AgentServiceCheck{
HTTP: healthCheckURL,