To use only local configuration in your Spring Boot application

To use only local configuration in your Spring Boot application and avoid fetching configuration from a Nacos server, you can adjust your application settings to disable Nacos configuration fetching. Here's how you can configure it:


1. Exclude Nacos Config in Dependencies

If you're using spring-cloud-starter-alibaba-nacos-config, remove it from your pom.xml or build.gradle. This ensures the application doesn't fetch configurations from Nacos.

For example, in Maven:

<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <scope>provided</scope> </dependency>


2. Disable Nacos Config Fetching

If you need to keep the dependency (e.g., for service discovery), explicitly disable the Nacos configuration fetching by setting the following properties in application.yml or application.properties:

spring: cloud: nacos: config: enabled: false

This configuration disables the Nacos configuration feature while allowing other Nacos functionalities like service discovery.


3. Use Only Local Configurations

Ensure your local configurations are defined in application.yml or application.properties. For example:

server: port: 8080 app: name: LocalConfigApp datasource: url: jdbc:mysql://localhost:3306/mydb username: root password: password


4. Explicitly Set Profile (Optional)

If your application has multiple environments, set the active profile explicitly to use local configurations:

spring: profiles: active: local

And create an environment-specific configuration file such as application-local.yml:

app: environment: local


5. Verify at Startup

When you start your application, check the logs to ensure that it is not fetching any configuration from Nacos.

Look for:

  • No attempts to connect to Nacos (NacosPropertySource logs).
  • Only local configuration being loaded.

6. (Optional) Remove Nacos Config Beans

If the above methods still cause Nacos config to interfere, you can explicitly exclude the related auto-configuration classes:

@SpringBootApplication(exclude = { NacosConfigBootstrapConfiguration.class }) public class LocalConfigApplication { public static void main(String[] args) { SpringApplication.run(LocalConfigApplication.class, args); } }

相关推荐
筏镜23 分钟前
调整docker bridge地址冲突,通过bip调整 bridge地址
java·docker·eureka
winner888131 分钟前
git merge 冲突 解决 show case
java·git·git merge·git冲突
AI人H哥会Java2 小时前
【Spring】Spring的模块架构与生态圈—Spring MVC与Spring WebFlux
java·开发语言·后端·spring·架构
毕设资源大全2 小时前
基于SpringBoot+html+vue实现的林业产品推荐系统【源码+文档+数据库文件+包部署成功+答疑解惑问到会为止】
java·数据库·vue.js·spring boot·后端·mysql·html
Watermelon_Mr2 小时前
Spring(三)-SpringWeb-概述、特点、搭建、运行流程、组件、接受请求、获取请求数据、特殊处理、拦截器
java·后端·spring
eternal__day3 小时前
数据结构十大排序之(冒泡,快排,并归)
java·数据结构·算法
唐墨1233 小时前
golang自定义MarshalJSON、UnmarshalJSON 原理和技巧
开发语言·后端·golang
凡人的AI工具箱3 小时前
每天40分玩转Django:Django测试
数据库·人工智能·后端·python·django·sqlite
qyq13 小时前
Django框架与ORM框架
后端·python·django
Theodore_10224 小时前
3 需求分析
java·开发语言·算法·java-ee·软件工程·需求分析·需求