Nacos 默认使用嵌入式数据库进行数据存储,它支持改为外部Mysql存储。(修改的是Nacos配置文件)
spring.datasource.platform=mysql
### Count of DB:
db.num=1
### Connect URL of DB:
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos_config?
characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&au
toReconnect=true
db.user=root
db.password=123456
(1)安装3个或3个以上的Nacos,复制解压后的nacos文件夹,分别命名为nacos-01、nacos-02、nacos-03
(2)修改配置文件
nacos.inetutils.ip-address=127.0.0.1
# 集群节点配置
127.0.0.1:8848
127.0.0.1:8849
127.0.0.1:8850
sh startup.sh -m cluster
之前:Spring Cloud Config + Bus
有Nacos之后,分布式配置就简单很多
Github不需要了(配置信息直接配置在Nacos server中),Bus也不需要了(依然可以完成动态刷新)。
接下来
Nacos server 添加配置集

Nacos 服务端已经搭建完毕,那么我们可以在我们的微服务中开启 Nacos 配置管理
- <dependency>
- <groupId>com.alibaba.cloudgroupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
- dependency>
通过 Namespace + Group + dataId 来锁定配置⽂件,Namespace不指定就默认public,Group不指定就默认 DEFAULT_GROUP。
dataId 的完整格式如下
${prefix}-${spring.profile.active}.${file-extension}
spring:
cloud:
nacos:
discovery:
# 集群中各节点信息都配置在这⾥(域名-VIP-绑定映射到各个实例的地址信息)
server-addr: 127.0.0.1:8848
config:
server-addr: 127.0.0.1:8848
namespace: f965f7e4-7294-40cf-825c-ef363c269d37
group: DEFAULT_GROUP
file-extension: yaml
- package com.lagou.edu.controller;
-
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.cloud.context.config.annotation.RefreshScope;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- /**
- * 该类用于模拟,我们要使用共享的那些配置信息做一些事情
- */
- @RestController
- @RequestMapping("/config")
- @RefreshScope
- public class ConfigController {
-
- // 和取本地配置信息一样
- @Value("${lagou.message}")
- private String lagouMessage;
- @Value("${mysql.url}")
- private String mysqlUrl;
-
- @Value("${abc.test}")
- private String abctest;
- @Value("${def.test}")
- private String deftest;
-
- @Value("${java.first}")
- private String javafirst;
-
-
-
- // 内存级别的配置信息
- // 数据库,redis配置信息
-
- @GetMapping("/viewconfig")
- public String viewconfig() {
-
- return "lagouMessage==>" + lagouMessage + " mysqlUrl=>" + mysqlUrl
- + " abctest=>" + abctest + " deftest=>" + deftest + " javafirst=>" + javafirst;
- }
- }
注意:上述代码获取的是我们在Nacos中自己定义的配置文件(这里省略配置文件)
思考:一个微服务希望从配置中心Nacos server中获取多个dataId的配置信息,可以的,扩展多个dataId。
- server:
- port: 8083
- spring:
- application:
- name: lagou-service-resume
- # nacos配置
- cloud:
- nacos:
- discovery:
- # 集群中各节点信息都配置在这里(域名-VIP-绑定映射到各个实例的地址信息)
- server-addr: 127.0.0.1:8848,127.0.0.1:8849,127.0.0.1:8850
-
- # nacos config 配置
- config:
- server-addr: 127.0.0.1:8848,127.0.0.1:8849,127.0.0.1:8850
- # 锁定server端的配置文件(读取它的配置项)
- namespace: 07137f0a-bf66-424b-b910-20ece612395a # 命名空间id
- group: DEFAULT_GROUP # 默认分组就是DEFAULT_GROUP,如果使用默认分组可以不配置
- file-extension: yaml #默认properties
- # 根据规则拼接出来的dataId效果:lagou-service-resume.yaml
- ext-config[0]:
- data-id: abc.yaml # 在Nacos中定义的配置文件名
- group: DEFAULT_GROUP
- refresh: true #开启扩展dataId的动态刷新
- ext-config[1]:
- data-id: def.yaml
- group: DEFAULT_GROUP
- refresh: true #开启扩展dataId的动态刷新
-
-
优先级:根据规则生成的dataId > 扩展的dataId(对于扩展的dataId,[n] n越大优先级越高)