spring-cloud-consul icon indicating copy to clipboard operation
spring-cloud-consul copied to clipboard

Re-register service in consul if the service get's de-registered

Open mirwais27 opened this issue 10 years ago • 34 comments

If a local consul agent is restarted (for what ever reason) then the local Spring Boot Apps (with spring-cloud-consul inside) does not a re-connect. Anything special that I missed to configure for having this?

mirwais27 avatar Jun 14 '16 14:06 mirwais27

I just tried it and works ok. There are no persistent connections to consul, so the next time it tries to connect, it should work. Are you seeing any errors?

spencergibb avatar Jun 14 '16 23:06 spencergibb

We are starting the consul agent like that: consul.exe agent -log-level info -data-dir .\consul-data -server -ui -ui-dir=.\consul-ui -bootstrap -advertise=127.0.0.1. We configured also the retry feature in pur spring boot apps as mentioned here: http://cloud.spring.io/spring-cloud-static/spring-cloud.html#spring-cloud-consul-retry The issue is that from time to time we update or reinstall the consul agent - with deleting the consul-data folder. And if we start then the consul agent then the spring boot apps can't reconnect: The consul-ui http://localhost:8500/ shows only the consul agent itself, but not the running services anymore. Workaround is to restart all spring boot apps as well. But if you have many of them then it's really annoying.

It would be good if the spring boot apps re-register again automatically if consul is running again and they are not registered yet. Would make spring boot apps even more resilient.

mirwais27 avatar Aug 10 '16 16:08 mirwais27

with deleting the consul-data folder

is the key statement there. So you're not asking for a reconnection, that works. You're asking for reregistration.

spencergibb avatar Aug 10 '16 17:08 spencergibb

As you describe: re-register even if consul agent is started with a fresh empty consul-data folder would be really nice! A general re-connect of spring boot apps seems to work.

mirwais27 avatar Aug 11 '16 08:08 mirwais27

Knowing when to re-register is the problem then.

spencergibb avatar Aug 11 '16 14:08 spencergibb

I have the same problem, when my consul instance is restarted, spring boot app is sending heartbeat bot service is missing and it get 500. It would be nice to have re-register feature

I tried to implement it and it seems that we can just add NewService to ConsulHeartbeatTask and make something in lines

 @Override
        public void run() {
            try {
                client.agentCheckPass(checkId);
            } catch (OperationException e) {
                if (e.getStatusCode() == 500) {
                    logger.warn("Re-register service", service.getId());
                    client.agentServiceRegister(service);
                }
            }
        }

What do you say?

pwielgolaski avatar Mar 02 '17 14:03 pwielgolaski

how do you know a 500 should reregister?

spencergibb avatar Mar 02 '17 15:03 spencergibb

Fair point, we can also check if error message contains information about missing check https://github.com/hashicorp/consul/blob/master/command/agent/acl.go#L372

pwielgolaski avatar Mar 02 '17 20:03 pwielgolaski

@spencergibb i believe, if health check attempt fails, we need to check whether service is still registered in consul. So we can re-register service if it got de-registered somehow.

avarabyeu avatar Mar 07 '17 10:03 avarabyeu

The same issue.

FlowerWrong avatar May 06 '17 15:05 FlowerWrong

Not sure I'm keen on adding something like this. This should be a function of the platform.

spencergibb avatar May 15 '17 19:05 spencergibb

Has there been any progress or a work around solution to this problem?

dabarbanel avatar Aug 04 '17 16:08 dabarbanel

No, there hasn't. See my comments above.

spencergibb avatar Aug 04 '17 16:08 spencergibb

"Simple things should be simple, complex things should be possible." ;) @spencergibb could we make it possible ? I see some reasoning behind not making it part of platform, but could we think how people that want to do it could do it?

pwielgolaski avatar Aug 12 '17 19:08 pwielgolaski

+1

askaribragimov avatar Aug 15 '17 14:08 askaribragimov

+1

deevodavis71 avatar Sep 05 '17 09:09 deevodavis71

I just did some basic investigation into this issue, and the code below seems to work OK for me in a Spring Boot app. It just relies on the endpoint being manually poked with a Spring Actuator "/refresh" command after the Consul agent has been restarted (it even works if /refresh is called multiple times!)

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.agent.model.NewService;

@Component
public class RefreshConsul implements ApplicationListener<EnvironmentChangeEvent> {

	@Autowired
	Environment environment;

	@Override
	public void onApplicationEvent(EnvironmentChangeEvent event) {

		System.out.println("\n\nREFRESHED!!!!\n\n");

		try {

			// do stuff
			ConsulClient client = new ConsulClient("localhost");

			// Get the port
			String port = environment.getProperty("local.server.port");
			if (port == null)
				return;

			// Get the app name
			String appName = environment.getProperty("spring.application.name");

			// Fix underscores
			appName = appName.replace('_', '-');

			// register new service
			NewService newService = new NewService();
			newService.setId(appName + "-" + port);
			newService.setName(appName);
			newService.setPort(Integer.parseInt(port));

			// Add the health check
			NewService.Check serviceCheck = new NewService.Check();
			serviceCheck.setHttp("http://localhost:" + port + "/health");
			serviceCheck.setInterval("10s");

			// register the health check
			newService.setCheck(serviceCheck);

			// register the service
			client.agentServiceRegister(newService);

		} catch (Exception e) {

			// Oops
			e.printStackTrace();

		}
	}

}

deevodavis71 avatar Sep 05 '17 10:09 deevodavis71

+1 this would be extremely helpful as we end up restarting our spring applications when redeploying/upgrading consul agents.

gitrc avatar Oct 18 '17 15:10 gitrc

+1

perdonare avatar Nov 22 '17 05:11 perdonare

+1

SystemOutPrint avatar May 15 '18 06:05 SystemOutPrint