alibaba-nacos-discovery 延迟注册到nacos

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完成");

    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容