美文网首页
alibaba-nacos-discovery 延迟注册到nac

alibaba-nacos-discovery 延迟注册到nac

作者: 备货仓66 | 来源:发表于2023-03-05 13:31 被阅读0次

    1、配置文件修改(关闭自动注册)

    spring:

        cloud:

            nacos:

                discovery:

                    registerEnabled: false

    2、加载配置类(手动注册)

    (注意该类,放在spring可以扫描到的包下)

    import com.alibaba.cloud.nacos.registry.NacosServiceRegistry;

    import lombok.extern.slf4j.Slf4j;

    import org.springframework.beans.BeansException;

    import org.springframework.boot.CommandLineRunner;

    import org.springframework.cloud.client.serviceregistry.Registration;

    import org.springframework.context.ApplicationContext;

    import org.springframework.context.ApplicationContextAware;

    import org.springframework.stereotype.Component;

    /**

    * @ClassName: NacosServiceRegistry

    * @Description:

    * @author: hanye

    * @date: 2023-03-06 11:12:45

    */

    @Component

    @Slf4j

    public class NacosServiceDelayRegistryimplements CommandLineRunner, ApplicationContextAware {

    /**

    * spring的上下文

    */

        private ApplicationContextapplicationContext;

        @Override

        public void setApplicationContext(ApplicationContext applicationContext)throws BeansException {

    this.applicationContext = applicationContext;

        }

    @Override

        public void run(String... args)throws Exception {

    NacosServiceRegistry nacosServiceRegistry =applicationContext.getBean(NacosServiceRegistry.class);

            Registration registration =applicationContext.getBean(Registration.class);

            nacosServiceRegistry.register(registration);

            log.info("项目加载nacos完成");

        }

    }

    相关文章

      网友评论

          本文标题:alibaba-nacos-discovery 延迟注册到nac

          本文链接:https://www.haomeiwen.com/subject/ikguldtx.html