com.ecwid.consul.v1.agent.model.Service Java Examples

The following examples show how to use com.ecwid.consul.v1.agent.model.Service. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConsulClientTest.java    From consul-api with Apache License 2.0 6 votes vote down vote up
private void serviceRegisterTest(ConsulClient consulClient) {
    NewService newService = new NewService();
    String serviceName = "abc";
    newService.setName(serviceName);
    consulClient.agentServiceRegister(newService);

    Response<Map<String, Service>> agentServicesResponse = consulClient.getAgentServices();
    Map<String, Service> services = agentServicesResponse.getValue();
    assertThat(services, IsMapContaining.hasKey(serviceName));
}
 
Example #2
Source File: ConsulAutoServiceRegistrationDisabledTests.java    From spring-cloud-consul with Apache License 2.0 6 votes vote down vote up
private void testAutoRegistrationDisabled(String testName, String disableProperty) {
	new WebApplicationContextRunner().withUserConfiguration(TestConfig.class)
			.withPropertyValues("spring.application.name=" + testName,
					disableProperty + "=false", "server.port=0")
			.run(context -> {

				assertThat(context)
						.doesNotHaveBean(ConsulAutoServiceRegistration.class);
				assertThat(context)
						.doesNotHaveBean(ConsulAutoServiceRegistrationListener.class);
				assertThat(context).doesNotHaveBean(ConsulAutoRegistration.class);
				assertThat(context)
						.doesNotHaveBean(ConsulRegistrationCustomizer.class);

				ConsulClient consul = context.getBean(ConsulClient.class);

				Response<Map<String, Service>> response = consul.getAgentServices();
				Map<String, Service> services = response.getValue();
				Service service = services.get(testName);
				assertThat(service).as("service was registered").isNull();

			});
}
 
Example #3
Source File: ConsulEndpoint.java    From spring-cloud-consul with Apache License 2.0 6 votes vote down vote up
@ReadOperation
public ConsulData invoke() {
	ConsulData data = new ConsulData();
	// data.setKeyValues(kvClient.getKeyValueRecurse());
	Response<Map<String, Service>> agentServices = this.consul.getAgentServices();
	data.setAgentServices(agentServices.getValue());

	Response<Map<String, List<String>>> catalogServices = this.consul
			.getCatalogServices(CatalogServicesRequest.newBuilder()
					.setQueryParams(QueryParams.DEFAULT).build());

	for (String serviceId : catalogServices.getValue().keySet()) {
		Response<List<CatalogService>> response = this.consul
				.getCatalogService(serviceId, CatalogServiceRequest.newBuilder()
						.setQueryParams(QueryParams.DEFAULT).build());
		data.getCatalogServices().put(serviceId, response.getValue());
	}

	Response<List<Node>> catalogNodes = this.consul
			.getCatalogNodes(CatalogNodesRequest.newBuilder()
					.setQueryParams(QueryParams.DEFAULT).build());
	data.setCatalogNodes(catalogNodes.getValue());

	return data;
}
 
Example #4
Source File: ConsulAutoServiceRegistrationManagementDisabledServiceTests.java    From spring-cloud-consul with Apache License 2.0 6 votes vote down vote up
@Test
public void contextLoads() {
	Response<Map<String, Service>> response = this.consul.getAgentServices();
	Map<String, Service> services = response.getValue();

	Service mgmtService = services.get("myTestService-NM-0-management");
	assertThat(mgmtService).as("Management service was not null").isNull();

	Service service = services.get("myTestService1-NM");
	assertThat(service).as("Service was not null").isNotNull();
	assertThat(service.getPort().intValue()).as("service port was 0").isNotEqualTo(0);
	assertThat(service.getId()).as("service id was wrong")
			.isEqualTo("myTestService1-NM");
	assertThat(service.getService()).as("service name was wrong")
			.isEqualTo("myTestService-NM");
	assertThat(StringUtils.isEmpty(service.getAddress()))
			.as("service address must not be empty").isFalse();
	assertThat(service.getAddress())
			.as("service address must equals hostname from discovery properties")
			.isEqualTo(this.discoveryProperties.getHostname());

}
 
Example #5
Source File: ConsulServiceRegistryDisabledTests.java    From spring-cloud-consul with Apache License 2.0 6 votes vote down vote up
private void testAutoRegistrationDisabled(String testName, String disableProperty) {
	new WebApplicationContextRunner().withUserConfiguration(TestConfig.class)
			.withPropertyValues("spring.application.name=" + testName,
					disableProperty + "=false", "server.port=0")
			.run(context -> {
				assertThat(context).doesNotHaveBean(ConsulServiceRegistry.class);
				assertThat(context).doesNotHaveBean(HeartbeatProperties.class);

				ConsulClient consul = context.getBean(ConsulClient.class);

				Response<Map<String, Service>> response = consul.getAgentServices();
				Map<String, Service> services = response.getValue();
				Service service = services.get(testName);
				assertThat(service).as("service was registered").isNull();
			});
}
 
Example #6
Source File: ConsulAutoServiceRegistrationTests.java    From spring-cloud-consul with Apache License 2.0 6 votes vote down vote up
@Test
public void contextLoads() {
	Response<Map<String, Service>> response = this.consul.getAgentServices();
	Map<String, Service> services = response.getValue();
	Service service = services.get(this.registration.getInstanceId());
	assertThat(service).as("service was null").isNotNull();
	assertThat(service.getPort().intValue()).as("service port is 0").isNotEqualTo(0);
	assertThat(service.getId().contains(":"))
			.as("service id contained invalid character: " + service.getId())
			.isFalse();
	assertThat(service.getId()).as("service id was wrong")
			.isEqualTo(this.registration.getInstanceId());
	assertThat(service.getService()).as("service name was wrong")
			.isEqualTo("myTestService1-FF-something");
	assertThat(StringUtils.isEmpty(service.getAddress()))
			.as("service address must not be empty").isFalse();
	assertThat(service.getAddress())
			.as("service address must equals hostname from discovery properties")
			.isEqualTo(this.discoveryProperties.getHostname());
}
 
Example #7
Source File: ConsulAutoServiceRegistrationCustomizedInstanceGroupTests.java    From spring-cloud-consul with Apache License 2.0 6 votes vote down vote up
@Test
@Ignore // FIXME: 3.0.0
public void contextLoads() {
	Response<Map<String, Service>> response = this.consul.getAgentServices();
	Map<String, Service> services = response.getValue();
	Service service = services.get("myTestService1-WithGroup");
	assertThat(service).as("service was null").isNotNull();
	assertThat(service.getPort().intValue()).as("service port is 0").isNotEqualTo(0);
	assertThat(service.getId()).as("service id was wrong")
			.isEqualTo("myTestService1-WithGroup");
	assertThat(service.getTags().contains("group=test")).as("service group was wrong")
			.isTrue();

	// ConsulServerList serverList = new ConsulServerList(this.consul,
	// this.properties);
	// DefaultClientConfigImpl config = new DefaultClientConfigImpl();
	// config.setClientName("myTestService-WithGroup");
	// serverList.initWithNiwsConfig(config);
	//
	// List<ConsulServer> servers = serverList.getInitialListOfServers();
	// assertThat(servers.size()).as("servers was wrong size").isEqualTo(1);
	// assertThat(servers.get(0).getMetaInfo().getServerGroup())
	// .as("service group was wrong").isEqualTo("test");
}
 
Example #8
Source File: ConsulAutoServiceRegistrationCustomizedInstanceZoneTests.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
@Test
public void contextLoads() {
	Response<Map<String, Service>> response = this.consul.getAgentServices();
	Map<String, Service> services = response.getValue();
	Service service = services.get("myTestService1-WithZone");
	assertThat(service).as("service was null").isNotNull();
	assertThat(service.getPort().intValue()).as("service port is 0").isNotEqualTo(0);
	assertThat(service.getId()).as("service id was wrong")
			.isEqualTo("myTestService1-WithZone");
	assertThat(service.getMeta()).as("service zone was wrong").containsEntry("myZone",
			"zone1");
}
 
Example #9
Source File: ConsulAutoServiceRegistrationDefaultPortTests.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
@Test
public void contextLoads() {
	Response<Map<String, Service>> response = this.consul.getAgentServices();
	Map<String, Service> services = response.getValue();
	Service service = services.get("myTestService2-DD");
	assertThat(service).as("service was null").isNotNull();
	assertThat(service.getPort().intValue()).as("service port is 0").isNotEqualTo(0);
}
 
Example #10
Source File: ConsulAutoServiceRegistrationCustomizedAgentAddressTests.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
@Test
public void contextLoads() {
	Response<Map<String, Service>> response = this.consul.getAgentServices();
	Map<String, Service> services = response.getValue();
	Service service = services.get("myTestService1-AA");
	assertThat(service).as("service was null").isNotNull();
	assertThat(service.getPort().intValue()).as("service port is 0").isNotEqualTo(0);
	assertThat(service.getId()).as("service id was wrong")
			.isEqualTo("myTestService1-AA");
	assertThat(service.getService()).as("service name was wrong")
			.isEqualTo("myprefix-myTestService-AA");
	assertThat(StringUtils.isEmpty(service.getAddress()))
			.as("service address must be empty").isTrue();
}
 
Example #11
Source File: ConsulAutoServiceRegistrationManagementServiceTests.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
@Test
public void contextLoads() {
	final Response<Map<String, Service>> response = this.consul.getAgentServices();
	final Map<String, Service> services = response.getValue();

	final Service service = services.get("myTestService-EE-0");
	assertThat(service).as("service was null").isNotNull();
	assertThat(service.getPort().intValue()).as("service port was 0").isNotEqualTo(0);
	assertThat(service.getId()).as("service id was wrong")
			.isEqualTo("myTestService-EE-0");
	assertThat(service.getService()).as("service name was wrong")
			.isEqualTo("myTestService-EE");
	assertThat(StringUtils.isEmpty(service.getAddress()))
			.as("service address must not be empty").isFalse();
	assertThat(service.getAddress())
			.as("service address must equals hostname from discovery properties")
			.isEqualTo(this.discoveryProperties.getHostname());

	final Service managementService = services.get("myTestService-EE-0-management");
	assertThat(managementService).as("management service was null").isNotNull();
	assertThat(managementService.getPort().intValue())
			.as("management service port was wrong").isEqualTo(4452);
	assertThat(managementService.getId()).as("management service id was wrong")
			.isEqualTo("myTestService-EE-0-management");
	assertThat(managementService.getService()).as("management service name was wrong")
			.isEqualTo("myTestService-EE-management");
	assertThat(StringUtils.isEmpty(managementService.getAddress()))
			.as("management service address must not be empty").isFalse();
	assertThat(managementService.getAddress()).as(
			"management service address must equals hostname from discovery properties")
			.isEqualTo(this.discoveryProperties.getHostname());
}
 
Example #12
Source File: ConsulAutoServiceDeRegistrationDisabledTests.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
private void checkService(final boolean expected) {
	final Response<Map<String, Service>> response = this.consul.getAgentServices();
	final Map<String, Service> services = response.getValue();
	final Service service = services.get("myTestNotDeRegisteredService-D");
	if (expected) {
		assertThat(service).as("service was not registered").isNotNull();
	}
	else {
		assertThat(service).as("service was registered").isNull();
	}
}
 
Example #13
Source File: TxleConsulClient.java    From txle with Apache License 2.0 5 votes vote down vote up
public Set<String> getServersIPAndPort() {
    Set<String> serverIpPortSet = new HashSet<>(8);
    Response<Map<String, Service>> agentServices = consulClient.getAgentServices();
    if (agentServices != null) {
        Map<String, Service> serviceMap = agentServices.getValue();
        if (serviceMap != null && !serviceMap.isEmpty()) {
            serviceMap.keySet().forEach(key -> {
                Service service = serviceMap.get(key);
                serverIpPortSet.add(service.getAddress() + ":" + service.getPort());
            });
        }
    }
    return serverIpPortSet;
}
 
Example #14
Source File: ConsulAutoServiceRegistrationCustomizedPropsTests.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
@Test
public void propertiesAreCorrect() {
	Response<Map<String, Service>> response = this.consul.getAgentServices();
	Map<String, Service> services = response.getValue();
	Service service = services.get("myTestService1-B");
	assertThat(service).as("service was null").isNotNull();
	assertThat(service.getPort()).as("service port is discovery port")
			.isEqualTo(4452);
	assertThat("myTestService1-B").as("service id was wrong")
			.isEqualTo(service.getId());
	assertThat("myTestService-B").as("service name was wrong")
			.isEqualTo(service.getService());
	assertThat("myhost").as("property hostname was wrong")
			.isEqualTo(this.properties.getHostname());
	assertThat("10.0.0.1").as("property ipAddress was wrong")
			.isEqualTo(this.properties.getIpAddress());
	assertThat("myhost").as("service address was wrong")
			.isEqualTo(service.getAddress());
	assertThat(service.getEnableTagOverride())
			.as("property enableTagOverride was wrong").isTrue();
	assertThat(service.getTags()).as("property tags contains the wrong values")
			.containsExactly("mytag");
	HashMap<String, String> entries = new HashMap<>();
	entries.put("key1", "value1");
	entries.put("key2", "value2");
	entries.put("mydefaultzonemetadataname", "myzone");
	entries.put("group", "mygroup");
	entries.put("secure", "false");
	assertThat(service.getMeta()).as("property metadata contains the wrong entries")
			.containsExactlyInAnyOrderEntriesOf(entries);

	Response<List<Check>> checkResponse = this.consul.getHealthChecksForService(
			"myTestService-B", HealthChecksForServiceRequest.newBuilder()
					.setQueryParams(QueryParams.DEFAULT).build());
	List<Check> checks = checkResponse.getValue();
	assertThat(checks).as("checks was wrong size").hasSize(0);
}
 
Example #15
Source File: ConsulAutoServiceRegistrationCustomizedServletContextTests.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
@Test
public void contextLoads() {
	Response<Map<String, Service>> response = this.consul.getAgentServices();
	Map<String, Service> services = response.getValue();
	Service service = services.get("myTestService1-WithServletContext");
	assertThat(service).as("service was null").isNotNull();
	assertThat(service.getPort().intValue()).as("service port is 0").isNotEqualTo(0);
	assertThat(service.getId()).as("service id was wrong")
			.isEqualTo("myTestService1-WithServletContext");
	assertThat(service.getTags()).as("contextPath tag missing")
			.contains("contextPath=/customContext");
}
 
Example #16
Source File: ConsulAutoServiceRegistrationCustomizedServiceNameTests.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
@Test
public void contextLoads() {
	Response<Map<String, Service>> response = this.consul.getAgentServices();
	Map<String, Service> services = response.getValue();
	Service service = services.get("myTestService1-CC");
	assertThat(service).as("service was null").isNotNull();
	assertThat(service.getPort().intValue()).as("service port is 0").isNotEqualTo(0);
	assertThat(service.getId()).as("service id was wrong")
			.isEqualTo("myTestService1-CC");
	assertThat(service.getService()).as("service name was wrong")
			.isEqualTo("myprefix-myTestService-CC");
}
 
Example #17
Source File: ConsulAutoServiceRegistrationNonWebTests.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
@Test
public void contextLoads() {
	assertThat(this.autoServiceRegistration)
			.as("ConsulAutoServiceRegistration was created").isNotNull();

	Response<Map<String, Service>> response = this.consul.getAgentServices();
	Map<String, Service> services = response.getValue();
	Service service = services.get("consulNonWebTest");
	assertThat(service).as("service was registered").isNull(); // no port to listen,
																// hence no
	// registration
}
 
Example #18
Source File: ConsulAutoServiceRegistrationCustomizedManagementServicePortTests.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
@Test
public void contextLoads() {
	final Response<Map<String, Service>> response = this.consul.getAgentServices();
	final Map<String, Service> services = response.getValue();

	final Service service = services.get("myTestService1-GG");
	assertThat(service).as("service was null").isNotNull();
	assertThat(service.getPort().intValue()).as("service port was 0").isNotEqualTo(0);
	assertThat(service.getId()).as("service id was wrong")
			.isEqualTo("myTestService1-GG");
	assertThat(service.getService()).as("service name was wrong")
			.isEqualTo("myprefix-myTestService-GG");
	assertThat(StringUtils.isEmpty(service.getAddress()))
			.as("service address must not be empty").isFalse();
	assertThat(service.getAddress())
			.as("service address must equals hostname from discovery properties")
			.isEqualTo(this.discoveryProperties.getHostname());

	final Service managementService = services.get("myTestService1-GG-management");
	assertThat(managementService).as("management service was null").isNotNull();
	assertThat(managementService.getPort().intValue())
			.as("management service port is not 4452").isEqualTo(4452);
	assertThat(this.managementServerProperties.getPort().intValue())
			.as("management port is not 0").isEqualTo(0);
	assertThat(managementService.getId()).as("management service id was wrong")
			.isEqualTo("myTestService1-GG-management");
	assertThat(managementService.getService()).as("management service name was wrong")
			.isEqualTo("myprefix-myTestService-GG-management");
	assertThat(StringUtils.isEmpty(managementService.getAddress()))
			.as("management service address must not be empty").isFalse();
	assertThat(managementService.getAddress()).as(
			"management service address must equals hostname from discovery properties")
			.isEqualTo(this.discoveryProperties.getHostname());
}
 
Example #19
Source File: ConsulEndpoint.java    From spring-cloud-consul with Apache License 2.0 4 votes vote down vote up
public Map<String, Service> getAgentServices() {
	return this.agentServices;
}
 
Example #20
Source File: ConsulEndpoint.java    From spring-cloud-consul with Apache License 2.0 4 votes vote down vote up
public void setAgentServices(Map<String, Service> agentServices) {
	this.agentServices = agentServices;
}