Spring Cloud-Config Server 分布式配置中心

  随着我们接触微服务、分布式系统这些概念和应用后,由于每个功能模块都能拆分为一个独立的服务,服务数量不断增加。一次业务可能需要多个服务来共同协调才能完成,为了方便服务配置文件统一管理以及维护,我们就需要用到分布式配置中心组件,Spring Cloud提供了Spring Cloud Config Server分布式配置中心组件,如果大家使用过zookeeper去做分布式配置文件或者disconf之类的就变得十分容易理解了。
  Spring Cloud Config Server支持配置放在配置服务的内存中,也支持将其放在远程Git仓库中,而在Spring Cloud提供的分布式配置服务中主要包含两个角色Config Server和Config Client。我们可以将公用的外部配置文件集中放在一个Git仓库中,然后建立一个Config Server来管理所有的配置文件,维护配置文件的时候只需要修改该Git仓库中的配置文件,其他所有服务都可以作为Config Client通过Config Server来获取所需配置文件,当然所有服务都通过同一个服务中心获取配置文件,那么依赖性就相对增大,若服务中心挂了之后其他服务都不能正常使用,所以这里是支持集群化部署的。

在查阅资料的时候看到一张架构图,大家可以参考理解下

这里写图片描述

1.Config Server基本使用

首先新建一个项目用于做配置中心服务使用,添加所需依赖

pom.xml

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
    <version>1.4.3.RELEASE</version>
</dependency>
我们只要在主程序上添加@EnableConfigServer注解,就可以使该服务变为配置服务

ConfigServerApp.java

package com.ithzk.spring.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

/**
 * Hello world!
 *
 */
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApp
{
    public static void main( String[] args )
    {
        SpringApplication.run(ConfigServerApp.class);
    }
}
这里我们把配置文件放在远程Git上,为了方便这里我暂用码云,然后将项目地址配置到我们该服务中

application.yml

server:
  port: 12900
spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/onethree/spring-cloud-config #配置文件的仓库地址
我们在码云上新建一个项目,添加一个application.yml的配置文件

这里写图片描述
这里写图片描述

完成以上步骤后启动服务,访问localhost:12900/abc-default.properties
或者localhost:12900/abc-default.yml,由于abc-default.yml文件不存在,config server会默认去获取
application.yml的配置

这里写图片描述

我们再新增一个config-dev.yml文件

这里写图片描述
这里写图片描述

无需重启,访问localhost:12900/abc-default.yml

这里写图片描述

我们可以发现如果你获取的配置文件不存在则会自动获取默认的配置文件
如果存在则会将默认的和本身存在的一起获取,如果属性名称相同,指定文件中的属性会覆盖默认文件中的属性,例如此处abc
我们还可以通过访问localhost:12900/配置文件名称/default/master查看该配置文件名可获取对应的配置文件

这里写图片描述
这里写图片描述

2.Config Client基本使用

上面我们搭建了一个Config server配置中心,当我们服务需要使用配置中心里面的配置时该服务就可以被看作是一个config
client,下面给一个小示例来简单介绍下如何是使用config client去获取config server里我们所需的配置文件

pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
</dependency>

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

application.yml

server:
  port: 13900

bootstrap.yml

spring:
  cloud:
    config:
      uri: http://localhost:12900 # config-server地址,配置server相关的属性应该放到 bootstrap.yml 中,bootstrap.yml会在加载application.yml之前加载
      profile: dev
      label: master #当使用 git 的时候 lable 默认是 master
  application:
    name: config #获取name-profile对应的文件,会获以config开头dev的文件

ConfigClientApp.java

package com.ithzk.spring.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Hello world!
 *
 */
@SpringBootApplication
public class ConfigClientApp
{
    public static void main( String[] args )
    {
        SpringApplication.run(ConfigClientApp.class);
    }
}

ConfigClientController .java

package com.ithzk.spring.cloud.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author hzk
 * @date 2018/7/4
 */
@RestController
public class ConfigClientController {

    @Value("${profile}")
    private String profile;

    @GetMapping("getProfile")
    public String getProfile(){
        return profile;
    }


}
访问接口localhost:13900/getProfile会从config server配置的远程仓库中获取对应名称的配置文件

这里写图片描述

3.Config通配符模式

之前我们配合的config server是从指定的spring-cloud-config仓库中获取配置文件,当我们需要从多个仓库中获取的时候,
我们可以利用Config Server提供的通配符配置模式去配置,这里我们建好三多个仓库并放好配置文件

这里写图片描述
这里写图片描述

如何让我们Config Server同时可以从多个仓库中获取配置文件呢,这里需要修改一下配置,将之前指定仓库改为{application}

application.yml

server:
  port: 12900
spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/onethree/{application}
通过ip:port/仓库名称-dev.properties(yml)即可从获取指定仓库中所有配置属性

这里写图片描述
这里写图片描述

4.Config模式匹配

上面我们使用通配符模式可以使一个Config Server同时连接多个仓库获取配置文件,我们还可以设置模式匹配
使特定仓库配置文件具有特定规则才可以获取

application.yml

server:
  port: 12900
spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/onethree/spring-cloud-config # 默认配置
          repos:
            simple:
              uri: https://gitee.com/onethree/simple # 当访问的是 simple 的时候执行
            special:
              uri: https://gitee.com/onethree/special # 当访问的是 special 并且符合以下规则的时候请求的 uri
              pattern: special*/dev*,special*/test*

这里我们可以看到只要符合simple前缀都可以从simple仓库中获取配置文件

这里写图片描述
这里写图片描述

但是我们special配置了特定规则访问,只有符合规则才可以获取special仓库内容,否则获取默认配置仓库内容

这里写图片描述
这里写图片描述

5.Config搜索路径配置

如果我们想在仓库里面建不同文件夹去管理不同配置文件,可以实现后获取吗
这里,我们先建一个searchabc文件夹然后新建一个配置文件

这里写图片描述

通过访问路径获取可以发现获取的是默认的配置文件,也就是说无法获取到指定文件夹里配置文件内容

这里写图片描述

那么我们需要怎么去获取指定目录下配置文件呢,只需增加配置

bootstrap.yml

server:
  port: 12900
spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/onethree/spring-cloud-config # 默认配置
          search-paths:
            - searchabc # 以 searchabc 开头的会被转到 searchabc 目录
          clone-on-start: true # 启动就加载文件
          #username: 读时可以不设但是写时建议设置
          #password:

这里写图片描述

通过以上配置,我们就可以达到从指定目录下获取配置文件的效果,这里还可以配置用户名和密码控制安全性

6.Config Server整合认证

我们可以整合spring-boot提供的安全配置达到config server实现认证

pom.xml

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
    <version>1.4.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

application.yml

server:
  port: 12900
spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/onethree/spring-cloud-config # 默认配置
          search-paths:
            - searchabc # 以 searchabc 开头的会被转到 searchabc 目录
          clone-on-start: true
security:
  user:
    name: configserver
    password: configserver
  basic:
    enabled: true

ConfigServerAuthApp.java

package com.ithzk.spring.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

/**
 * Hello world!
 *
 */
@SpringBootApplication
@EnableConfigServer
public class ConfigServerAuthApp
{
    public static void main( String[] args )
    {
        SpringApplication.run(ConfigServerAuthApp.class);
    }
}
启动服务之后访问发现需要认证才可获取配置

这里写图片描述
这里写图片描述

猜你喜欢

转载自blog.csdn.net/u013985664/article/details/80900095