Nacos 做为配置中心喷血踩坑

Nacos 是啥

Nacos 能够用来作服务发现和配置中心git

这里暂时使用 Nacos 作配置中心。spring

Nacos 控制台页面

Nacos 地址:http://127.0.0.1:8848/nacos/bootstrap

帐户名:adminbash

密码:123456app

接入方法

  1. 本地开发环境配置 Nacos 域名解析,hosts 中添加spring-boot

    127.0.0.1 nacos
  2. 原来Maven项目的父项目集成了 Spring-Cloud-Config,启动时会本身去拉取配置,因此这个要改掉。编码

    把原来的父项目换成spa

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
    </parent>

    加入对应的 Spring Cloud 的版本管理配置code

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>0.2.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
    
        </dependencies>
    </dependencyManagement>

    增长 Nacos 的 jar 包server

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    </dependency>
  3. bootstrap.yml 中的配置以下

    spring:
      profiles:
        active: @phase@
      cloud:
        nacos:
          config:
            server-addr: nacos:8848
            file-extension: yaml
            # 他大爷的,命名空间的这个也要加上,文档上彻底没有啊,这个地方真的是血坑
            namespace: 1470b864-9acc-42c0-8e0b-40ad39bc0c5a
            # 组名也要加上啊
            group: DEFAULT_GROUP
      application:
        name: @project.name@

    解释:

    • nacos 会去找 ${prefix}-${spring.profiles.active}.${file-extension} 的配置文件
    • prefix 默认为 spring.application.name 的值,也能够经过配置项 spring.cloud.nacos.config.prefix来配置。
    • spring.profiles.active 即为当前环境对应的 profile, 注意:当 spring.profile.active 为空时,对应的链接符 - 也将不存在,dataId 的拼接格式变成 ${prefix}.${file-extension}
    • file-exetension 为配置内容的数据格式,能够经过配置项 spring.cloud.nacos.config.file-extension 来配置。目前只支持 propertiesyaml 类型。
    • 目前按照我这个配置就能够了
  4. 把本身的配置从 git 上复制过来,并在控制台中 JUGGLE 空间中建立属于本身的配置,注意,配置文件中不能出现中文,补充,其实能够出现中文,以前报错是由于 IDEA 的项目文件编码是 GBK,要改为 UTF-8 就能够了。

按照如上配置项目能够顺利启动,此外 Nacos 还能够作热更新,实时推送配置。其余特性本身探索。

相关文章
相关标签/搜索