Javaspringcloud

微服务架构 | 2.2 Alibaba Nacos 的统一配置管

2022-01-21  本文已影响0人  多氯环己烷

前言

参考资料
《Spring Microservices in Action》
《Spring Cloud Alibaba 微服务原理与实战》
《B站 尚硅谷 SpringCloud 框架开发教程 周阳》
《Nacos 官网》

Nacos 致力于解决微服务中的统一配置、服务注册与发现等问题。它提供了一组简单易用的特性集,帮助开发者快速实现动态服务发现、服务配置、服务元数据及流量管理;


1. Nacos 配置中心基础知识

1.1 Nacos 在配置中心中的功能

1.2 Nacos 配置管理 Data ID 的构成

1.3 Nacos 配置的回滚机制

Nacos 配置的回滚.png

1.4 Nacos 配置的图形化管理界面

配置列表.png

1.5 Namespace、Group、Data ID 三者的关系

Namespace、Group、Data ID 三者的关系.png

1.6 Nacos 对配置的 CRUD

配置的 CRUD.png
对配置的操作 SDK Open API 说明
发布配置 public boolean publishConfig(String dataId, String group, String content) throws NacosException POST: /nacos/v1/cs/configs 将配置保存到 Nacos Config Server 中
删除配置 public boolean removeConfig(String dataId, String group)throws NacosException DELETE: /nacos/v1/cs/configs 删除配置中心的指定配置
获取配置 public string getConfig(String dataId, String group, long timeoutMs) throws NacosException GET: /nacos/v1/cs/configs 从 Nacos Config Server 中读取配置
监听配置 public void addListener(String dataId, String group, Listener listener) POST: /nacos/v1/cs/configs/listener 订阅感兴趣的配置,当配置发生变化时可以收到一个事件

1.7 Nacos 动态监听的长轮询机制

比较项 Pull 机制 Push 机制
说明 客户端从服务端主动拉取数据 服务端主动把数据推送到客户端
缺点 不能保证数据实时性;在服务端配置长时间不更新的情况下,客户端的定时任务会做一些无效的 Pull 如果客户端的数量比较多,服务端需要耗费大量的内存资源来保存每个连接;需要心跳机制来维持每个连接状态
Nacos 动态监听的长轮询机制.png

1.8 Nacos 配置中心的源码分析

2. Nacos 基础配置

以 Data ID 方案为例,更多方案详情请见本篇第三点《3. Nacos 加载配置的三种方案》;

2.1 下载 Nacos 服务器

2.2 引入 pom.xml 依赖文件

<!--nacos-config-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

2.3 修改 yml 配置文件

# nacos配置
server:
  port: 18082

spring:
  application:
    name: nacos-config-client     #必须,构成 Nacos 配置管理 Data ID 字段的一部分
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848     #Nacos 服务注册中心地址
      config:
        server-addr: localhost:8848     #Nacos 作为配置中心地址
        file-extension: yaml     #指定 yaml 格式的配置
        
        # prefix: hhh     #Data ID 的前缀,如果不指定,就是 nacos-config-client-dev.yaml。指定后,是 hhh-dev.yaml
        # refresh: true      #是否动态刷新
        
        # group: DEFAULT_GROUP     #指定组
        # namespace: PUBLIC     #指定命名空间 ID
spring:
  profiles:
    active: dev # 表示开发环境

2.4 在主程序类上添加注解

2.5 编写业务类

@RestController
@RefreshScope //使当前类下的配置支持 Nacos 的动态刷新功能
public class ConfigClientController{
    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/config/info")
    public String getConfigInfo() {
        return configInfo;
    }
}

2.6 在 Nacos 服务器中添加配置信息

Nacos 新建配置成功.png 启动服务,调用接口.png

2.7 报错无法装配 bean

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.configClientController': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'config.info' in value "${config.info}"
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:380) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$1(AbstractBeanFactory.java:356) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.cloud.context.scope.GenericScope$BeanLifecycleWrapper.getBean(GenericScope.java:389) ~[spring-cloud-context-2.1.2.RELEASE.jar:2.1.2.RELEASE]
    at org.springframework.cloud.context.scope.GenericScope.get(GenericScope.java:186) ~[spring-cloud-context-2.1.2.RELEASE.jar:2.1.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:353) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1105) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.cloud.context.scope.refresh.RefreshScope.eagerlyInitialize(RefreshScope.java:134) ~[spring-cloud-context-2.1.2.RELEASE.jar:2.1.2.RELEASE]
    at org.springframework.cloud.context.scope.refresh.RefreshScope.start(RefreshScope.java:125) ~[spring-cloud-context-2.1.2.RELEASE.jar:2.1.2.RELEASE]
    at org.springframework.cloud.context.scope.refresh.RefreshScope.onApplicationEvent(RefreshScope.java:119) ~[spring-cloud-context-2.1.2.RELEASE.jar:2.1.2.RELEASE]
    at org.springframework.cloud.context.scope.refresh.RefreshScope.onApplicationEvent(RefreshScope.java:73) ~[spring-cloud-context-2.1.2.RELEASE.jar:2.1.2.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    ......

3. Nacos 加载配置的三种方案

3.1 Data ID 方案

三个 Data ID 的环境.png
spring:
  profiles:
    active: prod
配置切换成其他环境.png

3.2 Group 方案

三个 Group 的环境.png
spring:
  profiles:
    active: info #修改
server:
  port: 18082
spring:
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 
      config:
        server-addr: localhost:8848
        file-extension: yaml
        group: PROD_GROUP #新增
配置切换成其他 Group 环境.png

3.3 Namespace 方案

新建两个命名空间.png 三个命名空间.png
spring:
  profiles:
    active: dev #修改
server:
  port: 18082
spring:
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 
      config:
        server-addr: localhost:8848
        file-extension: yaml
        group: DEFAULT_GROUP #新增
        namespace: 48b2da7d-0b26-4c15-907b-9a379db8f7de #新增,命名空间的 ID,在新建命名空间时会给出
配置切换成其他命名空间环境.png

最后

\color{blue}{\rm\small{新人制作,如有错误,欢迎指出,感激不尽!}}

\color{blue}{\rm\small{欢迎关注我,并与我交流!}}

\color{blue}{\rm\small{如需转载,请标注出处!}}

上一篇下一篇

猜你喜欢

热点阅读