move to http check by default

This commit is contained in:
Spencer Gibb
2015-05-20 14:46:50 -06:00
parent 8371aa78ef
commit 0d3f028a5f
5 changed files with 110 additions and 9 deletions

View File

@@ -202,6 +202,12 @@
<artifactId>ribbon-core</artifactId>
<version>${ribbon.version}</version>
</dependency>
<!-- ribbon core dep -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
<dependency>
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon-loadbalancer</artifactId>

View File

@@ -17,6 +17,8 @@
package org.springframework.cloud.consul.discovery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -26,6 +28,7 @@ import com.ecwid.consul.v1.ConsulClient;
* @author Spencer Gibb
*/
@Configuration
@EnableConfigurationProperties
public class ConsulDiscoveryClientConfiguration {
@Autowired
@@ -37,6 +40,7 @@ public class ConsulDiscoveryClientConfiguration {
}
@Bean
@ConditionalOnProperty("consul.heartbeat.enabled")
public TtlScheduler ttlScheduler() {
return new TtlScheduler(heartbeatProperties(), consulClient);
}
@@ -46,6 +50,11 @@ public class ConsulDiscoveryClientConfiguration {
return new HeartbeatProperties();
}
@Bean
public ConsulDiscoveryProperties consulDiscoveryProperties() {
return new ConsulDiscoveryProperties();
}
@Bean
public ConsulDiscoveryClient consulDiscoveryClient() {
return new ConsulDiscoveryClient();

View File

@@ -0,0 +1,70 @@
/*
* Copyright 2013-2015 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
*
* http://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.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @author Spencer Gibb
*/
@ConfigurationProperties("consul.discovery")
@Data
@CommonsLog
public class ConsulDiscoveryProperties {
@Getter(AccessLevel.PRIVATE)
@Setter(AccessLevel.PRIVATE)
private String[] hostInfo = initHostInfo();
private List<String> tags = new ArrayList<>();
private boolean enabled = true;
private List<String> managementTags = Arrays.asList("management");
private String healthCheckPath = "/health";
private String healthCheckUrl;
private String healthCheckInterval = "10s";
private String hostname = hostInfo[1];
private String[] initHostInfo() {
String[] info = new String[2];
try {
info[0] = InetAddress.getLocalHost().getHostAddress();
info[1] = InetAddress.getLocalHost().getHostName();
}
catch (UnknownHostException ex) {
log.error("Cannot get host info", ex);
}
return info;
}
}

View File

@@ -20,7 +20,6 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.AbstractDiscoveryLifecycle;
import org.springframework.cloud.consul.ConsulProperties;
import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.agent.model.NewService;
@@ -35,9 +34,9 @@ public class ConsulLifecycle extends AbstractDiscoveryLifecycle {
private ConsulClient client;
@Autowired
private ConsulProperties consulProperties;
private ConsulDiscoveryProperties properties;
@Autowired
@Autowired(required = false)
private TtlScheduler ttlScheduler;
@Autowired
@@ -53,9 +52,19 @@ public class ConsulLifecycle extends AbstractDiscoveryLifecycle {
// TODO: support port = 0 random assignment
Integer port = new Integer(getEnvironment().getProperty("server.port", "8080"));
service.setPort(port);
service.setTags(consulProperties.getTags());
service.setTags(properties.getTags());
NewService.Check check = new NewService.Check();
check.setTtl(ttlConfig.getTtl());
if (ttlConfig.isEnabled()) {
check.setTtl(ttlConfig.getTtl());
}
if (properties.getHealthCheckUrl() != null) {
check.setHttp(properties.getHealthCheckUrl());
} else {
check.setHttp(String.format("http://%s:%s%s", properties.getHostname(),
port, properties.getHealthCheckPath()));
}
check.setInterval(properties.getHealthCheckInterval());
//TODO support http check timeout
service.setCheck(check);
register(service);
}
@@ -66,7 +75,7 @@ public class ConsulLifecycle extends AbstractDiscoveryLifecycle {
management.setId(getManagementServiceId());
management.setName(getManagementServiceName());
management.setPort(getManagementPort());
management.setTags(consulProperties.getManagementTags());
management.setTags(properties.getManagementTags());
register(management);
}
@@ -74,12 +83,14 @@ public class ConsulLifecycle extends AbstractDiscoveryLifecycle {
protected void register(NewService service) {
log.info("Registering service with consul: {}", service.toString());
client.agentServiceRegister(service);
ttlScheduler.add(service);
if (ttlConfig.isEnabled() && ttlScheduler != null) {
ttlScheduler.add(service);
}
}
@Override
protected Object getConfiguration() {
return consulProperties;
return properties;
}
@Override
@@ -99,6 +110,6 @@ public class ConsulLifecycle extends AbstractDiscoveryLifecycle {
@Override
protected boolean isEnabled() {
return consulProperties.isEnabled();
return properties.isEnabled();
}
}

View File

@@ -32,6 +32,11 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
@Data
@CommonsLog
public class HeartbeatProperties {
// TODO: change enabled to default to true when I stop seeing messages like
// [WARN] agent: Check 'service:testConsulApp:xtest:8080' missed TTL, is now critical
boolean enabled = false;
@Min(1)
private int ttlValue = 30;