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); } }

相关推荐
码事漫谈几秒前
AI智能体全球应用调查报告:从“对话”到“做事”的变革
后端
Chen不旧5 分钟前
easyexcel实现excel读取
java·excel·easyexcell
码界奇点19 分钟前
Spring Web MVC构建现代Java Web应用的基石
java·前端·spring·设计规范
绝无仅有38 分钟前
某大厂跳动Java面试真题之问题与解答总结(二)
后端·面试·github
绝无仅有39 分钟前
某大厂跳动Java面试真题之问题与解答总结(三)
后端·面试·架构
板板正1 小时前
EasyExcel实现普通导入导出以及按模板导出excel文件
java·excel
野犬寒鸦1 小时前
从零起步学习Redis || 第十章:主从复制的实现流程与常见问题处理方案深层解析
java·服务器·数据库·redis·后端·缓存
luopandeng1 小时前
amd npt技术 对比 intel ept 技术
java·linux·网络
编啊编程啊程1 小时前
兼职管理平台
java·spring boot·rpc·dubbo·nio
吃饭最爱1 小时前
java项目中前后端结合的要点
java