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

简介: 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统一配置中心就搭建好了。

目录
相关文章
|
5月前
|
算法 Java 微服务
【SpringCloud(1)】初识微服务架构:创建一个简单的微服务;java与Spring与微服务;初入RestTemplate
微服务架构是What?? 微服务架构是一种架构模式,它提出将单一应用程序划分为一组小的服务,服务之间互相协调、互相配合,为用户提供最终价值。 每个服务允许在其独立的进程中,服务于服务间采用轻量级的通信机制互相协作(通常是Http协议的RESTful API或RPC协议)。 每个服务都围绕着具体业务进行构建,并且能够被独立的部署到生产环境、类生产环境等。另外应当尽量避免统一的、集中式的服务管理机制,对具体的一个服务而言,应根据上下文,选择合适的语言、工具对其进行构建
573 126
|
5月前
|
负载均衡 算法 Java
【SpringCloud(2)】微服务注册中心:Eureka、Zookeeper;CAP分析;服务注册与服务发现;单机/集群部署Eureka;连接注册中心
1. 什么是服务治理? SpringCloud封装了Netfix开发的Eureka模块来实现服务治理 在传统pc的远程调用框架中,管理每个服务与服务之间依赖关系比较复杂,管理比较复杂,所以需要使用服务治理,管理服务于服务之间依赖关系,可以实现服务调用、负载均衡、容错等,实现服务发现与注册
365 0
|
7月前
|
监控 Java API
Spring Boot 3.2 结合 Spring Cloud 微服务架构实操指南 现代分布式应用系统构建实战教程
Spring Boot 3.2 + Spring Cloud 2023.0 微服务架构实践摘要 本文基于Spring Boot 3.2.5和Spring Cloud 2023.0.1最新稳定版本,演示现代微服务架构的构建过程。主要内容包括: 技术栈选择:采用Spring Cloud Netflix Eureka 4.1.0作为服务注册中心,Resilience4j 2.1.0替代Hystrix实现熔断机制,配合OpenFeign和Gateway等组件。 核心实操步骤: 搭建Eureka注册中心服务 构建商品
1098 3
|
5月前
|
负载均衡 Java API
《深入理解Spring》Spring Cloud 构建分布式系统的微服务全家桶
Spring Cloud为微服务架构提供一站式解决方案,涵盖服务注册、配置管理、负载均衡、熔断限流等核心功能,助力开发者构建高可用、易扩展的分布式系统,并持续向云原生演进。
|
6月前
|
监控 安全 Java
Spring Cloud 微服务治理技术详解与实践指南
本文档全面介绍 Spring Cloud 微服务治理框架的核心组件、架构设计和实践应用。作为 Spring 生态系统中构建分布式系统的标准工具箱,Spring Cloud 提供了一套完整的微服务解决方案,涵盖服务发现、配置管理、负载均衡、熔断器等关键功能。本文将深入探讨其核心组件的工作原理、集成方式以及在实际项目中的最佳实践,帮助开发者构建高可用、可扩展的分布式系统。
370 1
|
6月前
|
jenkins Java 持续交付
使用 Jenkins 和 Spring Cloud 自动化微服务部署
随着单体应用逐渐被微服务架构取代,企业对快速发布、可扩展性和高可用性的需求日益增长。Jenkins 作为领先的持续集成与部署工具,结合 Spring Cloud 提供的云原生解决方案,能够有效简化微服务的开发、测试与部署流程。本文介绍了如何通过 Jenkins 实现微服务的自动化构建与部署,并结合 Spring Cloud 的配置管理、服务发现等功能,打造高效、稳定的微服务交付流程。
724 0
使用 Jenkins 和 Spring Cloud 自动化微服务部署
|
6月前
|
Kubernetes Java 微服务
Spring Cloud 微服务架构技术解析与实践指南
本文档全面介绍 Spring Cloud 微服务架构的核心组件、设计理念和实现方案。作为构建分布式系统的综合工具箱,Spring Cloud 为微服务架构提供了服务发现、配置管理、负载均衡、熔断器等关键功能的标准化实现。本文将深入探讨其核心组件的工作原理、集成方式以及在实际项目中的最佳实践,帮助开发者构建高可用、可扩展的分布式系统。
575 0
|
10月前
|
人工智能 数据可视化 JavaScript
颠覆开发效率!国内首个微服务编排框架Juggle开源啦!
Juggle是国内首个开源的微服务编排框架,专注于解决企业微服务进程中接口重复开发、系统对接复杂等问题。它提供零代码、低代码和AI增强功能,通过可视化拖拽快速组装简单API为复杂接口,支持多协议、多语言脚本和流程多版本管理。相比国外框架如Conductor,Juggle更贴合国内需求,具备高效开发、企业级可靠性及信创适配等优势,助力企业实现敏捷创新与数字化转型。
颠覆开发效率!国内首个微服务编排框架Juggle开源啦!
|
JSON Java API
利用Spring Cloud Gateway Predicate优化微服务路由策略
Spring Cloud Gateway 的路由配置中,`predicates`​(断言)用于定义哪些请求应该匹配特定的路由规则。 断言是Gateway在进行路由时,根据具体的请求信息如请求路径、请求方法、请求参数等进行匹配的规则。当一个请求的信息符合断言设置的条件时,Gateway就会将该请求路由到对应的服务上。
1305 69
利用Spring Cloud Gateway Predicate优化微服务路由策略