SpringCloudAlibaba-Nacos服务注册的原理

nacos是阿里巴巴开源的一个集注册中心和配置中心为一体的组件,先已加入到apache大家庭中。本文主要介绍nacos作为注册中心中的服务注册原理

首先,所有的服务注册功能,都使用一个相同的接口---ServiceRegistry

package org.springframework.cloud.client.serviceregistry;

/**
 * Contract to register and deregister instances with a Service Registry.
 *
 * @author Spencer Gibb
 * @since 1.2.0
 */
public interface ServiceRegistry<R extends Registration> {

	/**
	 * Register the registration. Registrations typically have information about
	 * instances such as: hostname and port.
	 * @param registration the registraion
	 */
	void register(R registration);

	/**
	 * Deregister the registration.
	 * @param registration
	 */
	void deregister(R registration);

	/**
	 * Close the ServiceRegistry. This a lifecycle method.
	 */
	void close();

	/**
	 * Sets the status of the registration. The status values are determined
	 * by the individual implementations.
	 *
	 * @see org.springframework.cloud.client.serviceregistry.endpoint.ServiceRegistryEndpoint
	 * @param registration the registration to update
	 * @param status the status to set
	 */
	void setStatus(R registration, String status);

	/**
	 * Gets the status of a particular registration.
	 *
	 * @see org.springframework.cloud.client.serviceregistry.endpoint.ServiceRegistryEndpoint
	 * @param registration the registration to query
	 * @param <T> the type of the status
	 * @return the status of the registration
	 */
	<T> T getStatus(R registration);
}

这个接口中最重要的方法就是register()服务注册方法和deregister()服务注销方法

可以看到,这个方法是在类org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration中被调用

具体调用的方法在bind()方法中,调用了start()方法,然后在start()中调用了registry()

@EventListener(WebServerInitializedEvent.class)
	public void bind(WebServerInitializedEvent event) {
		ApplicationContext context = event.getApplicationContext();
		if (context instanceof ConfigurableWebServerApplicationContext) {
			if ("management".equals(
					((ConfigurableWebServerApplicationContext) context).getServerNamespace())) {
				return;
			}
		}
		this.port.compareAndSet(0, event.getWebServer().getPort());
		this.start();
	}
public void start() {
		if (!isEnabled()) {
			i
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值