Springcloud_H版-alibaba学习笔记(十五)服务配置-Config(分布式配置中心)

1.概述

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

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

2.SpringCloud Config是什么

在这里插入图片描述

SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。
SpringCloud Config分为服务端和客户端两部分。
服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口。
客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。

3.分布式配置中心能做什么?

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

4.与GitHub或Gitee整合配置

由于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服务端配置与测试

1.在gitee上创建仓库及配置文件。

注意:仓库要是公开的。
springcloud-config: springcloud config 分布式配置中心测试用 (gitee.com)

2.创建工程

cloud-config-center-3344

3.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>dt-SpringCloud-2020</artifactId>  
        <groupId>com.dt.springcloud</groupId>  
        <version>1.0.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>  
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
    </properties>  
    <dependencies>  
        <!-- spring config -->  
        <dependency>  
            <groupId>org.springframework.cloud</groupId>  
            <artifactId>spring-cloud-config-server</artifactId>  
        </dependency>  
        <!-- eureka -->  
        <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>

4.yml

server:  
  port: 3344  
  
spring:  
  application:  
    name:  cloud-config-center #注册进Eureka服务器的微服务名  
  cloud:  
    config:  
      server:  
        git:  
          skipSslValidation: true # 跳过ssl认证  
          uri: https://gitee.com/DavidTio/springcloud-config.git #Gitee上面的git仓库名字  
          ####搜索目录  
          search-paths:  
            - springcloud-config  
      ####读取分支  
      label: master  
#服务注册到eureka地址  
#服务注册Eureka配置  
eureka:  
  client:  
    register-with-eureka: true #表示向注册中心注册自己 默认为true  
    fetch-registry: true #是否从EurekaServer抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡  
    service-url:  
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 入驻集群版Eureka地址  
      # defaultZone: http://localhost:7001/eureka/ # 入驻单机版Eureka地址  
  instance:  
    instance-id: cloud-config-center # 服务名称  
    prefer-ip-address: true # 访问路径可以显示ip地址

5.主启动

package com.dt.springcloud;  
  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;  
import org.springframework.cloud.config.server.EnableConfigServer;  
  
/**  
 * 主启动类  
 * EnableConfigServer  开启配置服务  
 */  
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})  
@EnableConfigServer  
public class ConfigCenterMain3344 {  
    public static void main(String[] args) {  
        SpringApplication.run(ConfigCenterMain3344.class, args);  
    }}

6.测试

启动eureka7001
启动eureka7002
启动cloud-config-center-3344
访问:localhost:3344/master/config-dev.yml
可以正确展示gitee仓库中配置文件的内容。

7.读取配置规则

Spring Cloud Config 有它的一套访问规则,我们通过这套规则在浏览器上直接访问就可以。

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

{application} 就是应用名称,对应到配置文件上来,就是配置文件的名称部分,例如我上面创建的配置文件。

{profile} 就是配置文件的版本,我们的项目有开发版本、测试环境版本、生产环境版本,对应到配置文件上来就是以 application-{profile}.yml 加以区分,例如application-dev.yml、application-sit.yml、application-prod.yml。

{label} 表示 git 分支,默认是 master 分支,如果项目是以分支做区分也是可以的,那就可以通过不同的 label 来控制访问不同的配置文件了。

3.Config客户端配置与测试

1.创建工程

cloud-config-client-3355

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>dt-SpringCloud-2020</artifactId>  
        <groupId>com.dt.springcloud</groupId>  
        <version>1.0.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>  
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
    </properties>  
    <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>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.bootstrap.yml

server:  
  port: 43355  
  
spring:  
  application:  
    name: config-client  
  cloud:  
    #Config客户端配置  
    config:  
      label: master #分支名称  
      name: config #配置文件名称  
      profile: dev #读取后缀名称   上述3个综合:master分支上config-dev.yml的配置文件被读取http://config-3344.com:3344/master/config-dev.yml  
      uri: http://localhost:3344 #配置中心地址k  
  
  
#服务注册Eureka配置  
eureka:  
  client:  
    register-with-eureka: true #表示向注册中心注册自己 默认为true  
    fetch-registry: true #是否从EurekaServer抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡  
    service-url:  
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 入驻集群版Eureka地址  
      # defaultZone: http://localhost:7001/eureka/ # 入驻单机版Eureka地址  
  instance:  
    instance-id: config-client # 服务名称  
    prefer-ip-address: true # 访问路径可以显示ip地址

4.主启动

package com.dt.springcloud;  
  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;  
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;  
  
/**  
 * 主启动类  
 * EnableConfigServer  开启配置服务  
 */  
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})  
@EnableEurekaClient  
public class ConfigClientMain3355 {  
    public static void main(String[] args) {  
        SpringApplication.run(ConfigClientMain3355.class, args);  
    }}

5.业务类

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

6.启动测试

启动eureka7001
启动eureka7002
启动cloud-config-center-3344
启动cloud-config-client-3355
访问:localhost:43355/configInfo
可以看到 config-client 通过config-center 可以访问到在gitee仓库上的配置文件信息。

7.bootstrap.yml 说明

applicaiton.yml是用户级的资源配置项
bootstrap.yml是系统级的,优先级更加高
Spring Cloud会创建一个“Bootstrap Context”,作为Spring应用的`Application Context`的父上下文。初始化的时候,`Bootstrap Context`负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的`Environment`。
`Bootstrap`属性有高优先级,默认情况下,它们不会被本地配置覆盖。 `Bootstrap context`和`Application Context`有着不同的约定,所以新增了一个`bootstrap.yml`文件,保证`Bootstrap Context`和`Application Context`配置的分离。
要将Client模块下的application.yml文件改为bootstrap.yml,这是很关键的,因为bootstrap.yml是比application.yml先加载的。bootstrap.yml优先级高于application.yml。

4.Config客户端之动态刷新

1.修改3355模块,pom引入actuator监控

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

2.修改yml,暴露监控端口

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

3.业务类Controller添加注解@RefreshScope

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

4.启动测试

启动eureka7001
启动eureka7002
启动cloud-config-center-3344
启动cloud-config-client-3355
访问:localhost:43355/configInfo
可以看到 config-client 通过config-center 可以访问到在gitee仓库上的配置文件信息。

这里发现3355的version还是没有变,这里需要需要运维人员发送Post请求刷新3355,我们手动发送post请求

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

再次刷新3355:http://localhost:43355/configInfo

5.还需要改进的地方有哪些?

假如有多个微服务客户端3355/3366/3377等,难道我们每个微服务都要执行一次post请求,手动刷新吗?能否实现消息广播,一次通知,处处生效. 见SpringCloud Bus消息总线。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值