top
本文目录
1.概述
1.1 分布式系统面临的---配置问题
1.2config是什么
1.3 怎么用
1.4 能干嘛
2.Config服务端配置与测试
2.1在GitHub上新建一个名为springcloud-config的新Repository
2.2 由上一步获得刚新建的git地址
2.3 本地硬盘目录上新建git仓库并clone
 2.4建moudle
2.5 pom
2.6 yaml
2.7 主启动类
2.8 测试
2.9 配置的读取规则
3.Config客户端配置与测试
3.1建module
3.2pom.xml
3.3 bootstrap.yml
3.4 主启动类
引入web跟actuator这两个依赖3.5 业务类
3.6 测试
3.7 问题随之而来——分布式配置的动态刷新问题
4.Config客户端之动态刷新
4.1 pom引入actuator监控
4.2修改yml 暴露监控端口
4.3 在业务类上加一个@RefreshScope注解
4.4 测试:3355还是没有动态刷新
4.5 需要运维人员发送Post请求刷新3355
4.6 多个微服务客户端,难道每个微服务都要手动刷新?
2021.7.31

SpringCloud:分布式配置中心-Config

服务配置——SpringCloud Config分布式配置中心

1.概述

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

1.模块越来越多,每个模块都要写一个application.yml,后期修改很麻烦

2.上线后,发布版本了。有生产环境,有测试环境,预发布版本环境。一个配置文件不能同时满足三种环境。

3.集中式的管理这些配置。每个微服务自己带着一个application.yml,上百个配置文件的管理... SpringCloud提供了ConfigServer来解决这个问题。

1.2config是什么

SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持(Git/GitHub)(意思就是可以配置远程的配置文件?),配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置(Config Server)。
统一共用的放在配置中心,各自特有的再单独配置。
官网: https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.1.RELEASE/reference/html/
由于SpringCloud Config默认使用Git来存储配置文件,最推荐与github整合。

1.3 怎么用

SpringCloud Config 分为服务端客户端两部分。

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

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

1.4 能干嘛

  • 集中管理配置文件
  • 不同环境不同配置,动态化的配置更新,分环境部署比如 dev/test/prod/beta/release
  • 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务回想配置中心统一拉取配置自己的信息。
  • 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置。
  • 将配置信息以Rest接口的形式暴露。
  • 与GitHub整合配置,由于SpringCloud Config默认使用Git来存储配置文件,虽然也支持SVN。但是最推荐的还是 Git,而且使用的是http/https访问的形式。

https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.1.RELEASE/reference/html/

2.Config服务端配置与测试

2.1在GitHub上新建一个名为springcloud-config的新Repository

2.2 由上一步获得刚新建的git地址

SSH:git@github.com:TinStuCom/springcloud-config.git
Https:https://github.com/TinStuCom/springcloud-config.git (推荐)

2.3 本地硬盘目录上新建git仓库并clone

我创建的仓库在:E:\SpringCloud 在此右键鼠标,点击Git Bash Here
注意:现在git clone命令进行clone时需要验证个人访问令牌,个人访问令牌的设置

表示多个环境的配置;保存格式必须是utf-8
如果需要对本地仓的文件进行了修改,同步到远程仓库,此处模拟运维人员操作git和github

git add 
git commit -m "init yml" 
git push origin master

 2.4建moudle

新建Module模块 cloud-config-center-3344,它就是Cloud的配置中心模块 cloudConfig Center

2.5 pom

pom.xml

<?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>cloud6</artifactId>
        <groupId>com.tinstu.cloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-config-center-3344</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <!--springCloud Config Server-->
        <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>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>

2.6 yaml

application.yaml

server:
  port: 3344

spring:
  application:
    name:  cloud-config-center #注册进Eureka服务器的微服务名
  cloud:
    config:
      server:
        git:
          uri: https://github.com/TinStuCom/springcloud-config.git  
          #GitHub上面的git仓库名字 这里可以写https地址跟ssh地址,https地址需要配置username和password,ssh地址需要为公开仓库
          default-label: main
          ####搜索目录
          search-paths:
            - springcloud-config
          username: TinStuCom
          password: ******
      ####读取分支
      label: main   # 以前是master

#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka #单机版
    #      defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka #集群版

2.7 主启动类

ConfigCenterMain3344

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

这里注意一下:我没有加@EnableEurekaClient注解,这个服务依然注册进eureka注册中心了

原因:通过pom.xml 

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

依赖,可以省略@EnableEurekaClient,而根据yml配置文件想eureka里面注册。

2.8 测试

2.9 配置的读取规则

官网上有五种:

常用的三种,都差不多

2.9.1/{label}/{application}-{profile}.yml

读取main(master)分支

  • http://localhost:3344/master/config-dev.yaml
  • http://localhost:3344/master/config-test.yaml
  • http://localhost:3344/master/config-prod.yaml

读取dev分支

  • http://localhost:3344/dev/config-dev.yaml
  • http://localhost:3344/dev/config-dev.yaml
  • http://localhost:3344/dev/config-dev.yaml

2.9.2 /{application}-{profile}.yml

默认去master分支找,所以这里不写分支

这里需要在yml文件中设置spring.cloud.config.server.git.default-label=main(github更新后master分支都成了main分支)

2.9.3 /{application}/{profile}[/{label}]

  • http://localhost:3344/config/dev/master
  • http://localhost:3344/config/test/master
  • http://localhost:3344/config/prod/master

得到的结果是Json字符串

3.Config客户端配置与测试

ConfigServer从github上获取配置信息,现在需要ConfigClient 从 ConfigServer 上获取配置信息

3.1建module

新建cloud-config-client-3355

3.2pom.xml

新增:

        <!--这里就是客户端的SpringCloud config 因为是客户端所以没有server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
pom.xml

<?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>cloud6</artifactId>
        <groupId>com.tinstu.cloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

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

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <!--这里就是客户端的SpringCloud config 因为是客户端所以没有server-->
        <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>

        <!--web/actuator这两个一般一起使用,写在一起-->
        <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.3 bootstrap.yml

为了配置文件的加载顺序和分级管理,我们这里使用boostrap.yml

  • application.yml是用户级的资源配置项
  • boostrap.yml是系统级的,优先级更高
boostrap.yml

server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    #Config客户端配置
    config:
      label: main #分支名称 以前是master
      name: config #配置文件名称
      profile: dev #读取后缀名称   上述3个综合:main分支上config-dev.yml的配置文件被读取http://config-3344.com:3344/main/config-dev.yml
      uri: http://localhost:3344 #配置中心地址k

#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

 

3.4 主启动类

ConfigClientMain3355

package com.atguigu.springcloud;

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

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

 

引入web跟actuator这两个依赖3.5 业务类

SpringCloud Config能够将配置信息以REST窗口的形式暴露

ConfigClientController

package com.atguigu.springcloud.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;
    }
}

 

3.6 测试

启动失败注意github上面的写法是否正确

如:

config:
  info: "master branch,springcloud-config/config-dev.yml version=7"

 

3.7 问题随之而来——分布式配置的动态刷新问题

经测试:修改github上面的配置文件后,刷新ConfigServer立马发生变化,但是ConfigClient不会里面发生变化,需要重启

如何解决?

4.Config客户端之动态刷新

避免每次更新配置都要重启客户端微服务3355,需要修改3355模块

4.1 pom引入actuator监控

引入web跟actuator这两个依赖

<!--web/actuator这两个一般一起使用,写在一起-->
<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>

4.2修改yml 暴露监控端口

management:
  endpoints:
    web:
      exposure:
        include: "*"

2.0.0版本的SpringBoot的actuator启动端点监控web端默认加载默认只有两个info, health可见的页面节点。 如果需要显示更多需要在application.properties配置文件中添加 management.endpoints.web.exposure.include=* management.endpoints.web.exposure.exclude=env,beans
include=* 表示包含所有节点页面 ,exclude=env,beans 表示排除env、beans

4.3 在业务类上加一个@RefreshScope注解

4.4 测试:3355还是没有动态刷新

4.5 需要运维人员发送Post请求刷新3355

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

请求后刷新3355,可以正常响应

4.6 多个微服务客户端,难道每个微服务都要手动刷新?

  • 在微服务多的情况下,每个微服务都需要执行一个post请求,手动刷新?
  • 可否广播,一次通知,处处生效? 大范围的实现自动刷新
  • 可能有这种情况:100台机器,有得不要求实时刷新。我们想实现该刷新的刷新,不想刷新的不刷新,怎么实现?

解决方法——Bus总线。

https://www.tinstu.com/2308.html

阅读剩余
THE END
icon
0
icon
打赏
icon
分享
icon
二维码
icon
海报
发表评论
评论列表

赶快来坐沙发