SpringCloud-Config配置中心


前言

今天大致学了下Config配置中心,不过因为Git相关的知识压根没学,在这上面耗费了一定的时间


一、Config配置中心

1、配置中心

微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。
SpringCloud提供了ConfigServer来解决这个问题,我们每一个微服务自己带着一个application.yml,上百个配置文件的管理.……
SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。
各个微服务的一些重复的配置信息或可能会更改的配置信息可以通过配置中心来获取,可以简化配置编写,大概。
微服务的配置信息从配置中心获取,而配置中心的配置信息可以从Git/Github等地方获取。
如果Git中的配置信息进行了更改,配置中心的信息也会更改,但是微服务的配置并不会立即更改(重启服务/发送post请求刷新)

2、Config使用

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

3、Config作用

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

4、与GitHub整合配置

由于SpringCloud Config默认使用Git来存储配置文件(也有其它方式,比如支持SVN和本地文件),但最推荐的还是Git,而且使用的是http/https访问的形式。

二、服务端使用步骤

需要在GitHub创建Repository,Repository需要是public型,否则需要密钥(大概,没学过)
git地址 - git@github.com:**/**.git -有可能会链接不上,使用https形式更容易链接

1、引入库

		<!--config服务端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

2、application.yml

server:
  port: 3344
eureka:
  instance:
    instance-id: cloud-config-center
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/
spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git:
          #github上的git仓库的名字
          uri: https://github.com/sakanal/springcloud-config.git
          #搜索目录
          search-paths:
            - springcloud-config
      #读取分支
      label: main

3、主启动类

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

4、配置读取规则

官方文档

  • /{label}/{application}-{profile}.yml(推荐)
    - http://config-3344.com:3344/main/config-dev.yml
  • /{application}-{profile}.yml
    - http://config-3344.com:3344/config-dev.yml
  • /{application}/{profile}[/{label}
    - http://config-3344.com:3344/config/dev/master

重要配置细节总结

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

启动服务器端后可以通过-http://config-3344.com:3344/main/config-dev.yml-获取到GitHub中的配置信息
因为在yml中设置了分支-label: main-所有默认会从main分支中获取
http://config-3344.com:3344/main/config-dev.yml==http://config-3344.com:3344/config-dev.yml
也可以通过输入不同的分支来获取信息:http://config-3344.com:3344/master/config-test.yml


三、客户端使用步骤

1、引入库

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

2、bootstrap.yml

applicaiton.yml是用户级的资源配置项

bootstrap.yml是系统级的,优先级更加高

Spring Cloud会创建一个Bootstrap Context,作为Spring应用的Application Context的父上下文。

初始化的时候,BootstrapContext负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的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

server:
  port: 3355
spring:
  application:
    name: cloud-config-client
  cloud:
    #Config客户端配置
    config:
      label: main #分支名称
      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、主启动类

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

4、业务类

@RestController
public class ConfigClientController {
	//application.yml中没有这个配置
	//bootstrap.yml会从配置中心获取配置
    @Value("${config.info}")
    private String configInfo;
    @GetMapping("/configInfo")
    public String getConfigInfo(){
        return configInfo;
    }
}

启动服务端和客户端后,可以输入-http://localhost:3355/configlnfo-获取信息
GitHub–>服务端–>客户端


四、Config手动动态刷新

如果GitHub中的配置信息进行了更改,配置中心(服务端)可以动态获取到新的信息
但是客户端不能动态获取到配置中心的信息

1、引入库

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

2、application.yml

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

3、业务类

@RestController
@RefreshScope//<-----
public class ConfigClientController{}

4、刷新客户端

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值