Spring Cloud Config分布式配置中心

Spring Cloud Config分布式配置中心

1. 概述

分布式系统面临的问题----配置问题

  1. 微服务意味着要将单体应用中的业务拆分为一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务.由于每个服务都需要必要的配置信息才能运行,所以一套集中式的,动态的配置管理设施是必不可少的.
  2. SpringCloud提供了ConfigServer来解决这个问题,我们每一个微服务自己带着一个application.yml,上百个配置文件的管理就是问题.
1.1 是什么
  1. SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同为服务应用的所有环境提供了一个中心化的外部配置.

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DARMGYzC-1610700210792)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20201217101134730.png)]

  2. SpringCloud Config分为服务端和客户端两部分

  3. 服务算也被称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口.

  4. 客户端通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用git来存储配置信息,这样有助于环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容.

1.2 能干嘛
  1. 集中管理配置文件
  2. 不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release
  3. 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
  4. 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
  5. 将配置信息以REST接口的形式暴露

2. Config服务端配置与测试

2.1 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>cloud2020</artifactId>
        <groupId>com.atguigu.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-config-center3344</artifactId>

    <dependencies>
        <!--eureka client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.atguigu.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
        </dependency>
    </dependencies>


</project>

2.2 yml文件
server:
  port: 3344
spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git: git@github.com:Professor00/spring-cloud-config.git   # github上面git仓库的名字
        # 搜索目录
        search-paths:
          - spring-cloud-config
      label: master
eureka:
  client:
    server-url:
      defaultZone: http://eureka7001.com:7001/eureka #单机版
2.3 主启动类
package com.atguigu.springcloud;

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

@SpringBootApplication
@EnableConfigServer
public class ConfigMain3344 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigMain3344.class,args);
    }
}

2.5 测试
# http://localhost:3344/config-dev.yml

# /{label}-{name}-{profiles}.yml
# label: 分支(branch)
# name: 服务名
# profiles: 环境(dev/test/prod)

3. Config客户端配置与测试

3.1 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>cloud2020</artifactId>
        <groupId>com.atguigu.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-config-client3355</artifactId>
    <dependencies>
        <!--eureka client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.atguigu.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
        </dependency>
    </dependencies>

</project>

3.2 bootstrap.yml文件
  1. application.yml是用户级的资源配置项
  2. bootstrap.yml是系统级的,优先级更高
  3. SpringCloud会创建yige"Bootstrap Context",作为Spring应用的"Application Context"的父上下文.初始化的时候,BootStrap Context负责从外部源加载配置属性并解析配置.这两个上下文共享一个外部获取的Environment.
  4. BootStrap属性有高优先级,默认情况下,它们不会被本地配置覆盖.BootStrap ContextApplication Context有着不同的约定,所以新增了一个bootstrap.yml文件,保证BootStrap ContextApplication Context配置的分离.
  5. 要将Client模块下的application.yml文件改为bootstrap.yml,这是很关键的,因为bootstrap.yml是比application.yml先加载的.bootstrap.yml优先级高于application.yml
server:
  port: 3344
spring:
  application:
    name: config-client
  cloud:
    # config客户端配置
    config:
      label: master #分支名称
      name: config #配置文件名称
      profile: dev #读取后缀名称  上述3个综合就是http://localhost:3344/master/config-dev.yml
      uri: http://localhost:3344 #配置中心地址
eureka:
  client:
    server-url:
      defaultZone: http://eureka7001.com:7001/eureka #单机版
#暴露监控点
management:
  endpoints:
    web:
      exposure:
        include: "*"


3.3 客户端配置文件手动刷新
controller类上添加@RefreshScope

curl -X POST http://localhost:3355/actuator/refresh
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值