Skip to content

Serivices won't register automatically when consul-server is started after starting spring-cloud-consul  #774

Closed

Description

Serivices wont register automatically when consul-server is started after starting spring-cloud-consul

version:spring-cloud-consul-discovery 3.1.0

I already configured spring.cloud.consul.discovery.heartbeat like this

spring.cloud.consul.discovery.heartbeat.enabled: true;
spring.cloud.consul.discovery.heartbeat.reregisterServiceOnFailure: true;
spring.cloud.consul.discovery.heartbeat.ttl: 10s;

then I checked the source code and  found a bug.
The bug is come from spring-cloud-consul-discovery package's class org.springframework.cloud.consul.serviceregistry.ConsulServiceRegistry.
In this method public void register(ConsulRegistration reg),the original code is:

@Override
    public void register(ConsulRegistration reg) {
        log.info("Registering service with consul: " + reg.getService());
        try {
            this.client.agentServiceRegister(reg.getService(), this.properties.getAclToken());
            NewService service = reg.getService();
            if (this.heartbeatProperties.isEnabled() && this.ttlScheduler != null && service.getCheck() != null
                    && service.getCheck().getTtl() != null) {
                this.ttlScheduler.add(reg.getService());
            }
        }
        catch (ConsulException e) {
            if (this.properties.isFailFast()) {
                log.error("Error registering service with consul: " + reg.getService(), e);
                ReflectionUtils.rethrowRuntimeException(e);
            }
            log.warn("Failfast is false. Error registering service with consul: " + reg.getService(), e);
        }
    }

than I changed it like this:

  @Override
  public void register(ConsulRegistration reg) {
    log.info("Registering service with consul: " + reg.getService());
    try {
      this.client.agentServiceRegister(reg.getService(), this.properties.getAclToken());

    }
    catch (ConsulException e) {
      if (this.properties.isFailFast()) {
        log.error("Error registering service with consul: " + reg.getService(), e);
        ReflectionUtils.rethrowRuntimeException(e);
      }
      log.warn("Failfast is false. Error registering service with consul: " + reg.getService(), e);
    }

    try {
      NewService service = reg.getService();
      if (this.heartbeatProperties.isEnabled() && this.ttlScheduler != null && service.getCheck() != null
          && service.getCheck().getTtl() != null) {
        this.ttlScheduler.add(reg.getService());
      }
    }
    catch (ConsulException e) {
      if (this.properties.isFailFast()) {
        log.error("Error registering service with consul: " + reg.getService(), e);
        ReflectionUtils.rethrowRuntimeException(e);
      }
      log.warn("Failfast is false. Error registering service with consul: " + reg.getService(), e);
    }
  }

Now when services is not registered successfully,the method will go to an exception,than I will add the registered services into the scheduler.
Maybe you can fix this,thank you.

Activity

  1. spencergibb commented on Mar 8, 2023

    @spencergibb
    Member

    PRs welcome

  2. Saravana-PS commented on Jun 21, 2023

    @Saravana-PS

    Hey @spencergibb , I would like to work on this issue. Can you please assign it on me?

  3. Saravana-PS commented on Jun 27, 2023

    @Saravana-PS

    Hey @spencergibb , I have few queries regarding the local setup of the project. Can we talk about this in a slack kinda environment?

  4. yashdotdev13 commented on Oct 27, 2026

    @yashdotdev13
    Contributor

    I've opened PR #1038 to address this issue. Added regression tests and verified the changes with the full Maven test suite—all tests passed. Thank you for reviewing!

  5. added this to the 5.1.0-RC2 milestone on Oct 28, 2026
  6. moved this from In Progress to Done in 2026.0.0-RC2on Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions