zoukankan      html  css  js  c++  java
  • Spring Cloud(九)Config配置中心

    1 概述

    架构图

    1.1 产生背景

    分布式系统中,会将服务拆分成一个个独立的服务,这些服务都要通过配置文件配置相应信息才能运行,随着系统内微服务数量的增多,配置文件也会不断的增多,大量的配置文件的管理成为一个繁琐的问题。因此一套集中式的、动态的配置管理设施是必不可少的。

    1.2 Spring Cloud Config配置中心

    • SpringCloud Config是一个提供外部集中式配置管理的组件,配置服务器为各种不同的微服务提供了一个中心化的外部配置中心。
    • SpringCloud Config分为服务端和客户端两部分:
      • 服务端:分布式配置中心,是一个独立的微服务,用来连接并为客户端提供配置、加密/解密信息等访问入口。
      • 客户端:通过指定的配置中心获取配置资源,推荐用git来存储配置文件。

    1.3 解决的问题

    • 集中管理配置文件。
    • 不同的环境对应不同的配置,动态化的更新配置。
    • 不需要在每个服务部署的机器上编写配置文件,让服务中心统一为服务拉取配置文件。
    • 将配置信息以REST接口形式暴露。

    2 案例

    2.1 服务端

    1. 新建github远程仓库,如:spring-cloud-config。
    2. 将新建的远程仓库克隆到本地,新建date-dev.yml配置文件,将该文件上传至远程仓库。
    date: 
      description: today is 2018-10-31
    # 保存为UTF-8格式
    
    1. 新建config server服务模块,加入依赖。
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>
    
    1. appliacation.yml配置文件。
    server: 
      port: 2233  
    spring:
      application:
        name:  spring-cloud-config-server
      cloud:
        config:
          server:
            git:
              uri: https://github.com/chenyulin19930815/spring-cloud-config.git #GitHub上面的git仓库名字
              username: xxx #git仓库用户名
              password: xxxx #github仓库密码
    
    1. 编写主启动类,加入@EnableConfigServer注解。
    @SpringBootApplication
    @EnableConfigServer
    public class SpringCloudConfigServerApplication2233 {
    	public static void main(String[] args) {
    		SpringApplication.run(SpringCloudConfigServerApplication2233.class, args);
    	}
    }
    
    1. 启动服务,访问配置文件。

    有以下访问规则:

    • {application}:配置文件的文件名
    • {profile}:读取的环境
    • {lable}:分支
    1. /{application}/{profile}[/{label}]
    2. /{application}-{profile}.yml
    3. /{label}/{application}-{profile}.yml
    4. /{application}-{profile}.properties
    5. /{label}/{application}-{profile}.properties
    1. 本示例有以下访问方式。

    2.2 客户端

    1. 在本地仓库新建spring-cloud-config-client.yml上传至远程仓库。
    spring:
      profiles:
        active: dev
    ---
    server:
      port: 2244
    spring:
      profiles: dev
      application:
        name: spring-cloud-config-client
    ---
    server:
      port: 2245
    spring:
      profiles: test
      application:
        name: spring-cloud-config-client
    
    1. 新建config client服务模块,导入依赖。
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    
    1. 新建bootstrap.yml1配置文件。
    spring:
      cloud:
        config:
          name: spring-cloud-config-client #从git仓库读取的配置文件资源名称,没有后缀
          profile: dev
          label: master
          uri: http://localhost:2233 #本微服务启动后先去找配置中心服务端,以获取GitHub的服务地址
    
    1. 新建application.yml
    spring:
      application:
        name: spring-cloud-config-client
    
    1. 启动类。
    @SpringBootApplication
    public class SpringCloudConfigClientApplication2244 {
    	public static void main(String[] args) {
    		SpringApplication.run(SpringCloudConfigClientApplication2244.class, args);
    	}
    }
    
    1. web测试controller。
    @RestController
    public class ConfigController {
    	@Value("${spring.application.name}")
    	private String applicationName;
    
    	@Value("${spring.profiles}")
    	private String springProfiles;
    
    	@Value("${server.port}")
    	private String port;
    
    	@RequestMapping("/config")
    	public String getConfig() {
    		return "applicationName: " + applicationName +"<br/>springProfiles:"+springProfiles+"<br/>port: " + port;
    	}
    }
    
    1. 测试:依次启动config server,config client服务。
      首先,dev启动的端口为2244,访问:http://localhost:2244/config
      在这里插入图片描述
      然后,更改bootstrap.yml中profile: dev为profile: test,端口切换为了2245,访问:http://localhost:2245/config
      在这里插入图片描述

    遗留问题
    将spring-cloud-config-client.yml文件中test的spring.application.name=spring-cloud-config-client改为spring.application.name=spring-cloud-config-client-test,再次访问发现名字没有更换,说明修改github配置信息后,系统不会主动去获取。

    看源码点这里


    1. 一,bootstrap.yml比application.yml具有更高的优先级。
      二,bootstrap.yml是系统级的资源配置项,application.yml是用户级的资源配置项。
      三,SpringCloud会创建"BootStrap Context"作为"ApplicationContext"的父上下文。初始化的时候BootStrap Context负责从外部源加载配置属性并解析。这两个上下文共享一个"Environment",BootStrap 具有更高优先级,他们不会被本地配置覆盖。 ↩︎

    只有把命运掌握在自己手中,从今天起开始努力,即使暂时看不到希望,也要相信自己。因为比你牛几倍的人,依然在努力。
  • 相关阅读:
    C# 酒鬼买酒喝,瓶盖和空瓶子可以换新的酒
    C# 图结构操作
    C# 二叉堆
    Unity 单元测试(NUnit,UnityTestTools)
    Unity PlayerPrefs类进行扩展(整个对象进行保存)
    客户端操作判断以服务器时间为准
    C# 使用枚举获取对应的数组值时
    C# 实现简单状态机(参考代码)
    叶脉图案以及藤蔓生长算法在houdini里面的实现 Leaf Venation
    steering behaviors 转向行为 集群模拟 小结
  • 原文地址:https://www.cnblogs.com/freesky168/p/14358218.html
Copyright © 2011-2022 走看看