当前位置:   article > 正文

Spring Cloud Alibaba (一) 之Nacos_nacos client name 前缀加了bootstrapproperties

nacos client name 前缀加了bootstrapproperties

关于Nacos

微服务概念兴起之后,越来越多的项目使用微服务的架构理念,随着服务节点的逐渐增多,我们需要一个独立的服务或者说组件,对我们的所有微服务节点进行管理。最早还没有SpringCloud的时候,我们在用Dubbo的同时也需要搭建Zokeeper作为注册中心来管理所有的Dubbo的生产者和消费者,Zokeeper也就实现了其中的服务治理的功能,但是这也只是Zokeeper其中一小部分的功能,为了服务治理就是用Zookeeper是一件事倍功半的事,在SpringCloud横空出世后Netflix开源了自己的SpringCloud组件,其中就包括服务治理中心Eureka,而后Alibaba开源SpringCloud的一系列组件,就包括作为服务治理中心的Nacos,与Eureka最大的不同是Nacos不只是服务治理中心,还是配置中心。我们以下将Nacos分为两个部分Nacos Discovery(服务发现)和Nacos Config(配置中心)来详细描述。

版本适配

Spring Cloud Alibaba 和其组件以及和SpringBoot和SpringCloud的版本适配可以在 版本说明 查看

Spring Cloud Alibaba VersionSentinel VersionNacos VersionRocketMQ VersionDubbo VersionSeata Version
2.2.3.RELEASE or 2.1.3.RELEASE or 2.0.3.RELEASE1.8.01.3.34.4.02.7.81.3.0
2.2.1.RELEASE or 2.1.2.RELEASE or 2.0.2.RELEASE1.7.11.2.14.4.02.7.61.2.0
2.2.0.RELEASE1.7.11.1.44.4.02.7.4.11.0.0
2.1.1.RELEASE or 2.0.1.RELEASE or 1.5.1.RELEASE1.7.01.1.44.4.02.7.30.9.0
2.1.0.RELEASE or 2.0.0.RELEASE or 1.5.0.RELEASE1.6.31.1.14.4.02.7.30.7.1
Spring Cloud VersionSpring Cloud Alibaba VersionSpring Boot Version
Spring Cloud Hoxton.SR82.2.3.RELEASE2.3.2.RELEAS
Spring Cloud Greenwich.SR62.1.3.RELEASE2.1.13.RELEASE
Spring Cloud Hoxton.SR32.2.1.RELEASE2.2.5.RELEASE
Spring Cloud Hoxton.RELEASE2.2.0.RELEASE2.2.X.RELEASE
Spring Cloud Greenwich2.1.2.RELEASE2.1.X.RELEASE
Spring Cloud Finchley2.0.3.RELEASE2.0.X.RELEASE
Spring Cloud Edgware1.5.1.RELEASE(停止维护,建议升级)1.5.X.RELEASE

本次我们使用 SpringCloudAlibaba 2.2.1.RELEASE 和Nacos 1.2.1 SpringBoot 2.2.5.RELEASE版本。

Nacos Discovery

Nacos支持单机部署和集群部署
你可以在 nacos下载地址 下载Nacos的最新版本和历史版本

单机部署

我们下载下来的文件解压就可以得到 NacosServer的启动文件了,如果在单机不修改配置的情况下,我们只需要在 bin目录下 使用 startup.sh/startup.cmd 命令启动即可

启动命令

# -m 参数表示 启动模式 standalone为单机启动 nacos默认是集群启动 所以我们在单机启动的时候需要 加上这个参数
./startup.sh -m standalone
  • 1
  • 2

如图所示便启动好了

在这里插入图片描述

此时我们就可以通过默认端口访问我们的Nacos

http://127.0.0.1:8848/nacos

默认的 账号:nacos 密码:nacos

我们同时可以在配置中更改启动的端口和serlvetContext,打开conf目录下的application.properties文件 更改以下配置重启即可

#*************** Spring Boot Related Configurations ***************#
### Default web context path:
server.servlet.contextPath=/nacosstandalone
### Default web server port:
server.port=8850

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
服务注册

我们的服务注册到Nacos上需要Nacos-discovery 的包

项目依赖的pom文件

<properties>
        <java.version>1.8</java.version>
        <spring-boot.version>2.2.5.RELEASE</spring-boot.version>
        <spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
    </properties>

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

    <dependencies>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>
        
        <!-- nacos discovery client 的依赖-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        
        <!--引入nacos-discovery后 项目启动就会报找不到 HttpClientBuilder 和 jackson的错 加入这个就不会出错了,可能是缺少依赖造成的-->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.5</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.11.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.11.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.11.0</version>
        </dependency>
        
    </dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66

在SpringBoot的启动类上添加 @EnableDiscoveryClient注解

package com.ofm.nacos.config.client;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
/**
 *  @EnableDiscoveryClient和@EnableEurekaClient
 *  共同点就是:都是能够让注册中心能够发现,扫描到该服务。
 * 不同点:@EnableEurekaClient只适用于Eureka作为注册中心,@EnableDiscoveryClient 可以是其他注册中心。
 */
@EnableDiscoveryClient
public class NacosConfigClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosConfigClientApplication.class, args);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

使用bootstrap.yml替换 application.properties配置文件

server:
  port: 5566
spring:
  application:
    name: nacos-config-client
  profiles:
    active: location
  cloud:
    nacos:
      discovery: 
        server-addr: 192.168.209.131:8850 #nacosServer的地址
        namespace: location #服务归属的命名空间 可以根据自己需要的 在nacosserver中创建命名空间
        group: TEST #服务的分组名称 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

在Nacos注册中心创建 Id为location的命名空间 和配置文件的namespace一致
在这里插入图片描述
然后启动服务我们就可以在服务中心找到我们启动注册上去的服务了
在这里插入图片描述

集群部署

服务端集群的话只需要在各个节点上配置数据库和集群的节点信息就好了,数据库信息在conf/application.properties,集群信息在conf/cluster.conf,我们这次配置三个节点的集群 分别在 8847 8848 8849端口上,以下用端口号代表各节点的实例。

数据库配置

首先是在conf/nacos-mysql.sql 找到数据库初始化的脚本,然后在自己的数据库上新建一个库(库名随意)用这个脚本初始化表结构和数据。

8847配置

application.properties

#*************** Spring Boot Related Configurations ***************#
### Default web context path:
server.servlet.contextPath=/nacos
### Default web server port: 配置端口
server.port=8847

#*************** Network Related Configurations ***************#
### If prefer hostname over ip for Nacos server addresses in cluster.conf:
# nacos.inetutils.prefer-hostname-over-ip=false

### Specify local server's IP:
# nacos.inetutils.ip-address=


#*************** Config Module Related Configurations ***************#
### If use MySQL as datasource: 如果使用Mysql 配置数据库
spring.datasource.platform=mysql

### Count of DB: 指定据源
db.num=1

### Connect URL of DB: 配置数据源 这里的库就是刚刚初始化的库
db.url.0=
db.user.0=
db.password.0=

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

cluster.conf

#配置所有节点的IP和端口即可
192.168.209.131:8847
192.168.209.131:8848
192.168.209.131:8849
  • 1
  • 2
  • 3
  • 4

剩下8848 8849实例 只有application.properties的 server.port 需要变更为自己对应的端口然后就可以挨个启动实例了
启动命令

#不需要设置模式了,默认就是集群模式
./startup.sh
  • 1
  • 2

此时访问其中一个节点的Nacos服务,在节点列表里面就可以找到我们的集群信息了
在这里插入图片描述

服务注册

集群的服务注册和单机差不多 只需要在配置文件中将 spring.cloud.nacos.server-addr的地址由一个变成多个即可

server:
  port: 5566
spring:
  application:
    name: nacos-config-client
  profiles:
    active: location
  cloud:
    nacos:
      discovery: 
        server-addr: 192.168.209.131:8847,192.168.209.131:8848,192.168.209.131:8849
        namespace: location
        group: TEST
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

然后启动服务就会在Nacos集群中注册上了

Nacos Config

Nacos和Eureka不同,Nacos集成了Cofing模块不需要在依赖别的Config服务,我们只需要在Nacos上配置好配置文件,然后在client中指定配置即可

服务端配置

在配置列表里选择我们想要使用的命名空间(namespace)新建配置文件
这里我新建了启动的基础配置文件(
nacos-config-client-location.yaml)和一个参数配置文件(
public-parameter.yml)

nacos-config-client-location.yaml

在这里插入图片描述

public-parameter.yml

在这里插入图片描述

客户端配置
依赖引入
		<!-- 再加入 nacos-config 的依赖-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
配置变更
server:
  port: 5566
spring:
  application:
    name: nacos-config-client
  profiles:
    active: location
  cloud:
    nacos:
      discovery: #192.168.209.131:8847,192.168.209.131:8848,192.168.209.131:8849 172.16.4.53:8848,192.168.209.131:8850
        server-addr: 192.168.209.131:8847,192.168.209.131:8848,192.168.209.131:8849
        namespace: location
        group: TEST
      config: #config配置
        server-addr: 192.168.209.131:8847,192.168.209.131:8848,192.168.209.131:8849 #nacos-config地址
        group: TEST #config使用的group
        enabled: true #启动nacos-config
        namespace: location #命名空间
        file-extension: yaml #配置yaml格式支持
        extension-configs: #扩展的配置文件(此处可以添加多个扩展的配置 相当于每个文件都是片段 然后组合成一个)
          - data-id: public-parameter.yml #扩展配置文件的ID
            group: TEST #扩展配置文件的group
            refresh: true #控制该 Data Id 在配置变更时,是否支持应用中可动态刷新, 感知到最新的配置值。默认是不支持的
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
测试接口

写一个接口测试我们刚刚配置文件里写入的参数是否被读取到

package com.ofm.nacos.config.client.controller;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
@ConfigurationProperties(prefix = "parameter") //通过注解引入参数 这些参数需要有set方法
public class HelloController {

    private String userName;

    private String userLink;

    private String age;


    @GetMapping("/me")
    public String me(){
        return "hello this is "+ userName+","+age+ "years old,and there is my blog link "+userLink;
    }


    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserLink() {
        return userLink;
    }

    public void setUserLink(String userLink) {
        this.userLink = userLink;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

然后我们启动项目

"C:\Program Files\Java\jdk1.8.0_181\bin\java.exe" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:9804,suspend=y,server=n -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true -javaagent:C:\Users\OFM\.IntelliJIdea2019.3\system\captureAgent\debugger-agent.jar -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_181\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\rt.jar;E:\Code\WorkSpeace\Java\Me\SpringCloud-Alibaba\me\ofm-cloud\nacos-config-client\target\classes;E:\Program\apache-maven-3.6.3\repository\com\alibaba\cloud\spring-cloud-starter-alibaba-nacos-config\2.2.1.RELEASE\spring-cloud-starter-alibaba-nacos-config-2.2.1.RELEASE.jar;E:\Program\apache-maven-3.6.3\repository\com\alibaba\spring\spring-context-support\1.0.6\spring-context-support-1.0.6.jar;E:\Program\apache-maven-3.6.3\repository\com\alibaba\nacos\nacos-client\1.2.1\nacos-client-1.2.1.jar;E:\Program\apache-maven-3.6.3\repository\com\alibaba\nacos\nacos-common\1.2.1\nacos-common-1.2.1.jar;E:\Program\apache-maven-3.6.3\repository\org\slf4j\slf4j-api\1.7.7\slf4j-api-1.7.7.jar;E:\Program\apache-maven-3.6.3\repository\commons-io\commons-io\2.2\commons-io-2.2.jar;E:\Program\apache-maven-3.6.3\repository\org\apache\commons\commons-lang3\3.4\commons-lang3-3.4.jar;E:\Program\apache-maven-3.6.3\repository\com\alibaba\nacos\nacos-api\1.2.1\nacos-api-1.2.1.jar;E:\Program\apache-maven-3.6.3\repository\com\alibaba\fastjson\1.2.58\fastjson-1.2.58.jar;E:\Program\apache-maven-3.6.3\repository\com\google\guava\guava\22.0\guava-22.0.jar;E:\Program\apache-maven-3.6.3\repository\com\google\code\findbugs\jsr305\1.3.9\jsr305-1.3.9.jar;E:\Program\apache-maven-3.6.3\repository\com\google\errorprone\error_prone_annotations\2.0.18\error_prone_annotations-2.0.18.jar;E:\Program\apache-maven-3.6.3\repository\com\google\j2objc\j2objc-annotations\1.1\j2objc-annotations-1.1.jar;E:\Program\apache-maven-3.6.3\repository\org\codehaus\mojo\animal-sniffer-annotations\1.14\animal-sniffer-annotations-1.14.jar;E:\Program\apache-maven-3.6.3\repository\io\prometheus\simpleclient\0.5.0\simpleclient-0.5.0.jar;E:\Program\apache-maven-3.6.3\repository\org\springframework\cloud\spring-cloud-commons\2.2.2.RELEASE\spring-cloud-commons-2.2.2.RELEASE.jar;E:\Program\apache-maven-3.6.3\repository\org\springframework\security\spring-security-crypto\5.2.2.RELEASE\spring-security-crypto-5.2.2.RELEASE.jar;E:\Program\apache-maven-3.6.3\repository\org\springframework\cloud\spring-cloud-context\2.2.2.RELEASE\spring-cloud-context-2.2.2.RELEASE.jar;E:\Program\apache-maven-3.6.3\repository\org\springframework\boot\spring-boot-starter-web\2.2.5.RELEASE\spring-boot-starter-web-2.2.5.RELEASE.jar;E:\Program\apache-maven-3.6.3\repository\org\springframework\boot\spring-boot-starter-json\2.2.5.RELEASE\spring-boot-starter-json-2.2.5.RELEASE.jar;E:\Program\apache-maven-3.6.3\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.10.2\jackson-datatype-jdk8-2.10.2.jar;E:\Program\apache-maven-3.6.3\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.10.2\jackson-datatype-jsr310-2.10.2.jar;E:\Program\apache-maven-3.6.3\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.10.2\jackson-module-parameter-names-2.10.2.jar;E:\Program\apache-maven-3.6.3\repository\org\springframework\boot\spring-boot-starter-tomcat\2.2.5.RELEASE\spring-boot-starter-tomcat-2.2.5.RELEASE.jar;E:\Program\apache-maven-3.6.3\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.31\tomcat-embed-core-9.0.31.jar;E:\Program\apache-maven-3.6.3\repository\org\apache\tomcat\embed\tomcat-embed-el\9.0.31\tomcat-embed-el-9.0.31.jar;E:\Program\apache-maven-3.6.3\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.31\tomcat-embed-websocket-9.0.31.jar;E:\Program\apache-maven-3.6.3\repository\org\springframework\boot\spring-boot-starter-validation\2.2.5.RELEASE\spring-boot-starter-validation-2.2.5.RELEASE.jar;E:\Program\apache-maven-3.6.3\repository\jakarta\validation\jakarta.validation-api\2.0.2\jakarta.validation-api-2.0.2.jar;E:\Program\apache-maven-3.6.3\repository\org\hibernate\validator\hibernate-validator\6.0.18.Final\hibernate-validator-6.0.18.Final.jar;E:\Program\apache-maven-3.6.3\repository\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;E:\Program\apache-maven-3.6.3\repository\com\fasterxml\classmate\1.3.4\classmate-1.3.4.jar;E:\Program\apache-maven-3.6.3\repository\org\springframework\spring-web\5.2.4.RELEASE\spring-web-5.2.4.RELEASE.jar;E:\Program\apache-maven-3.6.3\repository\org\springframework\spring-beans\5.2.4.RELEASE\spring-beans-5.2.4.RELEASE.jar;E:\Program\apache-maven-3.6.3\repository\org\springframework\spring-webmvc\5.2.4.RELEASE\spring-webmvc-5.2.4.RELEASE.jar;E:\Program\apache-maven-3.6.3\repository\org\springframework\spring-aop\5.2.4.RELEASE\spring-aop-5.2.4.RELEASE.jar;E:\Program\apache-maven-3.6.3\repository\org\springframework\spring-context\5.2.4.RELEASE\spring-context-5.2.4.RELEASE.jar;E:\Program\apache-maven-3.6.3\repository\org\springframework\spring-expression\5.2.4.RELEASE\spring-expression-5.2.4.RELEASE.jar;E:\Program\apache-maven-3.6.3\repository\com\alibaba\cloud\spring-cloud-starter-alibaba-nacos-discovery\2.2.1.RELEASE\spring-cloud-starter-alibaba-nacos-discovery-2.2.1.RELEASE.jar;E:\Program\apache-maven-3.6.3\repository\org\springframework\cloud\spring-cloud-starter-netflix-ribbon\2.2.2.RELEASE\spring-cloud-starter-netflix-ribbon-2.2.2.RELEASE.jar;E:\Program\apache-maven-3.6.3\repository\org\springframework\cloud\spring-cloud-starter\2.2.2.RELEASE\spring-cloud-starter-2.2.2.RELEASE.jar;E:\Program\apache-maven-3.6.3\repository\org\springframework\security\spring-security-rsa\1.0.9.RELEASE\spring-security-rsa-1.0.9.RELEASE.jar;E:\Program\apache-maven-3.6.3\repository\org\bouncycastle\bcpkix-jdk15on\1.64\bcpkix-jdk15on-1.64.jar;E:\Program\apache-maven-3.6.3\repository\org\bouncycastle\bcprov-jdk15on\1.64\bcprov-jdk15on-1.64.jar;E:\Program\apache-maven-3.6.3\repository\org\springframework\cloud\spring-cloud-netflix-ribbon\2.2.2.RELEASE\spring-cloud-netflix-ribbon-2.2.2.RELEASE.jar;E:\Program\apache-maven-3.6.3\repository\org\springframework\cloud\spring-cloud-netflix-archaius\2.2.2.RELEASE\spring-cloud-netflix-archaius-2.2.2.RELEASE.jar;E:\Program\apache-maven-3.6.3\repository\org\springframework\cloud\spring-cloud-starter-netflix-archaius\2.2.2.RELEASE\spring-cloud-starter-netflix-archaius-2.2.2.RELEASE.jar;E:\Program\apache-maven-3.6.3\repository\com\netflix\archaius\archaius-core\0.7.6\archaius-core-0.7.6.jar;E:\Program\apache-maven-3.6.3\repository\commons-configuration\commons-configuration\1.8\commons-configuration-1.8.jar;E:\Program\apache-maven-3.6.3\repository\com\netflix\ribbon\ribbon\2.3.0\ribbon-2.3.0.jar;E:\Program\apache-maven-3.6.3\repository\com\netflix\ribbon\ribbon-transport\2.3.0\ribbon-transport-2.3.0.jar;E:\Program\apache-maven-3.6.3\repository\io\reactivex\rxnetty-contexts\0.4.9\rxnetty-contexts-0.4.9.jar;E:\Program\apache-maven-3.6.3\repository\io\reactivex\rxnetty-servo\0.4.9\rxnetty-servo-0.4.9.jar;E:\Program\apache-maven-3.6.3\repository\com\netflix\hystrix\hystrix-core\1.4.3\hystrix-core-1.4.3.jar;E:\Program\apache-maven-3.6.3\repository\javax\inject\javax.inject\1\javax.inject-1.jar;E:\Program\apache-maven-3.6.3\repository\io\reactivex\rxnetty\0.4.9\rxnetty-0.4.9.jar;E:\Program\apache-maven-3.6.3\repository\com\netflix\ribbon\ribbon-core\2.3.0\ribbon-core-2.3.0.jar;E:\Program\apache-maven-3.6.3\repository\commons-lang\commons-lang\2.6\commons-lang-2.6.jar;E:\Program\apache-maven-3.6.3\repository\com\netflix\ribbon\ribbon-httpclient\2.3.0\ribbon-httpclient-2.3.0.jar;E:\Program\apache-maven-3.6.3\repository\commons-collections\commons-collections\3.2.2\commons-collections-3.2.2.jar;E:\Program\apache-maven-3.6.3\repository\com\sun\jersey\jersey-client\1.19.1\jersey-client-1.19.1.jar;E:\Program\apache-maven-3.6.3\repository\com\sun\jersey\jersey-core\1.19.1\jersey-core-1.19.1.jar;E:\Program\apache-maven-3.6.3\repository\javax\ws\rs\jsr311-api\1.1.1\jsr311-api-1.1.1.jar;E:\Program\apache-maven-3.6.3\repository\com\sun\jersey\contribs\jersey-apache-client4\1.19.1\jersey-apache-client4-1.19.1.jar;E:\Program\apache-maven-3.6.3\repository\com\netflix\servo\servo-core\0.10.1\servo-core-0.10.1.jar;E:\Program\apache-maven-3.6.3\repository\com\netflix\servo\servo-internal\0.10.1\servo-internal-0.10.1.jar;E:\Program\apache-maven-3.6.3\repository\com\netflix\netflix-commons\netflix-commons-util\0.1.1\netflix-commons-util-0.1.1.jar;E:\Program\apache-maven-3.6.3\repository\com\netflix\ribbon\ribbon-loadbalancer\2.3.0\ribbon-loadbalancer-2.3.0.jar;E:\Program\apache-maven-3.6.3\repository\com\netflix\netflix-commons\netflix-statistics\0.1.1\netflix-statistics-0.1.1.jar;E:\Program\apache-maven-3.6.3\repository\io\reactivex\rxjava\1.3.8\rxjava-1.3.8.jar;E:\Program\apache-maven-3.6.3\repository\org\apache\httpcomponents\httpclient\4.5.6\httpclient-4.5.6.jar;E:\Program\apache-maven-3.6.3\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;E:\Program\apache-maven-3.6.3\repository\commons-codec\commons-codec\1.10\commons-codec-1.10.jar;E:\Program\apache-maven-3.6.3\repository\org\apache\httpcomponents\httpcore\4.4.5\httpcore-4.4.5.jar;E:\Program\apache-maven-3.6.3\repository\com\fasterxml\jackson\core\jackson-core\2.11.0\jackson-core-2.11.0.jar;E:\Program\apache-maven-3.6.3\repository\com\fasterxml\jackson\core\jackson-annotations\2.11.0\jackson-annotations-2.11.0.jar;E:\Program\apache-maven-3.6.3\repository\com\fasterxml\jackson\core\jackson-databind\2.11.0\jackson-databind-2.11.0.jar;E:\Program\apache-maven-3.6.3\repository\org\springframework\boot\spring-boot-starter\2.2.5.RELEASE\spring-boot-starter-2.2.5.RELEASE.jar;E:\Program\apache-maven-3.6.3\repository\org\springframework\boot\spring-boot\2.2.5.RELEASE\spring-boot-2.2.5.RELEASE.jar;E:\Program\apache-maven-3.6.3\repository\org\springframework\boot\spring-boot-autoconfigure\2.2.5.RELEASE\spring-boot-autoconfigure-2.2.5.RELEASE.jar;E:\Program\apache-maven-3.6.3\repository\org\springframework\boot\spring-boot-starter-logging\2.2.5.RELEASE\spring-boot-starter-logging-2.2.5.RELEASE.jar;E:\Program\apache-maven-3.6.3\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;E:\Program\apache-maven-3.6.3\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;E:\Program\apache-maven-3.6.3\repository\org\apache\logging\log4j\log4j-to-slf4j\2.12.1\log4j-to-slf4j-2.12.1.jar;E:\Program\apache-maven-3.6.3\repository\org\apache\logging\log4j\log4j-api\2.12.1\log4j-api-2.12.1.jar;E:\Program\apache-maven-3.6.3\repository\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;E:\Program\apache-maven-3.6.3\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;E:\Program\apache-maven-3.6.3\repository\org\springframework\spring-core\5.2.4.RELEASE\spring-core-5.2.4.RELEASE.jar;E:\Program\apache-maven-3.6.3\repository\org\springframework\spring-jcl\5.2.4.RELEASE\spring-jcl-5.2.4.RELEASE.jar;E:\Program\apache-maven-3.6.3\repository\org\yaml\snakeyaml\1.25\snakeyaml-1.25.jar;D:\Program\IntelliJ IDEA 2019.3.1\lib\idea_rt.jar" com.ofm.nacos.config.client.NacosConfigClientApplication
Connected to the target VM, address: '127.0.0.1:9804', transport: 'socket'

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.5.RELEASE)

2020-11-30 11:31:44.886  INFO 45588 --- [           main] c.a.n.c.c.impl.LocalConfigInfoProcessor  : LOCAL_SNAPSHOT_PATH:C:\Users\OFM\nacos\config
2020-11-30 11:31:44.928  INFO 45588 --- [           main] c.a.nacos.client.config.impl.Limiter     : limitTime:5.0
2020-11-30 11:31:44.993  INFO 45588 --- [           main] c.a.nacos.client.config.utils.JVMUtil    : isMultiInstance:false
2020-11-30 11:31:45.005  WARN 45588 --- [           main] c.a.c.n.c.NacosPropertySourceBuilder     : Ignore the empty nacos configuration and get it based on dataId[nacos-config-client] & group[TEST]
2020-11-30 11:31:45.008  WARN 45588 --- [           main] c.a.c.n.c.NacosPropertySourceBuilder     : Ignore the empty nacos configuration and get it based on dataId[nacos-config-client.yaml] & group[TEST]
2020-11-30 11:31:45.013  INFO 45588 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-nacos-config-client-location.yaml,TEST'}, BootstrapPropertySource {name='bootstrapProperties-nacos-config-client.yaml,TEST'}, BootstrapPropertySource {name='bootstrapProperties-nacos-config-client,TEST'}, BootstrapPropertySource {name='bootstrapProperties-public-parameter.yml,TEST'}]
2020-11-30 11:31:45.017  INFO 45588 --- [           main] c.o.n.c.c.NacosConfigClientApplication   : The following profiles are active: location
2020-11-30 11:31:45.591  INFO 45588 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=e11d40f3-2e2a-35c5-ad4c-2b667bee4108
2020-11-30 11:31:45.886  INFO 45588 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-11-30 11:31:45.896  INFO 45588 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-11-30 11:31:45.897  INFO 45588 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.31]
2020-11-30 11:31:46.032  INFO 45588 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-11-30 11:31:46.032  INFO 45588 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1001 ms
2020-11-30 11:31:46.085  WARN 45588 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2020-11-30 11:31:46.085  INFO 45588 --- [           main] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2020-11-30 11:31:46.089  WARN 45588 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2020-11-30 11:31:46.089  INFO 45588 --- [           main] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2020-11-30 11:31:46.210  INFO 45588 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-11-30 11:31:47.136  INFO 45588 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'Nacso-Watch-Task-Scheduler'
2020-11-30 11:31:47.141  INFO 45588 --- [           main] com.alibaba.nacos.client.naming          : initializer namespace from System Property :null
2020-11-30 11:31:47.141  INFO 45588 --- [           main] com.alibaba.nacos.client.naming          : initializer namespace from System Environment :null
2020-11-30 11:31:47.142  INFO 45588 --- [           main] com.alibaba.nacos.client.naming          : initializer namespace from System Property :null
2020-11-30 11:31:48.201  INFO 45588 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-11-30 11:31:48.204  INFO 45588 --- [           main] com.alibaba.nacos.client.naming          : [BEAT] adding beat: {"cluster":"DEFAULT","ip":"172.16.4.53","metadata":{"preserved.register.source":"SPRING_CLOUD"},"period":5000,"port":8080,"scheduled":false,"serviceName":"TEST@@nacos-config-client","stopped":false,"weight":1.0} to beat map.
2020-11-30 11:31:48.283  INFO 45588 --- [           main] com.alibaba.nacos.client.naming          : [REGISTER-SERVICE] location registering service TEST@@nacos-config-client with instance: {"clusterName":"DEFAULT","enabled":true,"ephemeral":true,"healthy":true,"instanceHeartBeatInterval":5000,"instanceHeartBeatTimeOut":15000,"ip":"172.16.4.53","ipDeleteTimeout":30000,"metadata":{"preserved.register.source":"SPRING_CLOUD"},"port":8080,"weight":1.0}
2020-11-30 11:31:48.295  INFO 45588 --- [           main] c.a.c.n.registry.NacosServiceRegistry    : nacos registry, TEST nacos-config-client 172.16.4.53:8080 register finished
2020-11-30 11:31:49.139  INFO 45588 --- [           main] c.o.n.c.c.NacosConfigClientApplication   : Started NacosConfigClientApplication in 7.711 seconds (JVM running for 9.675)
2020-11-30 11:31:49.142  INFO 45588 --- [           main] c.a.n.client.config.impl.ClientWorker    : [fixed-192.168.209.131_8847-192.168.209.131_8848-192.168.209.131_8849-location] [subscribe] nacos-config-client+TEST+location
2020-11-30 11:31:49.143  INFO 45588 --- [           main] c.a.nacos.client.config.impl.CacheData   : [fixed-192.168.209.131_8847-192.168.209.131_8848-192.168.209.131_8849-location] [add-listener] ok, tenant=location, dataId=nacos-config-client, group=TEST, cnt=1
2020-11-30 11:31:49.144  INFO 45588 --- [           main] c.a.n.client.config.impl.ClientWorker    : [fixed-192.168.209.131_8847-192.168.209.131_8848-192.168.209.131_8849-location] [subscribe] nacos-config-client-location.yaml+TEST+location
2020-11-30 11:31:49.144  INFO 45588 --- [           main] c.a.nacos.client.config.impl.CacheData   : [fixed-192.168.209.131_8847-192.168.209.131_8848-192.168.209.131_8849-location] [add-listener] ok, tenant=location, dataId=nacos-config-client-location.yaml, group=TEST, cnt=1
2020-11-30 11:31:49.146  INFO 45588 --- [           main] c.a.n.client.config.impl.ClientWorker    : [fixed-192.168.209.131_8847-192.168.209.131_8848-192.168.209.131_8849-location] [subscribe] public-parameter.yml+TEST+location
2020-11-30 11:31:49.146  INFO 45588 --- [           main] c.a.nacos.client.config.impl.CacheData   : [fixed-192.168.209.131_8847-192.168.209.131_8848-192.168.209.131_8849-location] [add-listener] ok, tenant=location, dataId=public-parameter.yml, group=TEST, cnt=1
2020-11-30 11:31:49.147  INFO 45588 --- [           main] c.a.n.client.config.impl.ClientWorker    : [fixed-192.168.209.131_8847-192.168.209.131_8848-192.168.209.131_8849-location] [subscribe] nacos-config-client.yaml+TEST+location
2020-11-30 11:31:49.147  INFO 45588 --- [           main] c.a.nacos.client.config.impl.CacheData   : [fixed-192.168.209.131_8847-192.168.209.131_8848-192.168.209.131_8849-location] [add-listener] ok, tenant=location, dataId=nacos-config-client.yaml, group=TEST, cnt=1
2020-11-30 11:31:49.148  INFO 45588 --- [1_8849-location] c.a.n.client.config.impl.ClientWorker    : get changedGroupKeys:[]
2020-11-30 11:31:49.151  INFO 45588 --- [1_8849-location] c.a.n.client.config.impl.ClientWorker    : get changedGroupKeys:[]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

日志中有一段
Located property source: [BootstrapPropertySource {name=‘bootstrapProperties-nacos-config-client-location.yaml,TEST’}, BootstrapPropertySource {name=‘bootstrapProperties-nacos-config-client.yaml,TEST’}, BootstrapPropertySource {name=‘bootstrapProperties-nacos-config-client,TEST’}, BootstrapPropertySource {name=‘bootstrapProperties-public-parameter.yml,TEST’}]
可以看到这里加载的配置文件
分别是
{name=‘bootstrapProperties-nacos-config-client-location.yaml,TEST’}
这个文件是 我们在配置文件中指定了spring.application.name=nacos-config-client和 spring.profiles.active=location,和我们本地配置文件一样的 -环境名(-dev -pro 的方式 不过他这里默认加上了yaml 所以组成了 nacos-config-client-location.yaml 的dataId 这里的TEST指的是我们config配置的group为test 在结合config的配置 也就是说 读取namespace=location group=TEST dataid=nacos-config-client-location.yaml的配置文件
{name=‘bootstrapProperties-nacos-config-client.yaml,TEST’}
和springboot原理相同 除了要读取我们active的配置 还要读取原本的配置,这里也是加上了yaml
{name=‘bootstrapProperties-nacos-config-client,TEST’}
这个是没加上yaml的
{name=‘bootstrapProperties-public-parameter.yml,TEST’}
这个是在配置文件里指定的参数配置文件

在日志里我们同样可以看到

Tomcat initialized with port(s): 8080 (http)
  • 1

此时说明我们虽然在bootstrap.yml配置了server.port=5566 但是他还是读取了 -nacos-config-client-location.yaml里的 server.port=8080

现在访问我们的接口测试一下其他的参数配置 http://127.0.0.1:8080/hello/me
在这里插入图片描述
可以看到我们的配置是没有问题的,都被加载到了,至此Nacos Config配置正常使用

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/138972?site
推荐阅读
相关标签
  

闽ICP备14008679号