Spring Cloud——配置中心Config

上篇:Spring Cloud——分布式系统延迟容错组件Hystrix断路器

1. Spring Cloud Config简介

Spring Cloud Config 用于为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,分为server端和client端。server端为分布式配置中心,是一个独立的微服务应用;client端为分布式系统中的基础设置或微服务应用,通过指定配置中心来管理相关的配置。
Spring Cloud Config 构建的配置中心,除了适用于 Spring 构建的应用外,也可以在任何其他语言构建的应用中使用。
在这里插入图片描述
Spring Cloud Config 默认采用 Git 存储配置信息,支持对配置信息的版本管理。统一配置中心有一个config-server端,它会从远端的git拉取配置,并且同步到本地git中,所以即使远端git挂掉了,本地git依然能够提供支持。右边的product和order两个微服务是统一配置的客户端,可以从config-server获取配置。官网地址:https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.1.RELEASE/reference/html/

2. 搭建config-server服务

2.1 引入config server依赖
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
2.2 添加启动config注解
/**
 * @author Administrator
 * @version 1.0
 **/
@EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigApplication {
	public static void main(String[] args) throws Exception {
		SpringApplication.run(ConfigApplication.class, args);
	}
	
}
2.3 配置git仓库地址
server:
  port: 8080 
spring:
  application:
    name: springcloud-config
cloud:
  config:
    server:
      git:
        uri: https://github.com/qqxhb/springcloud-demo.git #配置文件在github上的地址
        search-paths: config  #Configserver会在 Git仓库根目录、 config子目录中查找配置文件。
#          clone-on-start: true  #启动时就clone仓库到本地,默认是在配置被首次请求时,config server才会clone git仓库
        #native:
          #search-locations: classpath:/config #若配置中心在本地,本地的地址
eureka:
  client:
    registerWithEureka: true #服务注册开关
    fetchRegistry: true #服务发现开关
    serviceUrl: #Eureka客户端与Eureka服务端进行交互的地址,多个中间用逗号分隔
        defaultZone: ${EUREKA_SERVER:http://localhost:8761/eureka/,http://localhost:8762/eureka/}
  instance:
    prefer-ip-address:  true  #将自己的ip地址注册到Eureka服务中
    ip-address: ${IP_ADDRESS:127.0.0.1}
    instance-id: ${spring.application.name}:${server.port} #指定实例id

2.4 访问配置仓库文件

在这里插入图片描述
访问上图中的application-eureka.yml文件的内容:http://127.0.0.1:8080/application-eureka.yml
在这里插入图片描述
spring cloud config可以自动转换yml和properties,比如同样访问刚刚的文件,将地址栏中的后缀改成properties:http://127.0.0.1:8080/application-eureka.properties
在这里插入图片描述

2.5 路径访问规则

参考官网:https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.1.RELEASE/reference/html/

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

application就是配置文件的名字, profile就是对应的环境 label就是不同的分支(默认为master)。

3. 客户端获取配置

3.1 引入config client依赖
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
3.2 配置bootstrap.yml

参考官网:https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.1.RELEASE/reference/html/#_spring_cloud_config_client

spring:
  cloud:
    config:
      name: application-teacher #这是我们要读取的配置文件名 对应获取规则的{application}
      profile: dev   #这个是要获取的环境 对应的便是{profile}
      label: master #这个就是获取的节点 对应的是{label}
      uri: http://localhost:8080/ #这就是我们config server的一个地址
     

spring cloud有一个“引导上下文"的概念,这是主应用程序的父上下文。引导上下文负责从配置服务器加载配置属性,以及解密外部配置文件中的属性。和主应用程序加载application.(yml或 properties)中的属性不同,引导上下文加载(bootstrap.)中的属性。配置在 bootstrap.*中的属性有更高的优先级。
上面的config-server可以搭建多个服务构成高可用的集群,配置中就不能配置uri而应该配置服务的ID

spring:
  cloud:
    config:
      name: application-teacher #这是我们要读取的配置文件名 对应获取规则的{application}
      profile: dev   #这个是要获取的环境 对应的便是{profile}
      label: master #这个就是获取的节点 对应的是{label}
      discovery:
        enabled: true
        service-id: springcloud-config

源码地址:https://github.com/qqxhb/springcloud-demo中的springcloud-config和teacher-service模块。
下一篇:Spring Cloud——链路跟踪Sleuth

发布了118 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43792385/article/details/104700128