Config分布式配置中心


本文来源:尚硅谷,谷周阳教程学习笔记记录

Config分布式配置中心概述

微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的,动态的配置管理设施是必不可少的。
SpringCloud提供了Config Server来解决这个问题
在这里插入图片描述

Config分布式配置中心的构成和原理?

Spring Cloud Config项目是一个解决分布式系统的配置管理方案。它包含了ClientServer两个部分。
server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,
client通过接口获取数据、并依据此数据初始化自己的应用

目前SpringCloud Config的使用主要是通过Git/SVN方式做一个配置中心,然后每个服务从其中获取自身配置所需的参数。

在这里插入图片描述

Config Server需要做哪些工作?

依赖:
spring-cloud-config-server
启动类:
增加@EnableConfigServer注解
配置:

spring:
  cloud:
    config:
      server:
        git:
          uri: git@gitee.com:lvhouh/springcloud-config.git  #自己的git仓库地址
          search-paths:
            - springcloud-config  #搜索目录
      label: master    #所在分支

配置的读取规则?

/{lable}-{name}-{profiles}.yml

分支名-服务名-环境

Config Client需要做哪些工作?

客户端新建bootstrap.yml

  # config客户端配置
  cloud:
    config:
      label: master   #分支名
      name: config    #配置文件名
      profile: dev    #读取后缀名称  上述3个综合:master分支config-dev.yml被读取
      uri: http://localhost:3344    #配置中心地址

动态刷新?

业务类
注意添加@RefreshScope注解实现动态刷新

配置中心获取配置文件是实时刷新的即动态获取,而客户端需要通过配置才能达到效果

curl -X POST "http://localhost:3355/actuator/refresh"

Config分布式配置中心搭建

新建项目:cloud-config-cenyer-3344

pom

变更

  <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

完整

<dependencies>


        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</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>

application.yml

server:
  port: 3344
spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git:
          uri: git@gitee.com:lvhouh/springcloud-config.git  #自己的git仓库地址
          search-paths:
            - springcloud-config  #搜索目录
      label: master    #所在分支
eureka:
  client:
    service-url:
      defaultZone:  http://localhost:7001/eureka

启动类

增加@EnableConfigServer注解

package com.atguigu.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;


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

配置读取规则

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
这里配置了hosts文件
在这里插入图片描述
不配置的话访问
http://localhost:3344/master/config-dev.yml

配置的话访问
http://config-3344.com:3344/master/config-dev.yml
在这里插入图片描述

配置中心获取配置文件是实时刷新的即动态获取,而客户端需要通过配置才能达到效果

客户端配置搭建cloud-config-client-3355

pom

新增,注意这个没有server

   <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

引入actuator是为了监控,实现动态刷新

 <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>

客户端新建bootstrap.yml

server:
  port: 3355

spring:
  application:
    name: config-client
    # config客户端配置
  cloud:
    config:
      label: master   #分支名
      name: config    #配置文件名
      profile: dev    #读取后缀名称  上述3个综合:master分支config-dev.yml被读取
      uri: http://localhost:3344    #配置中心地址
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka


# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"

bootstrap.yml和application.yml可以同时存在,但这里我不写application了
在这里插入图片描述

启动类

package com.atguigu.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;


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

业务类

注意添加@RefreshScope注解实现动态刷新

package com.atguigu.springcloud.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RefreshScope
public class ConfigClientController
{
    @Value("${config.info}")
    private String configInfo;

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

发送POST请求,完成刷新

curl -X POST "http://localhost:3355/actuator/refresh"

在这里插入图片描述
3355可以动态刷新了
访问;http://localhost:3355/configInfo
在这里插入图片描述
缺点:每个服务的配置更改都需要发送POST请求,太麻烦,消息总线可以解决,请看下一篇:Springcloud Bus 消息总线

这家伙写的不错
https://blog.csdn.net/qazwsxpcm/article/details/88578076

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值