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

相关推荐
地瓜伯伯28 分钟前
SpringBoot项目整合Elasticsearch启动失败的常见错误总结(2)
spring boot·elasticsearch·spring cloud
期待のcode37 分钟前
@RequestBody的伪表单提交场景
java·前端·vue.js·后端
气π1 小时前
【JavaWeb】——(若依+AI)-帝可得实践项目
java·spring
王中阳Go1 小时前
手把手教你用 Go + Eino 搭建一个企业级 RAG 知识库(含代码与踩坑)
人工智能·后端·go
幺零九零零1 小时前
Golang-Swagger
开发语言·后端·golang
乌暮1 小时前
JavaEE初阶--多线程案例
java·java-ee
小光学长2 小时前
基于ssm旅游管理系统的开发与设计z050cft7(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
java·数据库·旅游
moyueheng2 小时前
AG-UI 事件类型全解析:构建 AI 代理与 UI 的实时通信桥梁
后端
兔丝2 小时前
ThinkPHP8 常见并发场景解决方案文档
redis·后端