sequence
This article focuses on deregisterInstance of NacosNamingService
NacosNamingService
Nacos – 1.1.3 / client/SRC/main/Java/com/alibaba/nacos/client/naming/NacosNamingService. Java
public class NacosNamingService implements NamingService {
private static final String DEFAULT_PORT = "8080";
private static final long DEFAULT_HEART_BEAT_INTERVAL = TimeUnit.SECONDS.toMillis(5);
/**
* Each Naming instance should have different namespace.
*/
private String namespace;
private String endpoint;
private String serverList;
private String cacheDir;
private String logName; private HostReactor hostReactor; private BeatReactor beatReactor; private EventDispatcher eventDispatcher; private NamingProxy serverProxy; / /... @Override public void deregisterInstance(String serviceName, String ip, int port) throws NacosException { deregisterInstance(serviceName, ip, port, Constants.DEFAULT_CLUSTER_NAME); } @Override public void deregisterInstance(String serviceName, String groupName, String ip, int port) throws NacosException { deregisterInstance(serviceName, groupName, ip, port, Constants.DEFAULT_CLUSTER_NAME); } @Override public void deregisterInstance(String serviceName, String ip, int port, String clusterName) throws NacosException { deregisterInstance(serviceName, Constants.DEFAULT_GROUP, ip, port, clusterName); } @Override public void deregisterInstance(String serviceName, String groupName, String ip, int port, String clusterName) throws NacosException { Instance instance = new Instance(); instance.setIp(ip); instance.setPort(port); instance.setClusterName(clusterName); deregisterInstance(serviceName, groupName, instance); } @Override public void deregisterInstance(String serviceName, Instance instance) throws NacosException { deregisterInstance(serviceName, Constants.DEFAULT_GROUP, instance); } @Override public void deregisterInstance(String serviceName, String groupName, Instance instance) throws NacosException {if(instance.isEphemeral()) { beatReactor.removeBeatInfo(NamingUtils.getGroupedName(serviceName, groupName), instance.getIp(), instance.getPort()); } serverProxy.deregisterService(NamingUtils.getGroupedName(serviceName, groupName), instance); } / /... }Copy the code
- DeregisterInstance method for ephemeral instance (default is ephemeral) its BeatInfo will be removed from the beatReactor, then through serverProxy. DeregisterService for cancellation
BeatReactor
Nacos – 1.1.3 / client/SRC/main/Java/com/alibaba/nacos/client/naming/beat/BeatReactor. Java
public class BeatReactor { private ScheduledExecutorService executorService; private NamingProxy serverProxy; public final Map<String, BeatInfo> dom2Beat = new ConcurrentHashMap<String, BeatInfo>(); / /... public void removeBeatInfo(String serviceName, String ip, int port) { NAMING_LOGGER.info("[BEAT] removing beat: {}:{}:{} from beat map.", serviceName, ip, port);
BeatInfo beatInfo = dom2Beat.remove(buildKey(serviceName, ip, port));
if (beatInfo == null) {
return;
}
beatInfo.setStopped(true); MetricsMonitor.getDom2BeatSizeMonitor().set(dom2Beat.size()); } / /... class BeatTask implements Runnable { BeatInfo beatInfo; public BeatTask(BeatInfo beatInfo) { this.beatInfo = beatInfo; } @Override public voidrun() {
if (beatInfo.isStopped()) {
return; } long result = serverProxy.sendBeat(beatInfo); long nextTime = result > 0 ? result : beatInfo.getPeriod(); executorService.schedule(new BeatTask(beatInfo), nextTime, TimeUnit.MILLISECONDS); }} / /... }Copy the code
- The removeBeatInfo method removes the specified key from dom2Beat, marking it as stopped to true; The BeatTask checks whether the status is stopped, and sends heartbeat information through ServerProxy. sendBeat to calculate the next scheduling time and add scheduling tasks to the executorService
NamingProxy
Nacos – 1.1.3 / client/SRC/main/Java/com/alibaba/nacos/client/naming/net/NamingProxy. Java
public class NamingProxy { private static final int DEFAULT_SERVER_PORT = 8848; private int serverPort = DEFAULT_SERVER_PORT; private String namespaceId; private String endpoint; private String nacosDomain; private List<String> serverList; private List<String> serversFromEndpoint = new ArrayList<String>(); private long lastSrvRefTime = 0L; private long vipSrvRefInterMillis = TimeUnit.SECONDS.toMillis(30); private Properties properties; / /... public void deregisterService(String serviceName, Instance instance) throws NacosException { NAMING_LOGGER.info("[DEREGISTER-SERVICE] {} deregistering service {} with instance: {}",
namespaceId, serviceName, instance);
final Map<String, String> params = new HashMap<String, String>(8);
params.put(CommonParams.NAMESPACE_ID, namespaceId);
params.put(CommonParams.SERVICE_NAME, serviceName);
params.put(CommonParams.CLUSTER_NAME, instance.getClusterName());
params.put("ip", instance.getIp());
params.put("port", String.valueOf(instance.getPort()));
params.put("ephemeral", String.valueOf(instance.isEphemeral())); reqAPI(UtilAndComs.NACOS_URL_INSTANCE, params, HttpMethod.DELETE); } / /... }Copy the code
- The deregisterService method of NamingProxy sends a DELETE request to the /instance interface for service logout
summary
The deregisterInstance method of the NacosNamingService removes the BeatInfo from the beatReactor for ephemeral’s instance(which is ephemeral by default). Then through serverProxy. DeregisterService for cancellation
doc
- NacosNamingService