Spring Cloud从入门到精通(七):配置中心 Spring Cloud Config

Spring Cloud Config

分布式系统中,每个服务都有许多的配置文件,当服务越来越多的时候,配置文件的管理就是让人头疼的一件事,所以我们需要有统一管理配置文件的组件。而Spring Cloud Config是就为分布式系统提供集中化的外部配置支持,它分为服务端和客户端两个部分,服务端也被称为分布式配置中心,它是一个独立的微服务应用,用来连接配置仓库并为客户端提供获取配置信息等访问接口,而客户端就是各个微服务应用,它们通过指定的配置中心来获取和加载配置信息。

入门案例

(1)首先我们需要在本地创建一个git仓库,然后创建一个springcloud目录作为配置文件目录。

在这里插入图片描述

在springcloud目录下创建两个配置文件

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

(2)创建一个项目config-server,引入依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
            <version>2.0.3.RELEASE</version>
        </dependency>
    </dependencies>

(3)编写启动类,添加注解

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

(4)编写配置文件添加Git仓库的相关信息

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          //git仓库地址,如果有账号密码还需要配置账号密码
          uri: C:\repository
          //指定仓库路径下要搜索的目录,可以配置多个
          search-paths: springcloud
server:
  port: 8900

(5)完成以上操作之后,启动项目,就可以通过url来访问配置内容了。
url和配置文件的映射如下。
配置文件是由application和profile组成。{application}-{profile}.properties或者{application}-{profile}.yml。例如didispace-dev.yml.

我们通过url访问时可以为

  • /{application}/{profile}/[{lable}]
  • /{application}-{profile}.yml
  • /{lable}/{application}-{profile}.yml
    lable为分支,不写默认为master。
例如:http://localhost:8900/didispace/dev/master

在这里插入图片描述

(6)创建一个客户端项目config-client,引入依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
            <version>2.0.3.RELEASE</version>
        </dependency>
    </dependencies>

(7)编写启动类,启动类不需要加特殊注解

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

(8)创建编写bootstrap.yml文件,注意是bootstrap.yml,bootstrap配置文件比application优先级更高,会优先加载。

spring:
  application:
  	//name要与配置文件的application对应
    name: didispace
  cloud:
    config:
      //对应配置文件的profile部分
      profile: dev
      //对应分支
      label: master
      //config-server地址
      uri: http://localhost:8900/

(9)我们创建一个类来读取远程的配置文件信息

@RestController
public class TestController {

	//通过该注解来绑定配置中心中的from属性
    @Value("${from}")
    private String from;

    @RequestMapping("/from")
    public String from(){
        return this.from;
    }
}

(10)启动服务,发现虽然我们没有配置端口号,但是最终服务端口号为8908,说明成功地读取到了配置文件。
访问http://localhost:8908/from,成功读取到了远程配置文件的信息

在这里插入图片描述

服务端详解

客户端启动时,根据bootstrap.yml中配置的应用名{application},环境名{profile},分支名{lable},向config-server请求获取配置信息。config-server根据自己维护的git仓库信息和客户端传递的配置文件信息来查找配置信息。然后通过git clone命令将找到的配置信息下载到config-server的文件系统中,config-server创建Spring的ApplicationContext实例,并从git本地仓库中加载配置文件,最后将这些配置内容读取出来返回给客户端应用,客户端获取外部配置信息后加载到客户端的ApplicationContext实例。config-server通过git clone将配置信息存于本地,起到了缓存的作用,当git服务端无法使用时,可以取缓存内容进行使用。

配置Git仓库

在服务端,对于配置仓库的默认实现采用了Git。{application}、{profile}、{lable}这些占位符可以在配置Git仓库的URI地址上使用。

例如:
spring:
  cloud:
    config:
      server:
        git:
          uri: C:\repository\{application}

{application}代表应用名,当客户端向config-server发起获取配置的请求时,config-server会根据客户端的spring.application.name信息来填充{application}占位符。

我们可以通过http://xxxx/{application}-config来根据不同的服务匹配不同的配置仓库。

search-paths参数同样支持占位符。
config-server还可以使用SVN配置仓库,这个可以自行去百度。

高可用配置

当服务实例很多时,所有的服务实例都需要从配置中心读取配置文件,所以我们需要将配置中心Config Server做成独立的服务,并且将其集群化,达到高可用。

(1)首先我们需要先启动注册中心Eureka Server。
(2)在Config-Server pom文件中引入Eureka依赖,改造为Eureka Client。

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
            <version>2.0.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.0.3.RELEASE</version>
        </dependency>
    </dependencies>
@EnableEurekaClient
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class);
    }
}
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: C:\repository\
          search-paths: springcloud
server:
  port: 8900
eureka:
  client:
    service-url:
            defaultZone:
                http://server1:8001/eureka/

(3)改造Config-Client同样作为Eureka-Client,在配置文件中只需要指定服务名和开启通过服务访问Config Server功能。

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
            <version>2.0.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.0.3.RELEASE</version>
        </dependency>
    </dependencies>
@EnableEurekaClient
@SpringBootApplication
public class ConfigClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class);
    }
}
spring:
  application:
    name: didispace
  cloud:
    config:
      label: master
      profile: dev
      discovery:
        service-id: config-server
        enabled: true
eureka:
  client:
    service-url:
            defaultZone:
                http://server1:8001/eureka/

(4)将Config Server开启两个实例,开启Config Client
访问http://localhost:8908/from
可以看到成功的读取到了配置文件,并且做了负载均衡。

在这里插入图片描述

失败快速响应

Spring Cloud Config的客户端会预先加载很多其他信息,然后再开始连接Config Server进行属性的注入,当我们构建的应用比较复杂时,可能在连接Config Server之前花费较长的启动时间,所以我们希望可以快速知道当前应用是否能顺序从Config Server获取到配置信息,这样可以减少我们的等待时间。要实现客户端优先判断Config Server获取是否正常,并快速响应失败内容,可以在客户端配置参数

spring:
  cloud:
    config:
      fail-fast: true
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值