• (八)统一配置中心-Config


    对于配置的重要性,我想我不用进行任何强调,大家都可以明白其重要性。在普通单体应用,我们常使用配置文件(application(*).properties(yml))管理应用的所有配置。这些配置文件在单体应用中非常胜任其角色,并没有让我们感觉到有头疼的地方。但随着微服务框架的引入,微服务数量就会在我们产品中不断增加,之前我们重点考虑的是系统的可伸缩、可扩展性好,但随之就是配置管理的问题就会一一暴露出来。起初微服务器各自管各自的配置,在开发阶段并没什么问题,但到了生产环境管理就会很头疼,如果要大规模更新某项配置,困难就可想而知。

    为此,在分布式系统中,Spring Cloud提供一个Config子项目,该项目核心就是配置中心,通过一个服务端和多个客户端实现配置服务。我们可使用配置服务器集中的管理所有服务的各种环境配置文件。配置服务中心默认采用Git的方式进行存储,因此我们很容易部署修改,并可以对环境配置进行版本管理。

    Spring Cloud Config具有中心化、版本控制、支持动态更新和语言独立等特性。其特点是:

    • 提供服务端和客户端支持(Spring Cloud Config Server和Spring Cloud Config Client);
    • 集中式管理分布式环境下的应用配置;
    • 基于Spring环境,实现了与Spring应用无缝集成;
    • 可用于任何语言开发的程序;
    • 默认实现基于Git仓库(也支持SVN),从而可以进行配置的版本管理;

    Spring Cloud Config的结构图如下:

     
    Config-结构图

    从图中可以看出Spring Cloud Config有两个角色(类似Eureka): Server和Client。Spring Cloud Config Server作为配置中心的服务端承担如下作用:

    • 拉取配置时更新Git仓库副本,保证是配置为最新;
    • 支持从yml、json、properties等文件加载配置;
    • 配合Eureke可实现服务发现,配合Cloud Bus(这个后面我们在详细说明)可实现配置推送更新;
    • 默认配置存储基于Git仓库(可以切换为SVN),从而支持配置的版本管理.

    而对于,Spring Cloud Config Client则非常方便,只需要在启动配置文件中增加使用Config Server上哪个配置文件即可。

     

    构建Config-Server(idea)

    pom如下

    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    

    启动类

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

      其中增加了@EnableConfigServer

    application.properties

    server.port=8890
    spring.application.name=server-config
    
    
    spring.cloud.config.server.git.uri=https://gitee.com/skyLogin/SpringCloundConfigGit.git
    spring.cloud.config.server.git.username=登录名
    spring.cloud.config.server.git.password=密码
    

      这里最重要的是需要配置Git仓库的地址及登录用户名和口令。

    我们在SpringCloundConfigGit仓库中提交如下文件
    user.properties
    project.name = sky
    

      
    user-dev.properties

    project.description = dev-description

    启动测试

    {
    	"name": "user",
    	"profiles": ["dev"],
    	"label": null,
    	"version": "9bc698347dcb4e82e1c8fc631d7409cc3f0e6a65",
    	"state": null,
    	"propertySources": [{
    		"name": "https://gitee.com/skyLogin/SpringCloundConfigGit.git/user-dev.properties",
    		"source": {
    			"project.description": "dev-description"
    		}
    	}, {
    		"name": "https://gitee.com/skyLogin/SpringCloundConfigGit.git/user.properties",
    		"source": {
    			"project.name": "sky"
    		}
    	}]
    }

    这里可以看到,我们提交到Git中的配置文件已经能够被server-config正确的读取到。

     

    构建config-client

    config-client可以是任何一个基于Spring boot的应用,这里为了讲解方便,我们构建一个非常简单的web工程。

    我们的config-client项目需要引入对spring-cloud-starter-config的依赖,如下:

     

    pom

    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-starter-config</artifactId>
    </dependency>

    启动类

    一个标准的Spring Boot启动类:

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

    编写测试Controller

    这个测试Controller主要就是验证我们可以从Git仓库中获取配置内容。

    @RestController
    public class ConfigController {
        @Value("${project.name}")
        String name;
    
        @Value("${project.description}")
        String description;
    
        @RequestMapping("/config/get/message")
        public String getMessage() {
            return name + " - " + description;
        }
    }

    编写配置文件

    这里编写的配置文件名称为:bootstrap.properties,内容如下:

    server.port=8891
    
    spring.application.name=user
    spring.cloud.config.profile=dev
    spring.cloud.config.uri= http://localhost:8890/
    

      

    定义了微服务的名称和profile以及配置服务器的地址。

    注意: 这些配置不能够配置在application.properties文件中,因为在Spring Boot启动时有引导上下文和应用上下文的概念,只有将配置服务器信息定义在引导上下文中,才能够从配置服务器中获取到配置信息。否则,服务启动时会报找不到变量定义的错误。

    启动测试

    说明,我们的config-client已经成功从server-config上获取到配置的数据了。

     
     

    链接:https://www.jianshu.com/p/997600098e6c
    來源:简书
  • 相关阅读:
    从aptitude 换回 apt-get .
    ubuntu 通过安装TOMCAT7
    第一次ubuntu使用的故障排除 ...the public key is not available: NO_PUBKEY...
    DES加密算法应用:分组加密模式
    使用“牛顿迭代法”求解方程
    html当中如何引用js文件
    javascript当中火狐的firebug如何单步调试程序?
    给出一个javascript的Helloworld例子
    java中讲讲PrintWriter的用法,举例?
    卷积和神经网络有什么关系?
  • 原文地址:https://www.cnblogs.com/skyLogin/p/10208892.html
Copyright © 2020-2023  润新知