-
Notifications
You must be signed in to change notification settings - Fork 541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add option to configure Consul check status considered as healthy #599
base: main
Are you sure you want to change the base?
Changes from 1 commit
7fd20e8
b53ca09
78f6745
5974bbc
29893c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
package org.springframework.cloud.consul.discovery; | ||
|
||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import com.ecwid.consul.v1.health.model.Check; | ||
import com.ecwid.consul.v1.health.model.HealthService; | ||
|
@@ -33,11 +34,15 @@ public class ConsulServer extends Server { | |
|
||
private final HealthService service; | ||
|
||
private final Set<Check.CheckStatus> statusConsideredAsHealthy; | ||
|
||
private final Map<String, String> metadata; | ||
|
||
public ConsulServer(final HealthService healthService) { | ||
public ConsulServer(final HealthService healthService, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to maintain backwards compatibility so we need to leave this constructor as is (perhaps mark it as deprecated) and add a new one. |
||
Set<Check.CheckStatus> statusConsideredAsHealthy) { | ||
super(findHost(healthService), healthService.getService().getPort()); | ||
this.service = healthService; | ||
this.statusConsideredAsHealthy = statusConsideredAsHealthy; | ||
this.metadata = ConsulServerUtils.getMetadata(this.service); | ||
this.metaInfo = new MetaInfo() { | ||
@Override | ||
|
@@ -79,7 +84,7 @@ public Map<String, String> getMetadata() { | |
|
||
public boolean isPassingChecks() { | ||
for (Check check : this.service.getChecks()) { | ||
if (check.getStatus() != Check.CheckStatus.PASSING) { | ||
if (!statusConsideredAsHealthy.contains(check.getStatus())) { | ||
return false; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,10 +19,12 @@ | |
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import com.ecwid.consul.v1.ConsulClient; | ||
import com.ecwid.consul.v1.QueryParams; | ||
import com.ecwid.consul.v1.Response; | ||
import com.ecwid.consul.v1.health.model.Check; | ||
import com.ecwid.consul.v1.health.model.HealthService; | ||
import com.netflix.client.config.IClientConfig; | ||
import com.netflix.loadbalancer.AbstractServerList; | ||
|
@@ -83,20 +85,24 @@ private List<ConsulServer> getServers() { | |
if (response.getValue() == null || response.getValue().isEmpty()) { | ||
return Collections.emptyList(); | ||
} | ||
return transformResponse(response.getValue()); | ||
return transformResponse(response.getValue(), | ||
this.properties.getStatusConsideredAsHealthy()); | ||
} | ||
|
||
/** | ||
* Transforms the response from Consul in to a list of usable {@link ConsulServer}s. | ||
* @param healthServices the initial list of servers from Consul. Guaranteed to be | ||
* non-empty list | ||
* @param statusConsideredAsHealthy list of status that are considered as | ||
* healthy/passing | ||
* @return ConsulServer instances | ||
* @see ConsulServer#ConsulServer(HealthService) | ||
* @see ConsulServer#ConsulServer(HealthService, java.util.Set) | ||
*/ | ||
protected List<ConsulServer> transformResponse(List<HealthService> healthServices) { | ||
protected List<ConsulServer> transformResponse(List<HealthService> healthServices, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To avoid changing the method signature I would use |
||
Set<Check.CheckStatus> statusConsideredAsHealthy) { | ||
List<ConsulServer> servers = new ArrayList<>(); | ||
for (HealthService service : healthServices) { | ||
ConsulServer server = new ConsulServer(service); | ||
ConsulServer server = new ConsulServer(service, statusConsideredAsHealthy); | ||
if (server.getMetadata() | ||
.containsKey(this.properties.getDefaultZoneMetadataName())) { | ||
server.setZone(server.getMetadata() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 2013-2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.consul.discovery; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import com.ecwid.consul.v1.health.model.Check; | ||
import com.ecwid.consul.v1.health.model.HealthService; | ||
import org.junit.Test; | ||
|
||
import static com.ecwid.consul.v1.health.model.Check.CheckStatus.CRITICAL; | ||
import static com.ecwid.consul.v1.health.model.Check.CheckStatus.PASSING; | ||
import static com.ecwid.consul.v1.health.model.Check.CheckStatus.WARNING; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* @author Christian Lorenz | ||
*/ | ||
public class ConsulServerTest { | ||
|
||
@Test | ||
public void testIsPassingChecks() { | ||
Set<Check.CheckStatus> acceptedStatus = new HashSet<>( | ||
Arrays.asList(PASSING, WARNING)); | ||
assertThat(newServer(PASSING, acceptedStatus).isPassingChecks()).isTrue(); | ||
assertThat(newServer(WARNING, acceptedStatus).isPassingChecks()).isTrue(); | ||
assertThat(newServer(CRITICAL, acceptedStatus).isPassingChecks()).isFalse(); | ||
} | ||
|
||
static ConsulServer newServer(Check.CheckStatus checkStatus, | ||
Set<Check.CheckStatus> acceptedStatus) { | ||
HealthService healthService = new HealthService(); | ||
HealthService.Node node = new HealthService.Node(); | ||
node.setAddress("nodeaddr" + checkStatus.name()); | ||
node.setNode("nodenode" + checkStatus.name()); | ||
healthService.setNode(node); | ||
HealthService.Service service = new HealthService.Service(); | ||
service.setAddress("serviceaddr" + checkStatus.name()); | ||
service.setId("serviceid" + checkStatus.name()); | ||
service.setPort(8080); | ||
service.setService("serviceservice" + checkStatus.name()); | ||
healthService.setService(service); | ||
ArrayList<Check> checks = new ArrayList<>(); | ||
Check check = new Check(); | ||
check.setStatus(checkStatus); | ||
checks.add(check); | ||
healthService.setChecks(checks); | ||
return new ConsulServer(healthService, acceptedStatus); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should document the new property
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can add the explanation of this feature to
docs/src/main/asciidoc/spring-cloud-consul.adoc
.docs/src/main/asciidoc/_configprops.adoc
will be automatically generated for you so you don't need to make any changes here.