alibaba-nacos-discovery 延迟注册到nac

2023-03-05  本文已影响0人  备货仓66

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

    }

}

上一篇下一篇

猜你喜欢

热点阅读