springcloud继承nacos注册实现

springcloudcommon包中有一个类org.springframework.cloud.client.serviceregistry.ServiceRegistry他是spingcloud提供的服务注册标准,继承到springcloud中实现服务注册的组件,都会实现该接口

public interface ServiceRegistry<R extends Registration> {
     
    void register(R registration);

    void deregister(R registration);

    void close();

    void setStatus(R registration, String status);

    <T> T getStatus(R registration);
}

集成nacos中有一个实现类,com.alibaba.cloud.nacos.registry.NacosServiceRegistry
探究下注册动作触发的时机
springcloudcommon包的META-INF/spring.factories中自动装配信息包括了
org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration 这个就是服务注册相关的配置类

@Configuration(
    proxyBeanMethods = false
)
@Import({
     AutoServiceRegistrationConfiguration.class})
@ConditionalOnProperty(
    value = {
     "spring.cloud.service-registry.auto-registration.enabled"},
    matchIfMissing = true
)
public class AutoServiceRegistrationAutoConfiguration {
     
    @Autowired(
        required = false
    )
    private AutoServiceRegistration autoServiceRegistration;
    @Autowired
    private AutoServiceRegistrationProperties properties;

    public AutoServiceRegistrationAutoConfiguration() {
     
    }

    @PostConstruct
    protected void init() {
     
        if (this.autoServiceRegistration == null && this.properties.isFailFast()) {
     
            throw new IllegalStateException("Auto Service Registration has been requested, but there is no AutoServiceRegistration bean");
        }
    }
}

在其中注入了 AutoServiceRegistration实例,查看继承关系可以看出,AbstractAutoServiceRegistraion抽象类实现了该接口,并且NacosAutoServiceRegtistration继承了该抽象类,AbstractAutoServiceRegistraion继承了ApplicationListener

@FunctionalInterface
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
     
    void onApplicationEvent(E var1);
}

其中方法的作用是监听某个指定的事件,而AbstractAutoServiceRegistraion实现了该抽象方法,并且监听了,WebServerInitializedEvent事件,当Webserver初始化完成之后,调用 this.bind(event)方法
继续跟进最终会调用NacosServiceRegistry.register方法进行服务注册

你可能感兴趣的:(spring,boot)