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