Spring Cloud Config构建配置中心

  1. Spring Cloud Config是什么,能做什么?

    它是通过SpringBoot构造的,用来为分布式系统提供配置中心。由中心来管理所有环境应用程序的属性,便于在整个项目周期统一调配配置。支持将信息放在github中,在服务端做简单的配置即可从远程仓库中获取配置信息,而所有的客户端只需要指定去获取信息的服务连接,然后就可以直接使用各种变量信息。
  2. Spring Cloud Config构建

    1. Server端构建

      先去github创建个repository(当然可以使用默认的,需要配置spring.config.name=configserver,因为Spring-cloud-config-server-*.jar包中已经给出了默认的服务器配置信息和测试地址,Spring-cloud-config-server-2.0.2.jar的文件中叫configserver.yml),然后创建一个创建一个目录和一个文件,用来做服务器同步数据测试。比如我已经创建好的https://github.com/poetLoo/gitRepository

      创建一个SpringBoot项目,然后配置Spring Cloud config依赖,启动类中加入注解:@EnableConfigServer

      。我这里使用maven来构建项目,具体的pom如下:

      <?xml version="1.0" encoding="UTF-8"?>
      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      	<modelVersion>4.0.0</modelVersion>
      
      	<groupId>com.example</groupId>
      	<artifactId>springCloud</artifactId>
      	<version>0.0.1-SNAPSHOT</version>
      	<packaging>jar</packaging>
      
      	<name>springCloudConfig</name>
      	<description>Demo project for Spring Boot</description>
      
      	<parent>
      		<groupId>org.springframework.boot</groupId>
      		<artifactId>spring-boot-starter-parent</artifactId>
      		<version>2.1.0.RELEASE</version>
      		<relativePath/> <!-- lookup parent from repository -->
      	</parent>
      
      	<properties>
      		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      		<java.version>1.8</java.version>
      	</properties>
      
      	<dependencyManagement>
      	    <dependencies>
      	        <dependency>
      	            <groupId>org.springframework.cloud</groupId>
      	            <artifactId>spring-cloud-dependencies</artifactId>
      	            <version>Finchley.SR2</version>
      	            <type>pom</type>
      	            <scope>import</scope>
      	        </dependency>
      	    </dependencies>
      	</dependencyManagement>
      	
      	<dependencies>
      		
      		<dependency>
      	        <groupId>org.springframework.cloud</groupId>
      	        <artifactId>spring-cloud-starter-config</artifactId>
      	    </dependency>
      	    <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-config-server</artifactId>
              </dependency>
              
      		<dependency>
      			<groupId>org.springframework.boot</groupId>
      			<artifactId>spring-boot-starter-test</artifactId>
      			<scope>test</scope>
      		</dependency>
      		
      	</dependencies>
      
      	<build>
      		<plugins>
      			<plugin>
      				<groupId>org.springframework.boot</groupId>
      				<artifactId>spring-boot-maven-plugin</artifactId>
      			</plugin>
      		</plugins>
      	</build>
      
      
      </project>
      

      需要注意的是,Spring boot和Spring cloud的版本要能对应起来,否则项目启动会报错.版本对应关系:

      Spring CloudSpring Boot
      Finchley兼容Spring Boot 2.0.x,不兼容Spring Boot 1.5.x
      Dalston和Edgware兼容Spring Boot 1.5.x,不兼容Spring Boot 2.0.x
      Camden兼容Spring Boot 1.4.x,也兼容Spring Boot 1.5.x
      Brixton兼容Spring Boot 1.3.x,也兼容Spring Boot 1.4.x
      Angel兼容Spring Boot 1.2.x

      如果使用我们自己的repository,那么application.yml配置如下:

      server:
        port: 8888
      
      spring:
        cloud:
          config:
            server:
              git:
                #指定配置中心地址
                uri: https://github.com/poetLoo/cloudConfig
                #设置搜索目录,如果不设置,默认只搜索根目录
                search-paths:
                - myConfig
        name: server
        application:
          name: configServer

      启动后在请求地址:http://localhost:8888/config-sample/master

      返回信息如下:

      {
          "name": "config-sample",
          "profiles": [
              "master"
          ],
          "label": null,
          "version": "168139978ce7b76e646f222cc6fb1ff8f2203366",
          "state": null,
          "propertySources": [
              {
                  "name": "https://github.com/poetLoo/cloudConfig/myConfig/config-sample.yml",
                  "source": {
                      "appName": "poetLav",
                      "version": "1.0.0"
                  }
              }
          ]
      }

      请求格式和资源映射如下:「比如我将上面的search-paths配置为 '{profile}',那我就可以像这样请求http://localhost:8888/config-sample/myConfig/master

      /{application}/{profile}[/{label}]
      /{application}-{profile}.yml
      /{label}/{application}-{profile}.yml
      /{application}-{profile}.properties
      /{label}/{application}-{profile}.properties
    2. Client端构建

      新建一个项目,作为客户端构建和服务端基本一致,在pom中多加一个依赖,表示是一个web服务

      <dependency>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-web</artifactId>

      </dependency>


      主要是在resource目录下添加一个bootstrap.yml文件,基本配置如下:
      spring:
        application:
          name: config-sample
        cloud:
          config:
            uri:
            - http://localhost:8888
            

      添加一个简单的controller:
       

      import org.springframework.beans.factory.annotation.Value;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RestController;
      
      @RestController
      public class HelloConfigController {
      
      	@Value("${appName}") String name;
      	@Value("${version}") String version;
      
      	@RequestMapping("/helloConfig")
      	public Object hello() {
      		return "hello " + name
      				+" version:"+ version;
      	}
      }

      请求http://localhost:8080/helloConfig,输出:hello poetLav version:1.0.0 说明已经从服务器端同步到配置数据。

  3. 总结

    Spring cloud Config Server端的repository可以由多种方式,默认是使用远程git仓库、还可以是本地文件系统、自己搭建配置中心的方式。由于git本身是一个分布式版本控制系统,可以方便的查看和追踪资源变更记录,便于管理,所以默认推荐。
    server端还由其他一些配置策略,比如同步远程仓库的版本、是否启动时克隆配置信息到本地、连接不上远程配置时处理等,在使用到的时候可以去探索。
  4. 参考:https://www.cnblogs.com/boboooo/p/8796636.html?utm_source=debugrun&utm_medium=referral
               https://blog.csdn.net/hunan961/article/details/79230859
               https://www.cnblogs.com/ouyida3/p/9125413.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值