SpringCloud-5.服务配置(SpringCloud Config)

目录

一、概述

1.1 问题引入

1.2 SpringCloud Config是什么

二、服务端配置

2.1 Git/GitHub配置

2.2 服务器端模块

三、客户端配置

四、动态刷新


一、概述

1.1 问题引入

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

1.2 SpringCloud Config是什么

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

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

        作用

        1.集中管理配置文件

        2.不同环境不同配置,动态化的配置更新分环境部署比如 dev / test / beta / release

        3.运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息

        4.当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置

        5.将配置信息以REST接口的形式暴露

二、服务端配置

        读取配置的规则其实有或几种,为了规范推荐使用如下格式:

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

        例如:http://config-3344.com:3344/master/config-prod.yml

2.1 Git/GitHub配置

        1.在github上建一个仓库,名字:springcloud-config

        2.idea中配置Git,写几个配置文件,然后把github上的这个仓库克隆本地,新建几个配置文件yml,commit -> push,更新到github上。记得更改主分支名字为master

2.2 服务器端模块

        1.新建模块:cloud-config-center-3344

        2.POM

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

        3.YML

server:
  port: 3344

spring:
  application:
    name:  cloud-config-center #注册进Eureka服务器的微服务名
  cloud:
    config:
      server:
        git:
         # uri: git@github.com:master-wz/springcloud-config.git #GitHub上面的git仓库名字
          uri: https://github.com/master-wz/springcloud-config.git #https方式
          ####搜索目录
          search-paths:
            - springcloud-config
          username: xxx    #github账户
          password: xxx    #github密码
          skip-ssl-validation: true
      ####读取分支
      label: master

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

        4.主启动,@EnableConfigServer

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

        5.(可选,使用火绒很方便)修改host文件,加入:

127.0.0.1  config-3344.com

         6.测试

                输入:

http://config-3344.com:3344/master/config-dev.yml

                输出:

config:
  name: config-dev

三、客户端配置

        1.建模块:cloud-config-client-3355

        2.POM

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

        3.bootstrap.yml

        applicaiton.yml用户级的资源配置项
        bootstrap.yml系统级的,优先级更高
 
        SpringCloud会创建一个“Bootstrap Context”作为Spring应用的`Application Context`的父上下文。初始化的时候,`Bootstrap Context`负责从外部源加载配置属性解析配置。这两个上下文共享一个从外部获取的`Environment`。

server:
  port: 3355

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:
    service-url:
      defaultZone: http://localhost:7001/eureka

         4.主启动

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

         5.业务类

@RestController
public class ConfigClientController
{
    @Value("${config.name}")
    private String configInfo;//配置文件中的config.name属性

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

         6.测试

                输入:

http://localhost:3355/configInfo

                输出:

config-dev

                成功获取到了远程配置文件中的config.name属性 

四、动态刷新

        假设运维在github修改了配置文件。

        刷新3344,发现ConfigServer配置中心立刻响应;刷新3355,发现ConfigClient客户端没有任何响应。我们总不能改一次重启一次服务器吧!

        为了避免这样的情况,我们需要修改3355的配置,实现动态刷新。

        1.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注解

        4.测试

        修改github上的配置信息,然后在cmd中输入下面指令:

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

        这时发现不管config-server还是config-client,配置信息都更新了。

        但现在有了新的问题:我们实现了动态刷新3355的配置,但如果有多台config client呢?我们总不能一个个的去执行上面那个命令吧!我们希望能用广播的方式,只输入一次指令,就让所有的config client动态刷新。那就要用到下一张讲的 SpringCloud Bus 了。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值