com.ecwid.consul.v1.catalog.model.CatalogService Java Examples

The following examples show how to use com.ecwid.consul.v1.catalog.model.CatalogService. 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: DiscoveryBootstrapConfigurationTests.java    From spring-cloud-vault with Apache License 2.0 8 votes vote down vote up
@BeforeClass
public static void beforeClass() {

	assumeTrue(CanConnect.to(new InetSocketAddress(CONSUL_HOST, CONSUL_PORT)));

	ConsulClient client = new ConsulClient();

	Response<List<CatalogService>> response = client.getCatalogService("vault",
			QueryParams.DEFAULT);

	if (response.getValue().isEmpty()) {

		NewService service = new NewService();
		service.setAddress("localhost");
		service.setPort(8200);
		service.setId("vault");
		service.setName("vault");

		client.agentServiceRegister(service);
	}
}
 
Example #2
Source File: AlphaConsulAutoConfiguration.java    From servicecomb-pack with Apache License 2.0 6 votes vote down vote up
/**
 * Update grpc port of consul tags after Conusl Registered
 * */
@EventListener
public void listenInstanceRegisteredEvent(InstanceRegisteredEvent instanceRegisteredEvent){
  if(alphaServerPort == 0){
    if(instanceRegisteredEvent.getConfig() instanceof ConsulDiscoveryProperties){
      ConsulDiscoveryProperties properties = (ConsulDiscoveryProperties)instanceRegisteredEvent.getConfig();
      this.consuleInstanceId = formatConsulInstanceId(properties.getInstanceId());
      Response<List<CatalogService>> services = consulClient.getCatalogService(serviceName,null);
      if(services.getValue() != null){
        services.getValue().stream().filter(service ->
            service.getServiceId().equalsIgnoreCase(this.consuleInstanceId)).forEach(service -> {

          NewService newservice =  new NewService();
          newservice.setName(service.getServiceName());
          newservice.setId(service.getServiceId());
          newservice.setAddress(service.getAddress());
          newservice.setPort(service.getServicePort());
          List<String> tags = service.getServiceTags();
          tags.remove("alpha-server-port=0");
          tags.add("alpha-server-port="+actualAlphaServerPort);
          newservice.setTags(tags);
          consulClient.agentServiceRegister(newservice);
        });
      }
    }
  }
}
 
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: CamelCloudConsulServiceRegistryTest.java    From camel-spring-boot with Apache License 2.0 4 votes vote down vote up
@Test
public void testServiceRegistry() throws Exception {
    ConfigurableApplicationContext context = new SpringApplicationBuilder(TestConfiguration.class)
        .web(WebApplicationType.NONE)
        .run(
            "--debug=false",
            "--spring.main.banner-mode=OFF",
            "--spring.application.name=" + UUID.randomUUID().toString(),
            "--ribbon.enabled=false",
            "--ribbon.eureka.enabled=false",
            "--management.endpoint.enabled=false",
            "--spring.cloud.consul.enabled=true",
            "--spring.cloud.consul.host=" + container.getContainerIpAddress(),
            "--spring.cloud.consul.port=" + container.getMappedPort(8500),
            "--spring.cloud.consul.config.enabled=false",
            "--spring.cloud.consul.discovery.enabled=true",
            "--spring.cloud.service-registry.auto-registration.enabled=false",
            "--camel.cloud.service-registry.service-host=localhost",
            "--spring.main.allow-bean-definition-overriding=true"
        );
    // TODO: Remove --spring.main.allow-bean-definition-overriding=true when new version of spring-cloud
    //  is released that supports Spring Boot 2.1 more properly

    try {
        final ConsulClient client = context.getBean(ConsulClient.class);
        final ServiceRegistry registry = context.getBean(ServiceRegistry.class);

        registry.register(
            DefaultServiceDefinition.builder()
                .withHost(SERVICE_HOST)
                .withPort(SERVICE_PORT)
                .withName(SERVICE_NAME)
                .withId(SERVICE_ID)
                .build()
        );

        List<CatalogService> services = client.getCatalogService(SERVICE_NAME, QueryParams.DEFAULT).getValue();
        
        assertThat(services).hasSize(1);
        assertThat(services).first().hasFieldOrPropertyWithValue("serviceId", SERVICE_ID);
        assertThat(services).first().hasFieldOrPropertyWithValue("serviceName", SERVICE_NAME);
        assertThat(services).first().hasFieldOrPropertyWithValue("serviceAddress", SERVICE_HOST);
        assertThat(services).first().hasFieldOrPropertyWithValue("servicePort", SERVICE_PORT);

    } finally {
        context.close();
    }
}
 
Example #5
Source File: ConsulEndpoint.java    From spring-cloud-consul with Apache License 2.0 4 votes vote down vote up
public Map<String, List<CatalogService>> getCatalogServices() {
	return this.catalogServices;
}
 
Example #6
Source File: ConsulEndpoint.java    From spring-cloud-consul with Apache License 2.0 4 votes vote down vote up
public void setCatalogServices(
		Map<String, List<CatalogService>> catalogServices) {
	this.catalogServices = catalogServices;
}