SpringCloud微服务框架04 - Config统一配置中心

本系列持续更新中

SpringCloud微服务框架01-SpringCloud简介

SpringCloud微服务框架02-Eureka服务注册与发现

SpringCloud微服务框架03 - Ribbon负载均衡

SpringCloud微服务框架04 - Config统一配置中心

文章中设计到的项目源码,会逐步整理到github上。github除了本系列文章设计到的源码信息,还有Spring Cloud整合的项目框架。有同样正在学习Spring Cloud的小伙伴可以加我一起学习交流。

github地址:https://github.com/hack-feng/maple-cloud

一、Spring Cloud Config 是什么

Spring Cloud Config 是用来为分布式系统中的基础设施和微服务应用提供的集中化的外部配置支持。分为服务端和客户端两个部分。其中服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置仓库并为客户端提供获取配置信息。实现了对服务器端和客户端中环境变量和属性配置的抽象映射。

说直白一点:就是把springboot项目中的application.yml配置文件抽取出来,放在一起统一管理。

可以将配置信息放在git,svn或本地化的文件系统。

二、构建Spring Cloud Config配置中心

  • 创建一个springboot工程,命名为config-demo,并在pom.xml文件引入下面依赖:
<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.maple</groupId>
    <artifactId>config-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-demo</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <!-- 引入config-server配置中心 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>

    <!-- 引入spring cloud -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

  • 在springboot项目程序主类添加@EnableConfigServer注解,开启Spring Cloud Config的服务功能。
@SpringBootApplication
@EnableConfigServer
public class ConfigDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigDemoApplication.class, args);
    }
}
  • 在application.yml中添加配置服务的基本信息以及Git仓库的相关信息。
server:
  port: 2001

spring:
  application:
    name: config-demo
  cloud:
    config:
      profile: dev
      server:
        git:
          uri: https://github.com/hack-feng/maven-cloud-demo
          search-paths: /
          # username: username 公有库不需要设置账号密码
          # password: password    
      label: master

Git的配置分别对应以下内容

  • spring.cloud.config.profile:对应的是开发版本,对应配置规则中的profile,见下面介绍
  • spring.cloud.config.label:Git代码的分支信息,默认是master
  • spring.cloud.config.server.uri:配置Git仓库位置
  • spring.cloud.config.server.search-paths:配置Git仓库路径下的相对搜索位置,可以配置多个
  • spring.cloud.config.server.username:配置Git仓库用户名,共有库可以不设置
  • spring.cloud.config.server.password:配置Git仓库密码

配置规则详解

在Git仓库中https://github.com/hack-feng/maven-cloud-demo下创建以下配置文件

  • maple-config.yml
  • maple-config-dev.yml
  • maple-config-test.yml
    内容对应:
mycontent: default-1.0
mycontent: dev-1.0
mycontent: test-1.0

注意使用yml格式 :后面跟一个空格
端点与配置文件的映射规则如下:

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

{application}表示微服务的名字,即:spring.application.name
{profile}表示开发版本,即:spring.cloud.config.profile
{label}表示Git仓库的分支,即:spring.cloud.config.label

至此,已成功构建了Config Server,并通过构造URL方式,获取了Git仓库的配置信息。

三、编写config Client

上文已经构造了config-demo,并使用Config Server端点获取到了配置信息。接下来讨论SpringCloud微服务如何获取配置信息。

  • 创建一个springBoot工程,命名为:config-client-demo ,pom.xml文件如下:
<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.maple</groupId>
    <artifactId>config-client-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-client-demo</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- 引入web模块 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 引入config客户端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-boot-starter-config</artifactId>
        </dependency>
    </dependencies>

    <!-- 引入spring cloud -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>


  • springboot项目程序主类程序。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConfigClientDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientDemoApplication.class, args);
    }
}
  • 创建bootstarp.yml配置,指定congfig-server的位置,如下
spring:
  application:
    name: maple-config
  cloud:
    config:
      uri: http://localhost:2001
      profile: dev
      label: master
server:
  port: 2002
  • 创建测试ConfigTestController.java 内容如下
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigTestController {


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

    @GetMapping(value = "test")
    public String test(){
        return "获取到配置中心的内容:" + mycontent;
    }
}

启动项目时,打印一下内容,则表示调用配置服务成功。

2019-07-13 21:50:46.750  INFO 10360 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: http://localhost:2001
2019-07-13 21:50:48.982  INFO 10360 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=maple-config, profiles=[dev], label=master, version=d716392074e316e6cec9b4311e3bad52fb1ea91d, state=null
2019-07-13 21:50:48.983  INFO 10360 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource [name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://github.com/hack-feng/maven-cloud-demo/maple-config-dev.yml'}, MapPropertySource {name='https://github.com/hack-feng/maven-cloud-demo/maple-config.yml'}]]

到这里spring Cloud Config统一配置中心就搭建好了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

笑小枫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值