use of com.ecwid.consul.v1.ConsulClient in project spring-cloud-consul by spring-cloud.
the class ConsulServerListTests method tagsWork.
@Test
public void tagsWork() {
ConsulClient consul = new ConsulClient();
NewService nonTagged = createService("NonTagged", 8080, null);
String tag = "mytag";
NewService tagged = createService("Tagged", 9080, Arrays.asList(tag));
String zone = "myzone";
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(name);
ConsulServerList serverList = new ConsulServerList(consul, new ConsulDiscoveryProperties(inetUtils));
serverList.initWithNiwsConfig(config);
List<ConsulServer> servers = serverList.getInitialListOfServers();
assertThat("servers was wrong size", servers, hasSize(4));
int serverWithZoneCount = 0;
for (ConsulServer server : servers) {
if (server.getMetadata().containsKey("zone")) {
serverWithZoneCount++;
assertThat("server was wrong zone", server.getZone(), is(zone));
} else {
assertThat("server was wrong zone", server.getZone(), is(ConsulServer.UNKNOWN_ZONE));
}
}
assertThat("server was wrong zone", serverWithZoneCount, is(1));
serverList = new ConsulServerList(consul, getProperties(name, tag, inetUtils));
serverList.initWithNiwsConfig(config);
servers = serverList.getInitialListOfServers();
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());
}
}
use of com.ecwid.consul.v1.ConsulClient in project spring-cloud-consul by spring-cloud.
the class ConsulPropertySourceLocatorFilesTests method setup.
@Before
public void setup() {
this.properties = new ConsulProperties();
this.client = new ConsulClient(properties.getHost(), properties.getPort());
this.client.setKVValue(ROOT + APPLICATION_YML, "foo: bar\nmy.baz: ${foo}");
this.client.setKVValue(ROOT + APPLICATION_DEV_YML, "foo: bar-dev\nmy.baz: ${foo}");
this.client.setKVValue(ROOT + "/master.ref", UUID.randomUUID().toString());
this.client.setKVValue(ROOT + APP_NAME_PROPS, "foo: bar-app\nmy.baz: ${foo}");
this.client.setKVValue(ROOT + APP_NAME_DEV_PROPS, "foo: bar-app-dev\nmy.baz: ${foo}");
this.context = new SpringApplicationBuilder(Config.class).web(false).run("--spring.application.name=" + APP_NAME, "--spring.cloud.consul.config.prefix=" + ROOT, "--spring.cloud.consul.config.format=FILES", "--spring.profiles.active=dev", "spring.cloud.consul.config.watch.delay=1");
this.client = context.getBean(ConsulClient.class);
this.properties = context.getBean(ConsulProperties.class);
this.environment = context.getEnvironment();
}
use of com.ecwid.consul.v1.ConsulClient in project spring-cloud-consul by spring-cloud.
the class ConfigWatchTests method setupWatch.
private void setupWatch(ApplicationEventPublisher eventPublisher, GetValue getValue, String context, String aclToken) {
ConsulClient consul = mock(ConsulClient.class);
List<GetValue> getValues = null;
if (getValue != null) {
getValues = Arrays.asList(getValue);
}
Response<List<GetValue>> response = new Response<>(getValues, 1L, false, 1L);
when(consul.getKVValues(eq(context), nullable(String.class), any(QueryParams.class))).thenReturn(response);
if (StringUtils.hasText(aclToken)) {
configProperties.setAclToken(aclToken);
}
LinkedHashMap<String, Long> initialIndexes = new LinkedHashMap<>();
initialIndexes.put(context, 0L);
ConfigWatch watch = new ConfigWatch(configProperties, consul, initialIndexes);
watch.setApplicationEventPublisher(eventPublisher);
watch.start();
watch.watchConfigKeyValues();
}
use of com.ecwid.consul.v1.ConsulClient in project spring-cloud-consul by spring-cloud.
the class ConfigWatchTests method firstCallDoesNotPublishEvent.
@Test
public void firstCallDoesNotPublishEvent() {
ApplicationEventPublisher eventPublisher = mock(ApplicationEventPublisher.class);
configProperties.setFormat(FILES);
GetValue getValue = new GetValue();
String context = "/config/app.yml";
ConsulClient consul = mock(ConsulClient.class);
List<GetValue> getValues = Collections.singletonList(getValue);
Response<List<GetValue>> response = new Response<>(getValues, 1L, false, 1L);
when(consul.getKVValues(eq(context), anyString(), any(QueryParams.class))).thenReturn(response);
ConfigWatch watch = new ConfigWatch(configProperties, consul, new LinkedHashMap<String, Long>());
watch.setApplicationEventPublisher(eventPublisher);
watch.watchConfigKeyValues();
verify(eventPublisher, times(0)).publishEvent(any(RefreshEvent.class));
}
use of com.ecwid.consul.v1.ConsulClient in project spring-cloud-consul by spring-cloud.
the class ConsulPropertyPrefixTests method setup.
@Before
public void setup() {
ConsulProperties properties = new ConsulProperties();
client = new ConsulClient(properties.getHost(), properties.getPort());
}
Aggregations