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

相关推荐
麻瓜生活睁不开眼4 小时前
Android16修改全局桌面视图边框四直角显示为弧边圆角
android·java·深度学习
dear_bi_MyOnly4 小时前
【MyBatis 操作数据库】
java·数据库·学习·mybatis·学习方法
2601_953720825 小时前
【计算机毕业设计】基于Spring Boot的心理健康咨询与评测系统的设计与实现
java·spring boot·后端
丈剑走天涯6 小时前
JDK 17 正式特性
java·开发语言
东方小月6 小时前
从零开发一个 Coding Agent(四):使用状态机校验大模型事件流
前端·人工智能·后端
圣光SG6 小时前
Java操作题练习(七)
java·开发语言·算法
都叫我大帅哥6 小时前
“让AI修就行”?我劝你收回这句话——来自一位Java博主的硬核反常识
后端
whyfail7 小时前
前端学 Spring Boot(8):接口为什么越用越慢?
前端·spring boot·后端
Muscleheng8 小时前
Spring Boot 3.x 集成 DeepSeek 实现 Function Calling(工具调用)
人工智能·spring boot·后端·ai·spring ai·deepseek