[toc]

一、客户端配置说明

1. application和bootstrap区别

  1. applicaiton.yml是用户级的资源配置项
  2. bootstrap. ym1是系统级的,优先级更加高

2. application和bootstrap加载原理

Spring Cloud会创建一个“Bootstrap Context”,作为Spring应用的ApplicationContext的父上下文。初始化的时候,‘BootstrapContext'负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment'。
'Bootstrap属性有高优先级,默认情况下,它们不会被本地配置覆盖。Bootstrap contexti和Application Context有着不同的约定,所以新增了一个'bootstrap.yml文件,保证Bootstrap Context'和`Application Context'配置的分离。

3. application和bootstrap加载顺序

因为bootstrap.yml是比application.yml先加载的。bootstrap.yml优先级高于application.yml

4. 客户端配置说明

image.png

二、搭建客户端

1. 新建cloud-config-client-3355

image.png

2. 修改pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud_learn</artifactId>
        <groupId>org.cloud.learn</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-config-client-3355</artifactId>


    <dependencies>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>com.atguigu.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

3. 创建yaml文件

server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      name: config
      profile: dev
      uri: http://localhost:3344
eureka:
  client:
    service-url:
      defaultZone: http://eureka9001.com:9001/eureka

4. 创建启动类

package org.learn.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConfigClientMain3355 {

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

5. 创建业务控制层

通过接口形式,通过配置服务端获取配置

package org.learn.cloud.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

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

三、测试

测试地址:http://localhost:3355/configInfo
测试类型:GET
测试结果:如下
image.png

Q.E.D.


只有创造,才是真正的享受,只有拚搏,才是充实的生活。