该文档主要针对nacos的配置中心功能,演示springBoot、springCloud集成nacos
Nacos 支持基于 DNS 和基于 RPC 的服务发现(可以作为springcloud的注册中心)、动态配置服务(可以做配置中心)、动态 DNS 服务。
nacos官方文档
NameSpace:针对不同环境定义不同命名空间如果不指定,则默认使用public
命名空间,如需指定命名空间,需要在配置文件中提前配置。
Data Id:可以理解为配置文件名,是nacos配置中心中最小粒度,使用Data Id可以定位到一个配置文件
Group:分组名,使用dataId+group可以定位到一个配置文件,如不指定group则默认使用DEFAULT_GROUP
,如需要指定分组,需要在配置文件中提前配置
相关依赖
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>nacos-examplesartifactId>
<groupId>com.alibaba.nacosgroupId>
<version>0.2.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>nacos-spring-boot-exampleartifactId>
<packaging>pompackaging>
<modules>
<module>nacos-spring-boot-config-examplemodule>
<module>nacos-spring-boot-config-mysql-examplemodule>
<module>nacos-spring-boot-discovery-examplemodule>
modules>
<properties>
<spring-boot.version>2.0.3.RELEASEspring-boot.version>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
<maven.compiler.compilerVersion>1.8maven.compiler.compilerVersion>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>${spring-boot.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
project>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>nacos-spring-boot-exampleartifactId>
<groupId>com.alibaba.nacosgroupId>
<version>0.2.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>nacos-spring-boot-config-exampleartifactId>
<properties>
<nacos-config-spring-boot.version>0.2.1nacos-config-spring-boot.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>com.alibaba.bootgroupId>
<artifactId>nacos-config-spring-boot-starterartifactId>
<version>${nacos-config-spring-boot.version}version>
dependency>
<dependency>
<groupId>com.alibaba.bootgroupId>
<artifactId>nacos-config-spring-boot-actuatorartifactId>
<version>${nacos-config-spring-boot.version}version>
dependency>
dependencies>
project>
配置文件(application.properties)
# 填写nacos服务ip
nacos.config.server-addr={你自己的ip}:8848
# endpoint http://localhost:8080/actuator/nacos-config
# health http://localhost:8080/actuator/health
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
#spring.cloud.nacos.config.namespace=
#spring.cloud.nacos.config.group=
启动类
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@NacosPropertySource(dataId = "example", autoRefreshed = true)
public class NacosConfigApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConfigApplication.class, args);
}
}
controller层(用于演示动态更新)
import com.alibaba.nacos.api.config.annotation.NacosValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
@Controller
@RequestMapping("config")
public class ConfigController {
@NacosValue(value = "${useLocalCache:false}", autoRefreshed = true)
private boolean useLocalCache;
@NacosValue(value = "${aaa.bbb:test}", autoRefreshed = true)
private String test;
@RequestMapping(value = "/get", method = GET)
@ResponseBody
public void get() {
System.out.println(useLocalCache);
System.out.println(test);
}
}
nacos配置中心
相关依赖
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>nacos-examplesartifactId>
<groupId>com.alibaba.nacosgroupId>
<version>0.2.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>nacos-spring-boot-exampleartifactId>
<packaging>pompackaging>
<modules>
<module>nacos-spring-boot-config-examplemodule>
<module>nacos-spring-boot-config-mysql-examplemodule>
<module>nacos-spring-boot-discovery-examplemodule>
modules>
<properties>
<spring-boot.version>2.0.3.RELEASEspring-boot.version>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
<maven.compiler.compilerVersion>1.8maven.compiler.compilerVersion>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>${spring-boot.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
project>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>nacos-spring-cloud-exampleartifactId>
<groupId>com.alibaba.nacosgroupId>
<version>0.2.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>nacos-spring-cloud-config-exampleartifactId>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
<version>0.2.1.RELEASEversion>
dependency>
dependencies>
project>
配置文件(注意配置文件类型:bootstrap.properties)
配置文件与dataId的规则(配置文件拼接出的字符串需要与dataId相等配置才会生效)
# 指定nacos服务ip
spring.cloud.nacos.config.server-addr={你自己的ip}:8848
# 配置应用名
spring.application.name=example
#spring.cloud.nacos.config.prefix
# 该处随便写,但需要与dataId保持一致
#spring.profiles.active
# 配置 配置文件的类型
spring.cloud.nacos.config.file-extension=properties
#spring.cloud.nacos.config.file-extension=yaml
# 指定命名空间
#spring.cloud.nacos.config.namespace=
# 指定分组
#spring.cloud.nacos.config.group=
启动类(与springBoot不同的地方在于不需要多余的注解了)
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class NacosConfigApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConfigApplication.class, args);
}
}
controller层(演示动态更新,与springBoot不同在于可以使用Spring提供的@value进行注入,但需要在配置类上添加@RefreshScope注解)
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
@RequestMapping("/config")
@RefreshScope
public class ConfigController {
@Value("${useLocalCache:false}")
private boolean useLocalCache;
@RequestMapping("/get")
public boolean get() {
return useLocalCache;
}
}
所需依赖
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
<version>0.2.0.RELEASEversion>
dependency>
代码
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
public class ConfigExample {
public static void main(String[] args) throws NacosException, InterruptedException {
//nacos服务
String serverAddr = "{你自己的ip}:8848";
//nacos中配置的dotaId
String dataId = "application";
//nacos中配置的组名
String group = "DEFAULT_GROUP";
Properties properties = new Properties();
properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
ConfigService configService = NacosFactory.createConfigService(properties);
//入参 : dataId、group、读取超时 手动获取配置
// String content = configService.getConfig(dataId, group, 5000);
// System.out.println(content);
//使用API监听配置文件
configService.addListener(dataId, group, new Listener() {
@Override
public void receiveConfigInfo(String configInfo) {
System.out.println("recieve:" +configInfo);
}
@Override
public Executor getExecutor() {
return null;
}
});
//发布配置
// boolean isPublishOk = configService.publishConfig(dataId, group, "content");
// System.out.println(isPublishOk);
// Thread.sleep(3000);
//再次获取验证是否发布成功
// content = configService.getConfig(dataId, group, 5000);
// System.out.println(content);
//删除配置
// boolean isRemoveOk = configService.removeConfig(dataId, group);
// System.out.println(isRemoveOk);
// Thread.sleep(3000);
//验证是否删除成功
// content = configService.getConfig(dataId, group, 5000);
// System.out.println(content);
Thread.sleep(100000000);
}
}