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

相关推荐
Мартин.5 分钟前
[Meachines] [Easy] Help HelpDeskZ-SQLI+NODE.JS-GraphQL未授权访问+Kernel<4.4.0权限提升
后端·node.js·graphql
万物皆字节17 分钟前
Springboot3 自动装配流程与核心文件:imports文件
spring boot
问道飞鱼20 分钟前
【Springboot知识】Springboot结合redis实现分布式锁
spring boot·redis·分布式
Yeats_Liao20 分钟前
Spring 框架:配置缓存管理器、注解参数与过期时间
java·spring·缓存
Yeats_Liao20 分钟前
Spring 定时任务:@Scheduled 注解四大参数解析
android·java·spring
码明20 分钟前
SpringBoot整合ssm——图书管理系统
java·spring boot·spring
某风吾起25 分钟前
Linux 消息队列的使用方法
java·linux·运维
xiao-xiang28 分钟前
jenkins-k8s pod方式动态生成slave节点
java·kubernetes·jenkins
网络风云29 分钟前
golang中的包管理-下--详解
开发语言·后端·golang
取址执行39 分钟前
Redis发布订阅
java·redis·bootstrap