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方法进行服务注册

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值