Re-register service in consul if the service get's de-registered
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?
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?
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.
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.
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.
Knowing when to re-register is the problem then.
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?
how do you know a 500 should reregister?
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
@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.
The same issue.
Not sure I'm keen on adding something like this. This should be a function of the platform.
Has there been any progress or a work around solution to this problem?
No, there hasn't. See my comments above.
"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?
+1
+1
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();
}
}
}
+1 this would be extremely helpful as we end up restarting our spring applications when redeploying/upgrading consul agents.
+1
+1