Spring Cloud之——Config(配置中心)

Spring Cloud Config(配置中心)java

  你们好,有一段时间没有写技术博客了。因为工做上的事情,这方面很难分配时间。近几年随着服务化的兴起,一批服务化的框架应运而生,像dubbo,thrift,spring-cloud等。在国内使用dubbo的公司很是多,dubbo也是java程序员面试时必备知识点,并且它的官方文档写的很是清晰易懂,这都使得dubbo的普及很是容易。thrift是apache贡献的,彷佛也流行了一段时间,小编对这个框架不是很了解。随后spring-cloud一经推出,便在技术圈流行起来,这段时间小编也在学习spring-cloud,将学到的东西和你们分享一下,若是有不对的地方,还请你们多多指正。mysql

1、简介git

  Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持。使用Config Server,您能够为全部环境中的应用程序管理其外部属性。它很是适合spring应用,也可使用在其余语言的应用上。随着应用程序经过从开发到测试和生产的部署流程,您能够管理这些环境之间的配置,并肯定应用程序具备迁移时须要运行的一切。服务器存储后端的默认实现使用git,所以它轻松支持标签版本的配置环境,以及能够访问用于管理内容的各类工具。程序员

  Spring Cloud Config服务端特性github

  • HTTP,为外部配置提供基于资源的API(键值对,或者等价的YAML内容)
  • 属性值的加密和解密(对称加密和非对称加密)
  • 经过使用@EnableConfigServer在Spring boot应用中很是简单的嵌入。

  Config客户端的特性(特指Spring应用)面试

  • 绑定Config服务端,并使用远程的属性源初始化Spring环境。
  • 属性值的加密和解密(对称加密和非对称加密)

  入门示例:spring

  只要classpath下有Spring Boot Actuator和Spring Config Client,Spring Boot应用就会尝试链接配置服务http://localhost:8888,这个地址是spring.cloud.config.uri的默认地址。若是你想修改这个地址,你能够在bootstrap.[yml或properties]中设置spring.cloud.config.uri或者经过系统属性或者经过环境变量。sql

@Configuration
@EnableAutoConfiguration
@RestController
public class Application {

  @Value("${config.name}")
  String name = "World";

  @RequestMapping("/")
  public String home() {
    return "Hello " + name;
  }

  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }

}

  上面例子中的config.name能够来自本地的配置文件,也能够来自远程的配置服务。默认状况下,远程的配置服务将优先使用。apache

  为了运行你本身的配置服务中心,你可使用spring-cloud-config-server依赖,和@EnableConfigServer注解。若是你设置了spring.config.name=configserver,应用将会运行在8888端口,而且从一个样本仓库提供数据。你须要设置spring.cloud.config.server.git.uri来指定你本身的配置数据。默认的,它是一个git仓库,也能够配置成本地的文件系统。bootstrap

2、Spring Cloud Config服务端

  服务器为外部配置(键称值对或等效的YAML内容)提供了基于资源的HTTP。它能够在Spring Boot应用中使用@EnableConfigServer内嵌。例子以下:

@SpringBootApplication
@EnableConfigServer
public class SpringCloudConfigServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringCloudConfigServerApplication.class, args);
	}
}

  像全部的Spring Boot应用同样,它默认运行在8080端口,你能够经过多种方式将其切换到8888端口。最简单的能够设置spring.config.name=configserver(在Config Server的jar包有一个configserver.yml),它设置了一个默认的配置仓库。另一种方式是使用你本身的application.properties,这也是小编推荐的方式:

server.port: 8888
spring.cloud.config.server.git.uri: git地址

  git地址中是你的YAML或者properties文件。

  环境仓库

  你想在哪里存储配置数据?支持这种行为的策略是EnvironmentRepository,它服务于Environment实例。这个Environment是Spring Environment的一个浅副本。Environment经过3个变量被参数化。

  • {application}映射客户端的"spring.application.name"
  • {profile}映射客户端的"spring.profiles.active"(逗号分隔列表)
  • {label}它是服务端的特性,标记版本的一组配置文件

  仓库的实现一般表现的像Spring boot加载配置文件同样,"spring.config.name"等于{application}参数, "spring.profiles.active" 等于{profile}参数。profiles的优先规则和正常的规则是同样的,活动的profiles优于默认的。若是有多个profiles,则最后一个胜出。

  客户端的配置实例:

spring:
  application:
    name: foo
  profiles:
    active: dev,mysql

  在Spring Boot应用中,这些参数也能够经过环境变量或者命令行参数设置。

  git后端

  EnvironmentRepository的默认实现是使用git后端,它对管理更新、物理环境和审核更改很是的方便。要改变仓库的地址,你能够在配置服务端设置"spring.cloud.config.server.git.uri"属性(在application.properties文件中)。若是你用file:开头设置它,它将从本地仓库运行,这样能够在没有服务端的状况下很是快速和简单的启动。这种状况,服务端将直接在本地仓库中运行。为了扩展配置服务并使它高可用,你须要把服务的全部实例指向同一个仓库,所以只有共享文件系统能够工做。即便在这种状况下,最好使用共享文件系统存储库的ssh:协议,以便服务器能够将其克隆并使用本地工做副本做为缓存。

  该仓库的实现将HTTP资源中的{label}参数映射到git的标签(提交id、分支名称或者tag)。若是git分支或者tag名称中包含“/”,则HTTP URL中的label要使用特殊字符“(_)”代替。例如:若是分支的名称是foo/bar,则HTTP中的label的格式为foo(_)bar。这个特殊字符也能够用到{application}参数中。

  git URI中的占位符

  Spring Cloud Config Server支持在git URL中使用占位符,使用{application} 和 {profile}(若是使用{label},请记住它是使用在git标签中的)。所以你能够轻松的支持“一个应用一个仓库”的原则。以下:

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/myorg/{application}

  或者一个环境一个仓库的原则,使用{profile}代替{application}。另外在{application}参数中使用特殊字符"(_)"能够支持多组织。

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/{application}

  {application}参数的格式为"organization(_)application"。

  模式匹配和多仓库

  在{application}和{profile}参数中使用模式匹配能够支持更多复杂的需求。模式的格式是一组逗号分隔的{application}/{profile},其中的参数可使用通配符。例如:

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          repos:
            simple: https://github.com/simple/config-repo
            special:
              pattern: special*/dev*,*special*/dev*
              uri: https://github.com/special/config-repo
            local:
              pattern: local*
              uri: file:/home/configsvc/config-repo

  若是{application}/{profile}没有匹配到任何模式,它将使用默认的仓库地址:spring.cloud.config.server.git.uri。上面的例子中,"simple"仓库匹配的是“simple/*”(它仅仅匹配一个仓库simple,在全部的环境下)。"local"仓库将匹配全部{application}的名字以“local”开头的,而且也是在全部的环境下。“/*”前缀自动添加到全部没有设置{profile}的模式中。

  每个仓库也能够在子目录下存储配置文件,模式匹配也能够用于搜索这些目录,须要制定searchPaths,以下:

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          searchPaths: foo,bar*

  上面的例子中,将在foo和以bar开头的目录中,搜索配置文件。

  默认地,服务器在第一次请求配置文件时克隆远程的仓库,服务器也能够配置在启动的时候克隆仓库,以下:

spring:
  cloud:
    config:
      server:
        git:
          uri: https://git/common/config-repo.git
          repos:
            team-a:
                pattern: team-a-*
                cloneOnStart: true
                uri: http://git/team-a/config-repo.git
            team-b:
                pattern: team-b-*
                cloneOnStart: false
                uri: http://git/team-b/config-repo.git
            team-c:
                pattern: team-c-*
                uri: http://git/team-a/config-repo.git

  在上面的例子team-a的仓库将在服务端启动时进行克隆,其余的仓库将在第一次请求时克隆。

  认证

  若是远程的git仓库须要用户名和密码,能够参照下面的例子

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          username: trolley
          password: strongpassword

  到此,Spring Cloud Config服务端就介绍到这里,还有一些不经常使用的功能在这里就不介绍了,你们能够参照spring cloud官网。Spring Cloud Config服务端的代码示例能够参照个人GitHub地址:https://github.com/bigbugliu/spring-cloud-config-server。

3、Spring Cloud Config 客户端

  Spring Boot应用能够当即使用Spring Config Server。只要在classpath中有Spring Cloud Config Client的jar包,这个应用就会请求配置的服务端。他将使用绑定的配置服务器(spring.cloud.config.uri中配置的)的属性初始化spring环境。

  在某些状况下,若是服务没法链接到配置服务器,则可能但愿启动服务失败。若是这是所需的行为,请设置引导配置属性spring.cloud.config.failFast=true,客户端将以异常中止。

  若是您但愿配置服务器在您的应用程序启动时可能偶尔不可用,您能够要求它在发生故障后继续尝试。首先,您须要设置spring.cloud.config.failFast=true,而后您须要将spring-retry和spring-boot-starter-aop添加到您的类路径中。默认行为是重试6次,初始退避间隔为1000ms,指数乘数为1.1,用于后续退避。您可使用spring.cloud.config.retry.*配置属性配置这些属性(和其余)。

  详细代码请参考个人GitHub:https://github.com/bigbugliu/spring-boot-demo。

  

Spring Cloud Config就介绍完了,欢迎你们在评论区讨论。

相关文章
相关标签/搜索