add id when registering services to consul (#1802)

This commit is contained in:
shiling02404 2022-06-21 21:41:07 +08:00 committed by GitHub
parent ccf6111892
commit 6d0636ebab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 2 deletions

View File

@ -244,12 +244,13 @@ func getRegistrationConfig(cfg configSpec, props map[string]string) (*consul.Age
return nil, fmt.Errorf("error parsing %s: %w", nr.DaprHTTPPort, err) return nil, fmt.Errorf("error parsing %s: %w", nr.DaprHTTPPort, err)
} }
id := appID + "-" + host + "-" + httpPort
// if no health checks configured add dapr sidecar health check by default // if no health checks configured add dapr sidecar health check by default
if len(cfg.Checks) == 0 { if len(cfg.Checks) == 0 {
cfg.Checks = []*consul.AgentServiceCheck{ cfg.Checks = []*consul.AgentServiceCheck{
{ {
Name: "Dapr Health Status", Name: "Dapr Health Status",
CheckID: fmt.Sprintf("daprHealth:%s", appID), CheckID: fmt.Sprintf("daprHealth:%s", id),
Interval: "15s", Interval: "15s",
HTTP: fmt.Sprintf("http://%s:%s/v1.0/healthz", host, httpPort), HTTP: fmt.Sprintf("http://%s:%s/v1.0/healthz", host, httpPort),
}, },
@ -262,6 +263,7 @@ func getRegistrationConfig(cfg configSpec, props map[string]string) (*consul.Age
} }
return &consul.AgentServiceRegistration{ return &consul.AgentServiceRegistration{
ID: id,
Name: appID, Name: appID,
Address: host, Address: host,
Port: appPortInt, Port: appPortInt,

View File

@ -501,7 +501,7 @@ func TestGetConfig(t *testing.T) {
assert.Equal(t, 1, len(actual.Registration.Checks)) assert.Equal(t, 1, len(actual.Registration.Checks))
check := actual.Registration.Checks[0] check := actual.Registration.Checks[0]
assert.Equal(t, "Dapr Health Status", check.Name) assert.Equal(t, "Dapr Health Status", check.Name)
assert.Equal(t, "daprHealth:test-app", check.CheckID) assert.Equal(t, "daprHealth:test-app-"+metadata.Properties[nr.HostAddress]+"-"+metadata.Properties[nr.DaprHTTPPort], check.CheckID)
assert.Equal(t, "15s", check.Interval) assert.Equal(t, "15s", check.Interval)
assert.Equal(t, fmt.Sprintf("http://%s:%s/v1.0/healthz", metadata.Properties[nr.HostAddress], metadata.Properties[nr.DaprHTTPPort]), check.HTTP) assert.Equal(t, fmt.Sprintf("http://%s:%s/v1.0/healthz", metadata.Properties[nr.HostAddress], metadata.Properties[nr.DaprHTTPPort]), check.HTTP)