万世威 2020-10-16
1、docker 安装
docker pull nacos/nacos-server
docker run --env MODE=standalone --name nacos -d -p 8848:8848 nacos/nacos-server
2、本地安装
# 下载地址
https://github.com/alibaba/nacos/releases
运行 bin 目录下 startup.sh 或 startup.cmd
sh startup.sh -m standalone 不加参数,默认使用集群模式
3、使用
4、测试
# 发布配置
curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test&content=helloWorld&namespace=7daffe6f-e9c1-4986-a059-601c6d53d70d"
# 获取配置
curl -X GET "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test"
# 详细配置
https://nacos.io/zh-cn/docs/open-api.html
0、配置模型
通过 namespace、group、dataId 唯一确定一个配置
1、命名空间
2、新建配置
3、导出、导入配置
4、历史版本
5、配置修改监听
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.1.3</version>
</dependency>
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import java.util.Properties;
import java.util.concurrent.Executor;
public class SimpleDemoMain {
public static void main(String[] args) throws NacosException {
String server = "127.0.0.1:8848";
String dataId = "com.dist";
String group = "test";
String namespace = "7daffe6f-e9c1-4986-a059-601c6d53d70d";
Properties properties = new Properties();
properties.setProperty("serverAddr", server);
properties.setProperty("namespace", namespace);
ConfigService configService = NacosFactory.createConfigService(properties);
String config = configService.getConfig(dataId, group, 5000L);
System.out.println(config);
// 监听修改配置
configService.addListener(dataId, group, new Listener() {
@Override
public Executor getExecutor() {
return null;
}
@Override
public void receiveConfigInfo(String configInfo) {
System.out.println(configInfo);
}
});
while (true) {
try {
Thread.sleep(2000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/** 输出
server:
port: 8085
server:
port: 8088
*/
1、Pom 配置
<!-- parent pom.xml -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2、配置文件
spring:
application:
name: service0
cloud:
nacos:
config:
file-extension: yaml
server-addr: 127.0.0.1:8848
namespace: 7daffe6f-e9c1-4986-a059-601c6d53d70d # 可选
group: dist
3、实例
@RestController
public class NacosController {
@Value("${dist.name}")
private String config;
@Autowired
private ConfigurableApplicationContext configContext;
@GetMapping("/config")
public String getConfig() {
return config;
}
@GetMapping("/config2")
public String getConfig2() {
return configContext.getEnvironment().getProperty("dist.name");
}
}
spring.cloud.nacos.config.refresh.enabled=false
==来关闭动态刷新==4、支持 profile 粒度的配置
Nacos Config 在加载配置的时候,不仅仅加载了以 DataId 为 ${spring.application.name}.${file-extension:properties}
为前缀的基础配置,还加载了 DataId 为 ${spring.application.name}-${profile}.${file-extension:properties}
的基础配置。在日常开发中如果遇到多套环境下的不同配置,可以通过 Spring 提供的 ${spring.profiles.active}
这个配置项来配置
spring.profiles.active=develop
${spring.profiles.active} 当通过配置文件来指定时放在 bootstrap.properties|yml 文件中
spring.profiles.active 优先级比 spring.application.name.file-extension 优先级高
5、自定义扩展的 Data Id 配置
# 获取配置从上述配置的 namespace 中获取,默认(public)
# 顺序优先级参考 第 7 条
ext-config[0]:
data-id: ext-config-common01.properties
ext-config[1]:
data-id: ext-config-common02.properties
group: GLOBALE_GROUP
ext-config[2]:
data-id: ext-config-common03.properties
group: GLOBALE_GROUP
refresh: true
6、共享 Data Id 配置
# 只能获取 DEFAULT_GROUP 下配置,不常用
# 获取的是 上面配置的 namespace 下的配置(没有配置 namespace 默认是 public)
# common1、common2 有相同配置,以后面的为主
# 具体优先级看 第 7 条
shared-dataids: ext-config-common01.properties,ext-config-common02.properties
refreshable-dataids: ext-config-common03.properties
7、配置的优先级
Nacos Config 目前提供了三种配置能力从 Nacos 拉取相关的配置
spring.cloud.nacos.config.shared-dataids
支持多个共享 Data Id 的配置spring.cloud.nacos.config.ext-config[n].data-id
的方式支持多个扩展 Data Id 的配置当三种方式共同使用时,他们的一个优先级关系是:A < B < C
其中 n 的值越大,优先级越高
ext-config[n].data-id 必须带上文件扩展名(common.properties)
https://github.com/alibaba/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-config.adoc
# nacos 中 application.yaml(dataid) -- dist(group)配置
dist:
name: wanshw
dubbo:
scan:
base-packages: com.dist.nacos.web
application:
name: consumer
registry:
address: nacos://127.0.0.1:8848
provider:
version: 1.0.0
consumer:
version: 1.0.0
# nacos 中 application-service.yaml(dataid) -- dist(group)配置
dubbo:
registry:
address: nacos://127.0.0.1:8848
consumer:
version: 1.0.0
provider:
version: 1.0.0
scan:
base-packages: com.dist.service
application:
name: service
protocol:
name: dubbo
server:
port: 8090
https://github.com/alibaba/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc
1、取消登录验证
2、外部持久化配置
docker pull mysql:5.7
docker run --name mysql -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -d mysql:5.7
# 连接上此数据库
# 默认账号密码: root/123456
# 创建一个数据库
create database nacos_config;
# 执行 conf/nacos-mysql.sql
# 在 conf/application.properties 下新增以下配置
db.num=1
db.url.0=jdbc:mysql://localhost:3306/nacos_config?characterEncoding=utf8
db.user=root
db.password=123456
3、集群部署
集群模式下需要配置数据库,目前只支持 mysql, 版本要求:5.6.5+
检查 conf/application.properties 端口号是不是冲突
修改 conf 目录下 cluster.conf.example -> cluster.conf
添加 主机 IP + 端口号, 复制到其它机器上
ip 不能使用 127.0.0.1,改成真实 IP 即可
https://nacos.io/zh-cn/index.html
https://github.com/alibaba/nacos
https://nacos.io/zh-cn/docs/what-is-nacos.html