org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties Java Examples

The following examples show how to use org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties. 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: ConsulAutoRegistration.java    From spring-cloud-consul with Apache License 2.0 7 votes vote down vote up
public static ConsulAutoRegistration managementRegistration(
		AutoServiceRegistrationProperties autoServiceRegistrationProperties,
		ConsulDiscoveryProperties properties, ApplicationContext context,
		List<ConsulManagementRegistrationCustomizer> managementRegistrationCustomizers,
		HeartbeatProperties heartbeatProperties) {
	NewService management = new NewService();
	management.setId(getManagementServiceId(properties, context));
	management.setAddress(properties.getHostname());
	management
			.setName(getManagementServiceName(properties, context.getEnvironment()));
	management.setPort(getManagementPort(properties, context));
	management.setTags(properties.getManagementTags());
	management.setEnableTagOverride(properties.getManagementEnableTagOverride());
	management.setMeta(properties.getManagementMetadata());
	if (properties.isRegisterHealthCheck()) {
		management.setCheck(createCheck(getManagementPort(properties, context),
				heartbeatProperties, properties));
	}
	ConsulAutoRegistration registration = new ConsulAutoRegistration(management,
			autoServiceRegistrationProperties, properties, context,
			heartbeatProperties, managementRegistrationCustomizers);
	managementCustomize(managementRegistrationCustomizers, registration);
	return registration;
}
 
Example #2
Source File: ConsulAutoRegistration.java    From spring-cloud-consul with Apache License 2.0 7 votes vote down vote up
public static void setCheck(NewService service,
		AutoServiceRegistrationProperties autoServiceRegistrationProperties,
		ConsulDiscoveryProperties properties, ApplicationContext context,
		HeartbeatProperties heartbeatProperties) {
	if (properties.isRegisterHealthCheck() && service.getCheck() == null) {
		Integer checkPort;
		if (shouldRegisterManagement(autoServiceRegistrationProperties, properties,
				context)) {
			checkPort = getManagementPort(properties, context);
		}
		else {
			checkPort = service.getPort();
		}
		Assert.notNull(checkPort, "checkPort may not be null");
		service.setCheck(createCheck(checkPort, heartbeatProperties, properties));
	}
}
 
Example #3
Source File: SidecarConsulAutoRegistration.java    From spring-cloud-alibaba with Apache License 2.0 6 votes vote down vote up
public SidecarConsulAutoRegistration(NewService service,
		AutoServiceRegistrationProperties autoServiceRegistrationProperties,
		ConsulDiscoveryProperties properties, ApplicationContext context,
		HeartbeatProperties heartbeatProperties,
		List<ConsulManagementRegistrationCustomizer> managementRegistrationCustomizers) {
	super(service, autoServiceRegistrationProperties, properties, context,
			heartbeatProperties, managementRegistrationCustomizers);
}
 
Example #4
Source File: ConsulAutoRegistration.java    From spring-cloud-consul with Apache License 2.0 6 votes vote down vote up
public static NewService.Check createCheck(Integer port,
		HeartbeatProperties ttlConfig, ConsulDiscoveryProperties properties) {
	NewService.Check check = new NewService.Check();
	if (StringUtils.hasText(properties.getHealthCheckCriticalTimeout())) {
		check.setDeregisterCriticalServiceAfter(
				properties.getHealthCheckCriticalTimeout());
	}
	if (ttlConfig.isEnabled()) {
		// FIXME 3.0.0
		// https://github.com/spring-cloud/spring-cloud-consul/issues/614
		check.setTtl(ttlConfig.getTtl().getSeconds() + "s");
		return check;
	}

	Assert.notNull(port, "createCheck port must not be null");
	Assert.isTrue(port > 0, "createCheck port must be greater than 0");

	if (properties.getHealthCheckUrl() != null) {
		check.setHttp(properties.getHealthCheckUrl());
	}
	else {
		check.setHttp(String.format("%s://%s:%s%s", properties.getScheme(),
				properties.getHostname(), port, properties.getHealthCheckPath()));
	}
	check.setHeader(properties.getHealthCheckHeaders());
	check.setInterval(properties.getHealthCheckInterval());
	check.setTimeout(properties.getHealthCheckTimeout());
	check.setTlsSkipVerify(properties.getHealthCheckTlsSkipVerify());
	return check;
}
 
Example #5
Source File: ConsulAutoRegistration.java    From spring-cloud-consul with Apache License 2.0 6 votes vote down vote up
private static Map<String, String> getMetadata(ConsulDiscoveryProperties properties) {
	LinkedHashMap<String, String> metadata = new LinkedHashMap<>();
	if (!CollectionUtils.isEmpty(properties.getMetadata())) {
		metadata.putAll(properties.getMetadata());
	}

	// add metadata from other properties. See createTags above.
	if (!StringUtils.isEmpty(properties.getInstanceZone())) {
		metadata.put(properties.getDefaultZoneMetadataName(),
				properties.getInstanceZone());
	}
	if (!StringUtils.isEmpty(properties.getInstanceGroup())) {
		metadata.put("group", properties.getInstanceGroup());
	}

	// store the secure flag in the tags so that clients will be able to figure
	// out whether to use http or https automatically
	metadata.put("secure",
			Boolean.toString(properties.getScheme().equalsIgnoreCase("https")));

	return metadata;
}
 
Example #6
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 #7
Source File: GrpcConsulRegistrar.java    From grpc-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
private ConsulRegistration getRegistration(GRpcServerInitializedEvent event) {
    ApplicationContext applicationContext = event.getApplicationContext();


    ConsulDiscoveryProperties consulProperties = applicationContext.getBean(ConsulDiscoveryProperties.class);
    GRpcServerProperties gRpcServerProperties = event.getApplicationContext().getBean(GRpcServerProperties.class);

    NewService grpcService = new NewService();
    grpcService.setPort(event.getServer().getPort());
    if (!consulProperties.isPreferAgentAddress()) {
        grpcService.setAddress(consulProperties.getHostname());
    }
    String appName = "grpc-" + ConsulAutoRegistration.getAppName(consulProperties, applicationContext.getEnvironment());
    grpcService.setName(ConsulAutoRegistration.normalizeForDns(appName));
    grpcService.setId("grpc-" + ConsulAutoRegistration.getInstanceId(consulProperties, applicationContext));
    grpcService.setTags(ConsulAutoRegistration.createTags(consulProperties)
            .stream()
            .filter(t->!t.startsWith("secure="))
            .collect(Collectors.toList())
    );


    if(consulProperties.isRegisterHealthCheck()) {
        GRpcConsulHealthCheck health = GRpcConsulHealthCheck.builder()
                .socketAddr(consulProperties.getHostname() + ":" + event.getServer().getPort())
                .grpcUseTlc(Objects.nonNull(gRpcServerProperties.getSecurity()))
                .interval(consulProperties.getHealthCheckInterval())
                .timeout(consulProperties.getHealthCheckTimeout())
                .build();

        health.setDeregisterCriticalServiceAfter(consulProperties.getHealthCheckCriticalTimeout());

        grpcService.setCheck(health);
    }



    return new ConsulRegistration(grpcService, consulProperties);
}
 
Example #8
Source File: ConsulAutoRegistration.java    From spring-cloud-consul with Apache License 2.0 6 votes vote down vote up
public ConsulAutoRegistration(NewService service,
		AutoServiceRegistrationProperties autoServiceRegistrationProperties,
		ConsulDiscoveryProperties properties, ApplicationContext context,
		HeartbeatProperties heartbeatProperties,
		List<ConsulManagementRegistrationCustomizer> managementRegistrationCustomizers) {
	super(service, properties);
	this.autoServiceRegistrationProperties = autoServiceRegistrationProperties;
	this.context = context;
	this.heartbeatProperties = heartbeatProperties;
	this.managementRegistrationCustomizers = managementRegistrationCustomizers;
}
 
Example #9
Source File: ConsulLightminClientDiscoveryConfiguration.java    From spring-batch-lightmin with Apache License 2.0 6 votes vote down vote up
@Bean
@ConditionalOnMissingBean(ConsulAutoRegistration.class)
public ConsulAutoRegistration consulAutoRegistration(
        final MetaDataExtender metaDataExtender,
        final AutoServiceRegistrationProperties autoServiceRegistrationProperties,
        final ConsulDiscoveryProperties consulDiscoveryProperties,
        final ApplicationContext applicationContext,
        final List<ConsulRegistrationCustomizer> consulRegistrationCustomizers,
        final List<ConsulManagementRegistrationCustomizer> managementRegistrationCustomizers,
        final HeartbeatProperties heartbeatProperties) {

    metaDataExtender.extendMetaData();
    return ConsulAutoRegistration.registration(
            autoServiceRegistrationProperties,
            consulDiscoveryProperties,
            applicationContext,
            consulRegistrationCustomizers,
            managementRegistrationCustomizers,
            heartbeatProperties);
}
 
Example #10
Source File: ConsulAutoRegistration.java    From spring-cloud-consul with Apache License 2.0 6 votes vote down vote up
@Deprecated
public ConsulAutoRegistration(NewService service,
		AutoServiceRegistrationProperties autoServiceRegistrationProperties,
		ConsulDiscoveryProperties properties, ApplicationContext context,
		HeartbeatProperties heartbeatProperties) {
	this(service, autoServiceRegistrationProperties, properties, context,
			heartbeatProperties, Collections.emptyList());
}
 
Example #11
Source File: ConsulServiceRegistryAutoConfiguration.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public ConsulServiceRegistry consulServiceRegistry(ConsulClient consulClient,
		ConsulDiscoveryProperties properties, HeartbeatProperties heartbeatProperties,
		@Autowired(required = false) TtlScheduler ttlScheduler) {
	return new ConsulServiceRegistry(consulClient, properties, ttlScheduler,
			heartbeatProperties);
}
 
Example #12
Source File: ConsulAutoRegistration.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
/**
 * @param properties discovery properties
 * @param env Spring environment
 * @return the service name of the Management Service
 */
public static String getManagementServiceName(ConsulDiscoveryProperties properties,
		Environment env) {
	final String appName = properties.getServiceName();
	if (StringUtils.hasText(appName)) {
		return normalizeForDns(
				appName + SEPARATOR + properties.getManagementSuffix());
	}
	return normalizeForDns(getAppName(properties, env)) + SEPARATOR
			+ properties.getManagementSuffix();
}
 
Example #13
Source File: ConsulAutoRegistration.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
/**
 * @param properties discovery properties
 * @param context Spring application context
 * @return the port of the Management Service
 */
public static Integer getManagementPort(ConsulDiscoveryProperties properties,
		ApplicationContext context) {
	// If an alternate external port is specified, use it instead
	if (properties.getManagementPort() != null) {
		return properties.getManagementPort();
	}
	return ManagementServerPortUtils.getPort(context);
}
 
Example #14
Source File: ConsulAutoServiceRegistration.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
public ConsulAutoServiceRegistration(ConsulServiceRegistry serviceRegistry,
		AutoServiceRegistrationProperties autoServiceRegistrationProperties,
		ConsulDiscoveryProperties properties, ConsulAutoRegistration registration) {
	super(serviceRegistry, autoServiceRegistrationProperties);
	this.properties = properties;
	this.registration = registration;
}
 
Example #15
Source File: ConsulServiceRegistry.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
public ConsulServiceRegistry(ConsulClient client,
		ConsulDiscoveryProperties properties, TtlScheduler ttlScheduler,
		HeartbeatProperties heartbeatProperties) {
	this.client = client;
	this.properties = properties;
	this.ttlScheduler = ttlScheduler;
	this.heartbeatProperties = heartbeatProperties;
}
 
Example #16
Source File: ConsulAutoRegistration.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
/**
 * @param properties consul discovery properties
 * @param env Spring environment
 * @return the app name, currently the spring.application.name property
 */
public static String getAppName(ConsulDiscoveryProperties properties,
		Environment env) {
	final String appName = properties.getServiceName();
	if (StringUtils.hasText(appName)) {
		return appName;
	}
	return env.getProperty("spring.application.name", "application");
}
 
Example #17
Source File: ConsulAutoServiceRegistrationAutoConfiguration.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public ConsulAutoServiceRegistration consulAutoServiceRegistration(
		ConsulServiceRegistry registry,
		AutoServiceRegistrationProperties autoServiceRegistrationProperties,
		ConsulDiscoveryProperties properties,
		ConsulAutoRegistration consulRegistration) {
	return new ConsulAutoServiceRegistration(registry,
			autoServiceRegistrationProperties, properties, consulRegistration);
}
 
Example #18
Source File: ConsulAutoServiceRegistrationAutoConfiguration.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public ConsulAutoRegistration consulRegistration(
		AutoServiceRegistrationProperties autoServiceRegistrationProperties,
		ConsulDiscoveryProperties properties, ApplicationContext applicationContext,
		ObjectProvider<List<ConsulRegistrationCustomizer>> registrationCustomizers,
		ObjectProvider<List<ConsulManagementRegistrationCustomizer>> managementRegistrationCustomizers,
		HeartbeatProperties heartbeatProperties) {
	return ConsulAutoRegistration.registration(autoServiceRegistrationProperties,
			properties, applicationContext, registrationCustomizers.getIfAvailable(),
			managementRegistrationCustomizers.getIfAvailable(), heartbeatProperties);
}
 
Example #19
Source File: ConsulAutoServiceRegistrationCustomizedTests.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
@Bean
public CustomAutoRegistration consulAutoServiceRegistration(
		ConsulServiceRegistry serviceRegistry,
		AutoServiceRegistrationProperties autoServiceRegistrationProperties,
		ConsulDiscoveryProperties properties,
		ConsulAutoRegistration registration) {
	return new CustomAutoRegistration(serviceRegistry,
			autoServiceRegistrationProperties, properties, registration);
}
 
Example #20
Source File: ConsulAutoServiceRegistrationCustomizedTests.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
@Autowired
public CustomAutoRegistration(ConsulServiceRegistry serviceRegistry,
		AutoServiceRegistrationProperties autoServiceRegistrationProperties,
		ConsulDiscoveryProperties properties,
		ConsulAutoRegistration registration) {
	super(serviceRegistry, autoServiceRegistrationProperties, properties,
			registration);
}
 
Example #21
Source File: TestConsulDiscoveryClientBootstrapConfiguration.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
@Bean
public ConsulDiscoveryClient consulDiscoveryClient(
		ConsulDiscoveryProperties properties) {
	ConsulDiscoveryClient client = mock(ConsulDiscoveryClient.class);
	ServiceInstance instance = new DefaultServiceInstance("configserver1",
			"configserver", properties.getHostname(), properties.getPort(), false);
	given(client.getInstances("configserver")).willReturn(Arrays.asList(instance));
	return client;
}
 
Example #22
Source File: ConsulConfigServerAutoConfigurationTests.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
@Test
public void offByDefault() {
	this.context = new AnnotationConfigApplicationContext(
			ConsulConfigServerAutoConfiguration.class);
	assertThat(
			this.context.getBeanNamesForType(ConsulDiscoveryProperties.class).length)
					.isEqualTo(0);
}
 
Example #23
Source File: ConsulConfigServerAutoConfigurationTests.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
@Test
public void onWhenRequested() {
	setup("spring.cloud.config.server.prefix=/config",
			"spring.cloud.consul.discovery.tags-as-metadata=false");
	assertThat(
			this.context.getBeanNamesForType(ConsulDiscoveryProperties.class).length)
					.isEqualTo(1);
	ConsulDiscoveryProperties properties = this.context
			.getBean(ConsulDiscoveryProperties.class);
	assertThat(properties.getMetadata()).containsEntry("configPath", "/config");
}
 
Example #24
Source File: ConsulConfigServerAutoConfigurationTests.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
private void setup(String... env) {
	this.context = new SpringApplicationBuilder(
			PropertyPlaceholderAutoConfiguration.class,
			ConsulConfigServerAutoConfiguration.class, ConfigServerProperties.class,
			ConsulDiscoveryProperties.class).web(WebApplicationType.NONE)
					.properties(env).run();
}
 
Example #25
Source File: ConsulAutoRegistration.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
/**
 * @param autoServiceRegistrationProperties registration properties
 * @param properties discovery properties
 * @param context Spring application context
 * @return if the management service should be registered with the
 * {@link ServiceRegistry}
 */
public static boolean shouldRegisterManagement(
		AutoServiceRegistrationProperties autoServiceRegistrationProperties,
		ConsulDiscoveryProperties properties, ApplicationContext context) {
	return autoServiceRegistrationProperties.isRegisterManagement()
			&& getManagementPort(properties, context) != null
			&& ManagementServerPortUtils.isDifferent(context);
}
 
Example #26
Source File: SidecarConsulAutoConfiguration.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Bean
public ConsulAutoRegistration consulRegistration(
		AutoServiceRegistrationProperties autoServiceRegistrationProperties,
		ConsulDiscoveryProperties properties, ApplicationContext applicationContext,
		ObjectProvider<List<ConsulRegistrationCustomizer>> registrationCustomizers,
		ObjectProvider<List<ConsulManagementRegistrationCustomizer>> managementRegistrationCustomizers,
		HeartbeatProperties heartbeatProperties,
		SidecarProperties sidecarProperties) {
	return SidecarConsulAutoRegistration.registration(
			autoServiceRegistrationProperties, properties, applicationContext,
			registrationCustomizers.getIfAvailable(),
			managementRegistrationCustomizers.getIfAvailable(), heartbeatProperties,
			sidecarProperties);
}
 
Example #27
Source File: SidecarConsulAutoConfiguration.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Bean
public SidecarDiscoveryClient sidecarDiscoveryClient(
		ConsulDiscoveryProperties properties, ConsulServiceRegistry serviceRegistry,
		ConsulAutoRegistration registration) {
	return new SidecarConsulDiscoveryClient(properties, serviceRegistry,
			registration);
}
 
Example #28
Source File: ConsulStubsRegistrar.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
public ConsulStubsRegistrar(StubRunning stubRunning, ConsulClient consulClient,
		StubMapperProperties stubMapperProperties,
		ConsulDiscoveryProperties consulDiscoveryProperties, InetUtils inetUtils) {
	this.stubRunning = stubRunning;
	this.consulClient = consulClient;
	this.stubMapperProperties = stubMapperProperties;
	this.consulDiscoveryProperties = consulDiscoveryProperties;
	this.inetUtils = inetUtils;
}
 
Example #29
Source File: StubRunnerSpringCloudConsulAutoConfiguration.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
@Bean(initMethod = "registerStubs")
public StubsRegistrar stubsRegistrar(StubRunning stubRunning,
		ConsulClient consulClient, StubMapperProperties stubMapperProperties,
		ConsulDiscoveryProperties consulDiscoveryProperties, InetUtils inetUtils) {
	return new ConsulStubsRegistrar(stubRunning, consulClient, stubMapperProperties,
			consulDiscoveryProperties, inetUtils);
}
 
Example #30
Source File: ConsulAutoRegistration.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
public static String getInstanceId(ConsulDiscoveryProperties properties,
		ApplicationContext context) {
	if (!StringUtils.hasText(properties.getInstanceId())) {
		return normalizeForDns(IdUtils.getDefaultInstanceId(context.getEnvironment(),
				properties.isIncludeHostnameInInstanceId()));
	}
	return normalizeForDns(properties.getInstanceId());
}