1、引入依赖
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>0.2.7</version>
</dependency>
2、在启动类加上
@NacosPropertySource(dataId="nacos-config-example",groupId="DEFAULT_GROUP",autoRefreshed=true,type= ConfigType.YAML)
3、查看nacos配置
4、启动该服务,报了一个错,但是服务正常启动了
2024-07-16 15:58:04.870 INFO [main] com.alibaba.nacos.client.config.impl.Limiter.<clinit>:61 : limitTime:5.0
2024-07-16 15:58:04.884 ERROR [main] com.alibaba.nacos.client.config.impl.ClientWorker.queryConfig:976 : [fixed-127.0.0.1_8848] [sub-server-error] dataId=nacos-config-example, group=DEFAULT_GROUP, tenant=, code=Response{resultCode=500, errorCode=403, message='user not found!', requestId='null'}
2024-07-16 15:58:04.885 ERROR [main] com.alibaba.nacos.spring.util.NacosUtils.getContent:338 : Can't get content from dataId : nacos-config-example , groupId : DEFAULT_GROUP
com.alibaba.nacos.api.exception.NacosException: http error, code=403,msg=user not found!,dataId=nacos-config-example,group=DEFAULT_GROUP,tenant=
at com.alibaba.nacos.client.config.impl.ClientWorker$ConfigRpcTransportClient.queryConfig(ClientWorker.java:979)
at com.alibaba.nacos.client.config.impl.ClientWorker.getServerConfig(ClientWorker.java:396)
at com.alibaba.nacos.client.config.NacosConfigService.getConfigInner(NacosConfigService.java:166)
at com.alibaba.nacos.client.config.NacosConfigService.getConfig(NacosConfigService.java:94)
at com.alibaba.nacos.spring.context.event.config.EventPublishingConfigService.getConfig(EventPublishingConfigService.java:60)
at com.alibaba.nacos.spring.util.NacosUtils.getContent(NacosUtils.java:334)
at com.alibaba.nacos.spring.util.config.NacosConfigLoader.load(NacosConfigLoader.java:92)
at com.alibaba.nacos.spring.core.env.AbstractNacosPropertySourceBuilder.doBuild(AbstractNacosPropertySourceBuilder.java:181)
at com.alibaba.nacos.spring.core.env.AbstractNacosPropertySourceBuilder.build(AbstractNacosPropertySourceBuilder.java:118)
at com.alibaba.nacos.spring.core.env.NacosPropertySourcePostProcessor.buildNacosPropertySources(NacosPropertySourcePostProcessor.java:151)
at com.alibaba.nacos.spring.core.env.NacosPropertySourcePostProcessor.processPropertySource(NacosPropertySourcePostProcessor.java:133)
at com.alibaba.nacos.spring.core.env.NacosPropertySourcePostProcessor.postProcessBeanFactory(NacosPropertySourcePostProcessor.java:118)
at com.alibaba.nacos.spring.util.NacosBeanUtils.invokeNacosPropertySourcePostProcessor(NacosBeanUtils.java:416)
at com.alibaba.nacos.spring.context.annotation.config.NacosConfigBeanDefinitionRegistrar.registerBeanDefinitions(NacosConfigBeanDefinitionRegistrar.java:73)
at org.springframework.context.annotation.ImportBeanDefinitionRegistrar.registerBeanDefinitions(ImportBeanDefinitionRegistrar.java:86)
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.lambda$loadBeanDefinitionsFromRegistrars$1(ConfigurationClassBeanDefinitionReader.java:384)
at java.util.LinkedHashMap.forEach(LinkedHashMap.java:684)
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsFromRegistrars(ConfigurationClassBeanDefinitionReader.java:383)
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:148)
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:120)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:332)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:237)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:280)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:96)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:707)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:533)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:755)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:402)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:312)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1247)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1236)
at org.example.NacosTestApplication.main(NacosTestApplication.java:26)
2024-07-16 15:58:04.886 WARN [main] c.a.n.spring.core.env.AnnotationNacosPropertySourceBuilder.doBuild:185 : There is no content for NacosPropertySource from dataId[nacos-config-example] , groupId[DEFAULT_GROUP] , properties[{accessKey=${nacos.access-key:}, clusterName=${nacos.cluster-name:}, configLongPollTimeout=${nacos.configLongPollTimeout:}, configRetryTime=${nacos.configRetryTime:}, contextPath=${nacos.context-path:}, enableRemoteSyncConfig=${nacos.enableRemoteSyncConfig:}, encode=${nacos.encode:UTF-8}, endpoint=${nacos.endpoint:}, maxRetry=${nacos.maxRetry:}, namespace=${nacos.namespace:}, password=${nacos.password:}, secretKey=${nacos.secret-key:}, serverAddr=${nacos.server-addr:}, username=${nacos.username:}}].
2024-07-16 15:58:04.891 INFO [main] com.alibaba.nacos.client.config.impl.CacheData.<clinit>:65 : nacos.cache.data.init.snapshot = true
5、接口层代码如下:
package org.example.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope //加了这个注解之后,@value注解才可以实时的获取配置中心的数据
public class TestController {
@Value("${key:dd}")
private String config;
@RequestMapping("/getconfig")
public String getMyconfig() {
return config;
}
}
6、访问http://localhost:18080/getconfig
没有读到nacos中的配置