一、nacos作为统一配置中心

1. 他管理配置文件方式是在自己所在服务器上形成一个版本库,因此不需要再创建远程版本库
2. nacos 作为统一配置中心管理配置文件时,同样也是存在版本控制

二、Nacos统一配置中心使用步骤

1. 创建独立配置中心的客户端

       此处将 第十八章 的 order-server 和 product-server 作为Nacos config 客户端。并都引入 Nacos 配置中的客户端依赖。

<!-- 引入Nacos Config  Client 配置中心客户端依赖 -->
<dependency>
  <groupId>com.alibaba.cloud</groupId>
  <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
        springcloud2020 版本 把Bootstrap被默认禁用,spring.config.import加入了对解密的支持。对于Config Client、Consul、Vault和Zookeepe

r的配置导入,如果你需要使用原来的bootstrap的配置引导功能,那么需要将org.springframework.cloud:spring-cloud-starter-bootstrap依赖引入到工程中。

nacos management 配置 nacos配置中心配置_spring


2. 将自身配置交给 Nacos config 管理

       此处将 第十八章 的 order-server 和 product-server 中的配置全部转移到Nacos config 中心。

nacos management 配置 nacos配置中心配置_spring cloud_02

nacos management 配置 nacos配置中心配置_开发语言_03

nacos management 配置 nacos配置中心配置_spring cloud_04

 

nacos management 配置 nacos配置中心配置_开发语言_05

3. 通知客户端拉取文件位置

       在客户端的resources中的 bootstrap 中告知 客户端需要到哪个位置拉取文件。

  • 告知 config server 地址,即Nacos server地址
  • 告知 服务的组。
  • 告知 配置文件的名字 

nacos management 配置 nacos配置中心配置_nacos management 配置_06

spring:  cloud:
    nacos:
      config:
        #告知客户端到哪台服务器去找 nacos server 注册中心总的地址
        server-addr: localhost:8848 # 配置nacos server 注册中心地址
        group: DEFAULT_GROUP # 指定拉取的组
        name: order-prod # 指定拉取的文件名字
        file-extension: yamlspring:  cloud:
    nacos:
      config:
        #客户端指定 要拉取文件的配置中心的地址
        server-addr: localhost:8848 # 配置nacos server 配置中心地址
        group: DEFAULT_GROUP # 指定拉取的组
        name: product-prod # 指定拉取的文件名字
        file-extension: yaml

三、动态刷新

@RefreshScope

  • OrderController 使用注解@RefreshScope
OrderController 
import com.hwadee.springcloud.entity.Product;import com.hwadee.springcloud.service.IOrderFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
@RefreshScope
public class OrderController {

    @Autowired
    IOrderFeignService orderFeignService;

    @RequestMapping("/buy/{id}")
    public Product buy(@PathVariable Long id) {
        Product product = orderFeignService.findOrderById(id);
        return product;
    }
}
  • ProductController 使用注解@RefreshScope
ProductController 
import com.hwadee.springcloud.entity.Product;import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;

@RestController
@RequestMapping("/product")
@RefreshScope
public class ProductController {
    //方便后面讲负载均衡,查看ip,此处获取配置中的端口号和ip
    @Value("${server.port}")
    private String port;
    @Value("${spring.cloud.client.ip-address}")
    private String ip;

    @RequestMapping("/buy/{id}")
    public Product findById(@PathVariable Long id) {
        Product product = new Product();
        product.setId(id);
        // 后面需要测试负载均衡,所以返回 ip 地址及端口号
        product.setName("当前访问服务地址:" + ip + ":" + port+"  "+"查询商品订单,订单号:"+id);
        product.setPrice(new BigDecimal(10000.0));
        System.out.println(product);
        return product;
    }
}
  • 重启服务修改Nacos上配置文件,进行测试

nacos management 配置 nacos配置中心配置_java_07

nacos management 配置 nacos配置中心配置_java_08

四、三个重要概念

  • 命名空间:默认命名空间是public,从项目的角度隔离多个项目的配置文件
  • 组:每个命名空间的组默认是DEFAULT_GROUP,从同一个项目的不同微服务角度隔离多个配置文件。
  • DataID:文件唯一标识(即文件名)

 1. 命名空间

     创建命名空间

nacos management 配置 nacos配置中心配置_nacos management 配置_09

nacos management 配置 nacos配置中心配置_开发语言_10

nacos management 配置 nacos配置中心配置_spring cloud_11

2. 创建组 和 DataID 

nacos management 配置 nacos配置中心配置_spring cloud_12

nacos management 配置 nacos配置中心配置_开发语言_13

nacos management 配置 nacos配置中心配置_spring_14

 3. 指定命名空间、组和DataID

     修改引导文件 bootstrap ,指定命名空间,切记,命名空间使用其id。

nacos management 配置 nacos配置中心配置_nacos management 配置_15

  • order-server引导文件bootstrap.yml修改
spring:  cloud:
    nacos:
      config:
        #客户端指定 要拉取文件的配置中心的地址
        server-addr: localhost:8848 # 配置nacos server 配置中心地址
        namespace: bbcccf09-be17-419e-864f-d2cce63bb258 #指定命名空间
        group: DEV # 指定拉取的组
  •  product-server引导文件bootstrap.yml修改
spring:  cloud:
    nacos:
      config:
        #客户端指定 要拉取文件的配置中心的地址
        server-addr: localhost:8848 # 配置nacos server 配置中心地址
        namespace: bbcccf09-be17-419e-864f-d2cce63bb258  #指定命名空间
        group: DEV # 指定拉取的组
        name: order-dev # 指定拉取的文件名字
        file-extension: yaml

 4、启动测试

nacos management 配置 nacos配置中心配置_java_08

五、持久化切换配置

nacos management 配置 nacos配置中心配置_nacos management 配置_17

       Nacos默认使用自带的嵌入式数据库derby实现数据的存储。将 order-prod.yaml 等配置文件信息报错在本地。如果重新启动Nacos文件会消失。且如果启动多个默认配置下的Nacos节点,数据存储是存在一致性问题的。为了解决这个问题,Nacos采用了集中式存储的方式来支持集群化部署,目前只支持MySQL的存储。

Nacos 配置持久化 MySQL 存储步骤:

1. 安装数据库 mysql 数据库

2. 执行 mysql脚本
 

第十八章:第十八章 Nacos注册中心详解-入门案例及服务通信

第二十章:alibaba sentinel详解-简介及下载安装