spring版本4.2.8 nacos:1.1.0 jdk 1.8
引入依赖
<dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-spring-context</artifactId> <version>1.1.1</version> </dependency>
新增nacos.properties配置
创建配置文件 nacos.properties
SERVERADDR=10.89.5.3:8848 DATAID=config NAMESPACE=27496901-a363-44ba-9d42-cbe14904b04a
新增 NacosConfiguration.java
import com.alibaba.nacos.api.annotation.NacosProperties;
import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.config.annotation.NacosConfigurationProperties; import com.alibaba.nacos.spring.context.annotation.config.EnableNacosConfig; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Configuration @Component @PropertySource("classpath:nacos.properties") @EnableNacosConfig(globalProperties = @NacosProperties(serverAddr = "${SERVERADDR}",namespace = "${NAMESPACE}")) @NacosConfigurationProperties(dataId = "${DATAID}",autoRefreshed = true) public class NacosConfiguration {
//对应配置中心文件中的配置参数 读取 使用时 使用@Authowired注入即可 使用get获取值
private boolean useLocalCache;
private String NOTIFYURL; public boolean isUseLocalCache() { return useLocalCache; } public void setUseLocalCache(boolean useLocalCache) { this.useLocalCache = useLocalCache; } public String getNOTIFYURL() { return NOTIFYURL; } public void setNOTIFYURL(String NOTIFYURL) { this.NOTIFYURL = NOTIFYURL; } }
nacos配置中心控制台新增配置文件 名称对应即可
标签:集成,NOTIFYURL,Spring,Nacos,nacos,public,import,annotation,useLocalCache From: https://www.cnblogs.com/cpx123/p/18028921