fix #492-add hostname in instanceid (#570)
This commit is contained in:
committed by
Spencer Gibb
parent
129e8ec7fd
commit
f7d57ee1c2
@@ -115,6 +115,11 @@ public class ConsulDiscoveryProperties {
|
||||
/** Service instance group. */
|
||||
private String instanceGroup;
|
||||
|
||||
/**
|
||||
* Whether hostname is included into the default instance id when registering service.
|
||||
*/
|
||||
private boolean includeHostnameInInstanceId = false;
|
||||
|
||||
/**
|
||||
* Service instance zone comes from metadata. This allows changing the metadata tag
|
||||
* name.
|
||||
@@ -390,6 +395,14 @@ public class ConsulDiscoveryProperties {
|
||||
this.instanceGroup = instanceGroup;
|
||||
}
|
||||
|
||||
public boolean isIncludeHostnameInInstanceId() {
|
||||
return includeHostnameInInstanceId;
|
||||
}
|
||||
|
||||
public void setIncludeHostnameInInstanceId(boolean includeHostnameInInstanceId) {
|
||||
this.includeHostnameInInstanceId = includeHostnameInInstanceId;
|
||||
}
|
||||
|
||||
public String getDefaultZoneMetadataName() {
|
||||
return this.defaultZoneMetadataName;
|
||||
}
|
||||
|
||||
@@ -166,8 +166,8 @@ public class ConsulAutoRegistration extends ConsulRegistration {
|
||||
public static String getInstanceId(ConsulDiscoveryProperties properties,
|
||||
ApplicationContext context) {
|
||||
if (!StringUtils.hasText(properties.getInstanceId())) {
|
||||
return normalizeForDns(
|
||||
IdUtils.getDefaultInstanceId(context.getEnvironment(), false));
|
||||
return normalizeForDns(IdUtils.getDefaultInstanceId(context.getEnvironment(),
|
||||
properties.isIncludeHostnameInInstanceId()));
|
||||
}
|
||||
return normalizeForDns(properties.getInstanceId());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* 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.serviceregistry;
|
||||
|
||||
import com.ecwid.consul.v1.agent.model.NewService;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration;
|
||||
import org.springframework.cloud.consul.ConsulAutoConfiguration;
|
||||
import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||
|
||||
/**
|
||||
* @author varnson
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(
|
||||
classes = ConsulAutoRegistrationIncludeHostnameInInstanceIdTests.TestConfig.class,
|
||||
properties = {
|
||||
"spring.application.name=myTestService-IncludeHostnameInInstanceId",
|
||||
"spring.cloud.consul.discovery.include-hostname-in-instance-id=true",
|
||||
"spring.cloud.client.hostname=testhostname" },
|
||||
webEnvironment = RANDOM_PORT)
|
||||
public class ConsulAutoRegistrationIncludeHostnameInInstanceIdTests {
|
||||
|
||||
@Autowired
|
||||
private ConsulAutoRegistration registration;
|
||||
|
||||
@Autowired
|
||||
private ConsulDiscoveryProperties properties;
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
NewService service = this.registration.getService();
|
||||
assertThat(service).as("service was null").isNotNull();
|
||||
|
||||
NewService.Check check = service.getCheck();
|
||||
assertThat(service.getId()).as("id is null").isNotNull();
|
||||
assertThat(service.getId()).as("id no include hostname").contains("testhostname");
|
||||
assertThat(service.getId()).as("service id was wrong")
|
||||
.isEqualTo(this.registration.getInstanceId());
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ImportAutoConfiguration({ AutoServiceRegistrationConfiguration.class,
|
||||
ConsulAutoConfiguration.class,
|
||||
ConsulAutoServiceRegistrationAutoConfiguration.class })
|
||||
public static class TestConfig {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user