use of com.ecwid.consul.v1.agent.model.NewService in project motan by weibocom.
the class ConsulEcwidClient method registerService.
@Override
public void registerService(ConsulService service) {
NewService newService = convertService(service);
client.agentServiceRegister(newService);
}
use of com.ecwid.consul.v1.agent.model.NewService in project motan by weibocom.
the class ConsulEcwidClient method lookupHealthService.
@Override
public ConsulResponse<List<ConsulService>> lookupHealthService(String serviceName, long lastConsulIndex) {
QueryParams queryParams = new QueryParams(ConsulConstants.CONSUL_BLOCK_TIME_SECONDS, lastConsulIndex);
Response<List<HealthService>> orgResponse = client.getHealthServices(serviceName, true, queryParams);
ConsulResponse<List<ConsulService>> newResponse = null;
if (orgResponse != null && orgResponse.getValue() != null && !orgResponse.getValue().isEmpty()) {
List<HealthService> HealthServices = orgResponse.getValue();
List<ConsulService> ConsulServcies = new ArrayList<ConsulService>(HealthServices.size());
for (HealthService orgService : HealthServices) {
try {
ConsulService newService = convertToConsulService(orgService);
ConsulServcies.add(newService);
} catch (Exception e) {
String servcieid = "null";
if (orgService.getService() != null) {
servcieid = orgService.getService().getId();
}
LoggerUtil.error("convert consul service fail. org consulservice:" + servcieid, e);
}
}
if (!ConsulServcies.isEmpty()) {
newResponse = new ConsulResponse<List<ConsulService>>();
newResponse.setValue(ConsulServcies);
newResponse.setConsulIndex(orgResponse.getConsulIndex());
newResponse.setConsulLastContact(orgResponse.getConsulLastContact());
newResponse.setConsulKnownLeader(orgResponse.isConsulKnownLeader());
}
}
return newResponse;
}
use of com.ecwid.consul.v1.agent.model.NewService 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.agent.model.NewService in project spring-cloud-consul by spring-cloud.
the class ConsulServerListTests method createService.
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;
}
use of com.ecwid.consul.v1.agent.model.NewService in project spring-cloud-consul by spring-cloud.
the class ConsulDiscoveryClientDefaultQueryTagTests method serviceForEnvironment.
private NewService serviceForEnvironment(String env, int port) {
NewService service = new NewService();
service.setAddress("localhost");
service.setId(NAME + env);
service.setName(NAME);
service.setPort(port);
service.setTags(Arrays.asList(env));
return service;
}
Aggregations