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

本文涉及的产品
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
简介: 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


启动项目,访问:http://127.0.0.1:2001/maple-config/dev如下图

20190713210916763_.png

至此,已成功构建了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统一配置中心就搭建好了。

目录
相关文章
|
2天前
|
Prometheus 监控 负载均衡
【SpringCloud】微服务重点解析
【SpringCloud】微服务重点解析
13 0
|
2天前
|
缓存 负载均衡 算法
【微服务 SpringCloud】实用篇 · Ribbon负载均衡
【微服务 SpringCloud】实用篇 · Ribbon负载均衡
9 0
|
2天前
|
负载均衡 监控 算法
【微服务 SpringCloud】实用篇 · Eureka注册中心
【微服务 SpringCloud】实用篇 · Eureka注册中心
11 1
|
2天前
|
存储 SpringCloudAlibaba Java
【微服务 SpringCloud】实用篇 · 服务拆分和远程调用
【微服务 SpringCloud】实用篇 · 服务拆分和远程调用
16 2
|
2天前
|
前端开发 API
nuxt.config.js 配置
我们在使用Nuxt.js提供的create-nuxt-app 创建项目后,更希望对它自定义一些东西,这里我们可以在根目录下找到nuxt.config.js
19 7
|
2天前
|
XML SQL Java
SpringCloud 基础配置
SpringCloud 基础配置
13 0
|
2天前
|
开发框架 JSON .NET
.Net4.0 Web.config 配置实践
.Net4.0 Web.config 配置实践
|
2天前
|
消息中间件 Java RocketMQ
Spring Cloud RocketMQ:构建可靠消息驱动的微服务架构
【4月更文挑战第28天】消息队列在微服务架构中扮演着至关重要的角色,能够实现服务之间的解耦、异步通信以及数据分发。Spring Cloud RocketMQ作为Apache RocketMQ的Spring Cloud集成,为微服务架构提供了可靠的消息传输机制。
30 1
|
2天前
|
Dubbo Java 应用服务中间件
Spring Cloud Dubbo: 微服务通信的高效解决方案
【4月更文挑战第28天】在微服务架构的发展中,服务间的高效通信至关重要。Spring Cloud Dubbo 提供了一种基于 RPC 的通信方式,使得服务间的调用就像本地方法调用一样简单。本篇博客将探讨 Spring Cloud Dubbo 的核心概念,并通过具体实例展示其在项目中的实战应用。
20 2
|
2天前
|
负载均衡 Java 网络架构
【SpringCloud】如何理解分布式、微服务、集群
【SpringCloud】如何理解分布式、微服务、集群
24 1