-
Notifications
You must be signed in to change notification settings - Fork 542
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
Open
jackhammer2k
wants to merge
5
commits into
spring-cloud:main
Choose a base branch
from
jackhammer2k:feature/considerWarningAsHealthy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7fd20e8
add option to configure Consul check status considered as healthy
jackhammer2k b53ca09
Merge branch 'master' into feature/considerWarningAsHealthy
jackhammer2k 78f6745
* document new property
jackhammer2k 5974bbc
fix imports for checkstyle
jackhammer2k 29893c7
fix unit test by ignoring order in result
jackhammer2k File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,9 @@ | |
|
||
package org.springframework.cloud.consul.discovery; | ||
|
||
import java.util.Collections; | ||
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 +35,19 @@ 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) { | ||
this(healthService, Collections.singleton(Check.CheckStatus.PASSING)); | ||
} | ||
|
||
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 +89,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; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...-discovery/src/test/java/org/springframework/cloud/consul/discovery/ConsulServerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.