ConsulServer support server group (#252)

Fixes gh-248
This commit is contained in:
Jin Zhang
2016-11-29 03:43:44 +08:00
committed by Spencer Gibb
parent 5eaf6aa87a
commit e1641b44ab
5 changed files with 128 additions and 22 deletions

View File

@@ -109,6 +109,9 @@ public class ConsulDiscoveryProperties {
/** Service instance zone */
private String instanceZone;
/** Service instance group*/
private String instanceGroup;
/**
* Service instance zone comes from metadata.
* This allows changing the metadata tag name.

View File

@@ -213,6 +213,9 @@ public class ConsulLifecycle extends AbstractDiscoveryLifecycle {
if (!StringUtils.isEmpty(properties.getInstanceZone())) {
tags.add(properties.getDefaultZoneMetadataName() + "=" + properties.getInstanceZone());
}
if (!StringUtils.isEmpty(properties.getInstanceGroup())) {
tags.add("group=" + properties.getInstanceGroup());
}
return tags;
}

View File

@@ -31,10 +31,12 @@ public class ConsulServer extends Server {
private final MetaInfo metaInfo;
private final HealthService service;
private final Map<String, String> metadata;
public ConsulServer(final HealthService healthService) {
super(findHost(healthService), healthService.getService().getPort());
this.service = healthService;
this.metadata = ConsulServerUtils.getMetadata(this.service);
metaInfo = new MetaInfo() {
@Override
public String getAppName() {
@@ -43,7 +45,7 @@ public class ConsulServer extends Server {
@Override
public String getServerGroup() {
return null;
return getMetadata().get("group");
}
@Override
@@ -70,7 +72,7 @@ public class ConsulServer extends Server {
}
public Map<String, String> getMetadata() {
return ConsulServerUtils.getMetadata(this.service);
return metadata;
}
public boolean isPassingChecks() {

View File

@@ -0,0 +1,84 @@
/*
* 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.util.List;
import java.util.Map;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.agent.model.Service;
import com.netflix.client.config.DefaultClientConfigImpl;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
* @author Jin Zhang
*/
@RunWith(SpringJUnit4ClassRunner.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@SpringApplicationConfiguration(classes = TestPropsConfig.class)
@WebIntegrationTest(value = { "spring.application.name=myTestService-WithGroup",
"spring.cloud.consul.discovery.instanceId=myTestService1-WithGroup",
"spring.cloud.consul.discovery.instanceGroup=test"}, randomPort = true)
public class ConsulLifecycleCustomizedInstanceGroupTests {
@Autowired
ConsulLifecycle lifecycle;
@Autowired
ConsulClient consul;
@Autowired
ApplicationContext context;
@Autowired
ConsulDiscoveryProperties properties;
@Test
public void contextLoads() {
Response<Map<String, Service>> response = consul.getAgentServices();
Map<String, Service> services = response.getValue();
Service service = services.get("myTestService1-WithGroup");
assertNotNull("service was null", service);
assertNotEquals("service port is 0", 0, service.getPort().intValue());
assertEquals("service id was wrong", "myTestService1-WithGroup", service.getId());
assertTrue("service group was wrong", service.getTags().contains("group=test"));
ConsulServerList serverList = new ConsulServerList(consul, properties);
DefaultClientConfigImpl config = new DefaultClientConfigImpl();
config.setClientName("myTestService-WithGroup");
serverList.initWithNiwsConfig(config);
List<ConsulServer> servers = serverList.getInitialListOfServers();
assertEquals("servers was wrong size", 1, servers.size());
assertEquals("service group was wrong", "test", servers.get(0).getMetaInfo().getServerGroup());
}
}

View File

@@ -36,47 +36,38 @@ import static org.junit.Assert.assertThat;
* @author Spencer Gibb
*/
public class ConsulServerListTests {
private final String name = "consulServerListTestsService";
@Test
public void tagsWork() {
String name = "consulServerListTestsService";
ConsulClient consul = new ConsulClient();
NewService nonTagged = new NewService();
nonTagged.setAddress("localhost");
nonTagged.setId(name+"NonTagged");
nonTagged.setName(name);
nonTagged.setPort(8080);
NewService tagged = new NewService();
tagged.setAddress("localhost");
tagged.setId(name+"Tagged");
tagged.setName(name);
tagged.setPort(9080);
NewService nonTagged = createService("NonTagged", 8080, null);
String tag = "mytag";
tagged.setTags(Arrays.asList(tag));
NewService tagged = createService("Tagged", 9080, Arrays.asList(tag));
NewService withZone = new NewService();
withZone.setAddress("localhost");
withZone.setId(name+"WithZone");
withZone.setName(name);
withZone.setPort(10080);
String zone = "myzone";
withZone.setTags(Arrays.asList("zone=" + zone));
NewService withZone = createService("WithZone", 10080, Arrays.asList("zone=" + zone));
String group = "test";
NewService withGroup = createService("WithGroup", 11080, Arrays.asList("group=" + group));
try {
consul.agentServiceRegister(nonTagged);
consul.agentServiceRegister(tagged);
consul.agentServiceRegister(withZone);
consul.agentServiceRegister(withGroup);
InetUtils inetUtils = new InetUtils(new InetUtilsProperties());
DefaultClientConfigImpl config = new DefaultClientConfigImpl();
config.setClientName(tagged.getName());
config.setClientName(name);
ConsulServerList serverList = new ConsulServerList(consul, new ConsulDiscoveryProperties(inetUtils));
serverList.initWithNiwsConfig(config);
List<ConsulServer> servers = serverList.getInitialListOfServers();
assertThat("servers was wrong size", servers, hasSize(3));
assertThat("servers was wrong size", servers, hasSize(4));
int serverWithZoneCount = 0;
for (ConsulServer server : servers) {
@@ -96,10 +87,21 @@ public class ConsulServerListTests {
assertThat("servers was wrong size", servers, hasSize(1));
ConsulServer server = servers.get(0);
assertThat("server was wrong", server.getPort(), is(9080));
// test server group
serverList = new ConsulServerList(consul, getProperties(name, "group=" + group, inetUtils));
serverList.initWithNiwsConfig(config);
servers = serverList.getInitialListOfServers();
assertThat("servers was wrong size", servers, hasSize(1));
server = servers.get(0);
assertThat("server was wrong", server.getPort(), is(11080));
assertThat("server group was wrong", server.getMetaInfo().getServerGroup(), is(group));
} finally {
consul.agentServiceDeregister(nonTagged.getId());
consul.agentServiceDeregister(tagged.getId());
consul.agentServiceDeregister(withZone.getId());
consul.agentServiceDeregister(withGroup.getId());
}
}
@@ -110,4 +112,16 @@ public class ConsulServerListTests {
properties.setServerListQueryTags(map);
return properties;
}
private NewService createService(String id, int port, List<String> tags) {
NewService service = new NewService();
service.setName(name);
service.setId(name + id);
service.setAddress("localhost");
service.setPort(port);
if (tags != null) {
service.setTags(tags);
}
return service;
}
}