自学内容网 自学内容网

探索Spring Cloud Config:构建高可用的配置中心

认识Spring Cloud Config

概述:Spring Cloud Config为Spring应用提供了便捷的配置管理解决方案,并且可以与其他编程语言编写的应用程序协同工作。

Spring Cloud Config通过配置服务器(即Config Server)和配置客户端(即Config
Client)为分布式系统提供外部配置支持。

Config Server作为一个独立的微服务,负责从Git等存储库加载配置并向需要它的微服务公开这些配置信息。

该方案支持环境隔离功能,能够实现多环境的代码共享以及针对特定环境的配置管理需求。

所有环境配置数据由中央服务器统一维护,并通过Git进行版本控制。

为了保护敏感信息的安全,它提供了加密与解密机制,并可通过@EnableConfigServer注解轻松集成到Spring Boot应用中。

此外,这种集中管理方式确保了配置信息的一致性和安全性,同时简化了跨环境部署的复杂性。


Config Client -> Config Server ->git Repo

Config Server读取配置文件

从本地仓库读取配置文件

步骤:
1.搭建config-server

2.搭建config-client

3.测试运行

步骤1:

创建父工程config-1,在其中搭建Config Server

(1)创建config-server项目

导入依赖

使用Spring Initializr方式创建一个名称为config-server的Spring Boot项目,将Artifact命名为config-server,添加Config Server、Test依赖。其中Config Server依赖如下:

<dependency>

     <groupId>org.springframework.cloud</groupId>

     <artifactId>spring-cloud-config-server</artifactId>
     <version>2.0.0.RELEASE</version>
</dependency>

(2)在config-server中开启Config Server功能

在程序的启动类ConfigServerApplication添加@EnableConfigServer注解,开启Config Server功能。
在这里插入图片描述

(3)在config-server配置文件进行相关配置

在项目的配置文件application.yml中进行相关配置,包括指定服务名、端口号、本地读取配置以及读取路径。
在这里插入图片描述

(4)在config-server中创建配置文件

在项目的resource目录下建一个 bushuo 文件夹,用于存放本地配置文件。在 bushuo目录下,新建一个config-client-dev.yml文件,用作后续将要创建的config-client工程的dev开发环境的配置文件。在config-client-dev.yml配置文件中,配置端口号和自定义变量。
在这里插入图片描述

步骤2:搭建Config Client

(1)创建config-client项目,导入依赖

使用Spring Initializr方式创建一个名称为config-client的Spring Boot项目,将Artifact命名为config-client,添加Config、Web和Test的起步依赖。其中Config依赖如下:

 <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-config</artifactId>
            <version>2.0.0.RELEASE</version>

        </dependency>

在这里插入图片描述

(2)在config-client配置文件进行相关配置

在resources文件下新建bootstrap.yml文件。
在这里插入图片描述

(3)在config-client中添加方法

为了更直观的看到配置文件config-client-dev.yml被读取,创建controller包下的ConfigController类,添加一个hi()方法进行测试。
在这里插入图片描述

步骤3:测试运行

先启动config-server项目,再启动config-client项目,观察config-client控制台日志。

使用浏览器访问config-client的请求方法:http://localhost:8002/hi。

观察输出结果。

在这里插入图片描述

从Git仓库读取配置文件

步骤:

1.创建Git远程仓库并创建配置文件
2.修改Config Server的配置文件
3.测试运行

步骤1:创建Git远程仓库并创建配置文件

在远程Git仓库创建一个名称为hello的公开仓库,然后在公开库中创建一个和config-client-dev.yml相同的文件。

(1)百度搜索“码云”,打开Git官网并注册登录

gitee

(2)登录成功后,创建仓库

在这里插入图片描述
新建仓库内容如下:

在这里插入图片描述

(3)创建仓库成功后,在仓库中新建yml文件,输入配置内容提交,然后点击加号,新建文件夹

在这里插入图片描述
在这里插入图片描述
往下翻,点击提交

步骤2:修改Config Server的配置文件

在这里插入图片描述
点击“克隆”按钮复制uri地址,修改username与password的值为登录Git的用户名和密码。
在这里插入图片描述

修改Config Server的配置文件application.yml。配置服务名、端口号、远程Git仓库的地址、文件夹地址、用户名、密码、分支名等。
在这里插入图片描述

步骤3:测试运行

重启config-server和config-client,观察config-client控制台日志。

使用浏览器访问http://localhost:8002/hi。
在这里插入图片描述

bootstrap.yml与application.yml的区别

加载顺序:若application.yml 和bootstrap.yml 在同一目录下:bootstrap.yml 先加载 application.yml后加载
配置区别:bootstrap.yml 和 application.yml 都可以用来配置参数。
bootstrap.yml 用来程序引导时执行,应用于更加早期配置信息读取。可以理解成系统级别的一些参数配置,这些参数一般是不会变动的。一旦bootstrap.yml 被加载,则内容不会被覆盖。
application.yml 可以用来定义应用级别的, 应用程序特有配置信息,可以用来配置后续各个模块中需使用的公共参数等。
属性覆盖问题:application.yml 的内容标签与 bootstrap 的标签一致,application 也不会覆盖 bootstrap,而 application.yml 里面的内容可以动态替换


注意
git的配置文件与项目name一致
思考:

为新的config客户端config-client-another,在Git中添加配置文件,运行测试。

(1)创建config-client-another项目,导入依赖

其中Config依赖如下:

 <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-config</artifactId>
            <version>2.0.0.RELEASE</version>

        </dependency>

(2)在resources 新建bootstrap.yml文件
在这里插入图片描述

spring:
  application:
    name: config-client-another
  cloud:
    config:
      uri: http://localhost:8001
      fail-fast: true
  profiles:
    active: dev

(3)添加方法:创建controller包,configcontroller,添加方法hello1,进行测试

package com.bushuo.configclientanother.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 configcontroller {
    @Value( "${hello1}" )
    String hello1;
    @RequestMapping(value = "/hello1")
    public String hello1(){
        return hello1;
    }
}

在这里插入图片描述
(4)在git中在hello下创建config-client-another-dev.yml
在这里插入图片描述
(5)运行测试
在这里插入图片描述

搭建高可用Config Server

服务实例很多时,所有的服务实例需要同时从配置中心Config Server读取配置文件,这时可以考虑将配置中心Config Server做成一个集群化的微服务,从而达到高可用。将Config Server和Config Client注册在Eureka Server.

步骤:
1.创建Eureka Server

2.改造Config Server项目

3.改造Config Client项目

4.搭建Config Server集群

5.测试运行

步骤1:创建Eureka Server

步骤1:创建项目,引入依赖

1)
使用Spring Initializr方式创建一个名称为eureka-server的Spring Boot项目,将Artifact命名为eureka-server,在pom.xml文件中添加Eureka Server依赖。

方便测试效果,新建一个eureka-server项目作为Config Server的注册中心,将eureka-server端口号设置为7001。

<?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 https://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>2.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.bushuo</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-server</name>
    <description>Demo project for Spring Boot</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>

                spring-cloud-starter-netflix-eureka-server

            </artifactId>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <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>


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

</project>
与springboot一致 2.0.6.RELEASE 的springcloud的依赖 Finchley.SR2 ---

2):添加Eureka的相关配置

在全局配置文件application.yml中添加Eureka的相关配置信息。
在这里插入图片描述

3):在项目启动类添加@EnableEurekaServer注解

在项目启动类EurekaServerApplication上添加@EnableEurekaServer注解开启Eureka Server功能。
在这里插入图片描述
4)测试运行 http://localhost:7001
在这里插入图片描述
步骤2:改造Config Server项目

(1)Config Server作为服务器,需要在工程中的pom.xml配置文件中加入Eureka Client依赖。


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
    \
        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <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>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

(2)在项目的启动类ConfigServerApplication添加@EnableEurekaClient和 @EnableConfigServer注解,开启Eureka Server和Config Server功能。

在这里插入图片描述

(3)修改配置文件application.yml文件,为Config Server指定服务注册的地址等信息。

步骤3:改造Config Client项目

(1)在pom文件中加入Eureka Client起步依赖。在项目启动类上添加@EnableEurekaClient注解启动Eureka Client功能。
在这里插入图片描述
在配置文件bootstrap.yml加入指定服务注册地址等相关配置,配置如下:
在这里插入图片描述

步骤4:搭建Config Server集群

搭建高可用的Config Server服务只需要将Config Server多实例部署,使用Spring Initializr方式创建一个名称为config-server2的Config Server项目,设置端口号为8003,服务名也为config-server,其他配置信息和搭建过程与config-server项目一致。

步骤5:测试运行

使用浏览器访问http://localhost:7001。

在这里插入图片描述
访问config-client的请求方法:http://localhost:8002/hi。观察输入结果。

在这里插入图片描述

停掉config-server服务,再次访问请求,观察还能否正常访问?
在这里插入图片描述
在这里插入图片描述


原文地址:https://blog.csdn.net/m0_67187271/article/details/142860934

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!