fix(consul): fix gRPC health checks and add API Gateway Consul registration
This commit is contained in:
@@ -58,14 +58,25 @@ func main() {
|
||||
}
|
||||
gateway.SetupRoutes(srv.Router())
|
||||
|
||||
// Register with Consul
|
||||
// Determine port and host for registration
|
||||
gatewayPort := cfg.GetInt("gateway.port")
|
||||
if gatewayPort == 0 {
|
||||
gatewayPort = 8080
|
||||
gatewayPort = cfg.GetInt("server.port")
|
||||
if gatewayPort == 0 {
|
||||
gatewayPort = 8080
|
||||
}
|
||||
}
|
||||
|
||||
// In Docker, always use the Docker service name for health checks
|
||||
// Consul (also in Docker) needs to reach the service via Docker DNS
|
||||
gatewayHost := cfg.GetString("gateway.host")
|
||||
if gatewayHost == "" {
|
||||
gatewayHost = "localhost"
|
||||
if os.Getenv("ENVIRONMENT") == "production" || os.Getenv("DOCKER") == "true" {
|
||||
gatewayHost = "api-gateway" // Docker service name - required for Consul health checks
|
||||
} else if gatewayHost == "" {
|
||||
gatewayHost = cfg.GetString("server.host")
|
||||
if gatewayHost == "" || gatewayHost == "0.0.0.0" {
|
||||
gatewayHost = "localhost" // Local development
|
||||
}
|
||||
}
|
||||
|
||||
serviceInstance := ®istry.ServiceInstance{
|
||||
@@ -75,7 +86,8 @@ func main() {
|
||||
Port: gatewayPort,
|
||||
Tags: []string{"gateway", "http"},
|
||||
Metadata: map[string]string{
|
||||
"version": "1.0.0",
|
||||
"version": "1.0.0",
|
||||
"protocol": "http",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -324,6 +324,9 @@ func provideAuditService() fx.Option {
|
||||
// Register health service
|
||||
healthServer := health.NewServer()
|
||||
grpc_health_v1.RegisterHealthServer(grpcServer, healthServer)
|
||||
// Set serving status for the default service (empty string) - this is what Consul checks
|
||||
healthServer.SetServingStatus("", grpc_health_v1.HealthCheckResponse_SERVING)
|
||||
// Also set for the specific service name
|
||||
healthServer.SetServingStatus("audit.v1.AuditService", grpc_health_v1.HealthCheckResponse_SERVING)
|
||||
|
||||
// Register reflection for grpcurl
|
||||
|
||||
@@ -406,6 +406,9 @@ func provideAuthService() fx.Option {
|
||||
// Register health service
|
||||
healthServer := health.NewServer()
|
||||
grpc_health_v1.RegisterHealthServer(grpcServer, healthServer)
|
||||
// Set serving status for the default service (empty string) - this is what Consul checks
|
||||
healthServer.SetServingStatus("", grpc_health_v1.HealthCheckResponse_SERVING)
|
||||
// Also set for the specific service name
|
||||
healthServer.SetServingStatus("auth.v1.AuthService", grpc_health_v1.HealthCheckResponse_SERVING)
|
||||
|
||||
// Register reflection for grpcurl
|
||||
|
||||
@@ -271,6 +271,9 @@ func provideAuthzService() fx.Option {
|
||||
// Register health service
|
||||
healthServer := health.NewServer()
|
||||
grpc_health_v1.RegisterHealthServer(grpcServer, healthServer)
|
||||
// Set serving status for the default service (empty string) - this is what Consul checks
|
||||
healthServer.SetServingStatus("", grpc_health_v1.HealthCheckResponse_SERVING)
|
||||
// Also set for the specific service name
|
||||
healthServer.SetServingStatus("authz.v1.AuthzService", grpc_health_v1.HealthCheckResponse_SERVING)
|
||||
|
||||
// Register reflection for grpcurl
|
||||
|
||||
@@ -418,6 +418,9 @@ func provideIdentityService() fx.Option {
|
||||
// Register health service
|
||||
healthServer := health.NewServer()
|
||||
grpc_health_v1.RegisterHealthServer(grpcServer, healthServer)
|
||||
// Set serving status for the default service (empty string) - this is what Consul checks
|
||||
healthServer.SetServingStatus("", grpc_health_v1.HealthCheckResponse_SERVING)
|
||||
// Also set for the specific service name
|
||||
healthServer.SetServingStatus("identity.v1.IdentityService", grpc_health_v1.HealthCheckResponse_SERVING)
|
||||
|
||||
// Register reflection for grpcurl
|
||||
|
||||
Reference in New Issue
Block a user