【SpringCloud】【Hoxton】Config配置中心

01 基础环境准备
02 一文读懂Eureka
03 Zookeeper注册中心
04 Consule注册中心
05 Ribbon
06 OpenFegin
07 Hystrix全面解析
08 Gateway全面解析
09 Config配置中心
10 Bus消息总线

1 介绍

Spring Cloud Config 为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。

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

2 怎么用

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

3 创建springcloud-config文件夹

在这里插入图片描述
(1) 创建config-dev.yml

config:
  info: "Config-enviroment-release-v1.0 branch,springcloud-config/config-dev.yml version=1"

(2) 创建config-prod.yml、config-test.yml空文件

(3) 上传到github上
在这里插入图片描述

4 创建配置中心工程

在这里插入图片描述

(1) pom

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</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-starter-test</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>com.zrs.springcloud</groupId>
        <artifactId>commons</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
</dependencies>

(2) application.yml

server:
  port: 9003
spring:
  application:
    name: config-center
  cloud:
    config:
      server:
        git:
          #配置github
          uri: https://github.com/zhurongsheng666/spring-cloud-hoxton.git
          #配置搜索目录
          search-paths:
            - springcloud-config
      #配置分支
      label: Config-enviroment-release-v1.0
eureka:
  client:
    #是否将自己注册到EurekaServer
    register-with-eureka: true
    #是否从EurekaServer获取已有的注册服务
    fetch-registry: true
    #注册地址
    service-url:
      defaultZone: http://localhost:7000/eureka/
  instance:
    instance-id: config-center
    prefer-ip-address: true

(3) 主程序

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

(4) 启动后测试

#配置中心/分支/配置文件
http://localhost:9003/Config-enviroment-release-v1.0/config-dev.yml

在这里插入图片描述
(5) 配置文件

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

推荐使用:/{label}/{application}-{profile}.yml

#例:
http://localhost:9003/Config-enviroment-release-v1.0/config-dev.yml
http://localhost:9003/Config-enviroment-release-v1.0/config-test.yml
http://localhost:9003/Config-enviroment-release-v1.0/config-cofnig.yml

5 创建客户端

在这里插入图片描述

5.1 pom

<dependencies>
    <!-- 配置客户端 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</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-starter-test</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>com.zrs.springcloud</groupId>
        <artifactId>commons</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
</dependencies>

5.2 bootstrap.yml

(1) 介绍
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.ym文件改为 bootstrap.yml,这是很关键的。因为 bootstrap.yml是比 application.yml先加载的。 bootstrap.yml优先级高于application. yml。

(2) 创建bootstrap.yml

server:
  port: 9004
spring:
  application:
    name: config-client
  cloud:
    # 配置拼接:http://localhost:9003/Config-enviroment-release-v1.0/config-dev
    config:
      #分支
      label: Config-enviroment-release-v1.0
      #配置文件名称
      name: config
      #读取后缀名称
      profile: dev
      #配置中心地址
      uri: http://localhost:9003
eureka:
  client:
    #是否将自己注册到EurekaServer
    register-with-eureka: true
    #是否从EurekaServer获取已有的注册服务
    fetch-registry: true
    #注册地址
    service-url:
      defaultZone: http://localhost:7000/eureka/
  instance:
    instance-id: config-client
    prefer-ip-address: true

5.3 ConfigClientRontroller

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

5.4 主启动类

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

5.5 启动测试

在这里插入图片描述

6 自动刷新

6.1 bootstrap.yml添加配置文件

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

6.2 在Controller上添加注解@RefreshScope并重启

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

6.3 在github上修改version

在这里插入图片描述
在这里插入图片描述

6.4 查看配置中心

在这里插入图片描述

6.5 用post请求刷新客户端

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

在这里插入图片描述

6.6 请求客户端

在这里插入图片描述

发布了156 篇原创文章 · 获赞 136 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/qq_34125999/article/details/104887587