服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - SpringCloud高可用配置中心Config详解

SpringCloud高可用配置中心Config详解

2022-11-24 14:33m0_67401606 Java教程

Spring Cloud Config 是一个解决分布式系统的配置管理方案,它包含了 server 和 client 两个部分,这篇文章主要介绍了SpringCloud之配置中心Config(高可用),需要的朋友可以参考下

前言

SpringCloud 是微服务中的翘楚,最佳的落地方案。

Spring Cloud Config 是一个解决分布式系统的配置管理方案,它包含了 server 和 client 两个部分。

server 用来获取远程的配置信息(默认为 Git 仓库),并且以接口的形式提供出去;

client 根据 server 提供的接口读取配置文件,以便于初始化自己的应用。

如果配置中心出现了问题,将导致灾难性的后果,因此在生产环境下配置中心都会做集群,来保证高可用。

此处配置高可用实际就是把多个配置中心(指定同一个 Git 远程仓库)注册到注册中心。

源码

GitHub地址:https://github.com/intomylife/SpringCloud

环境

  • JDK 1.8.0 +
  • Maven 3.0 +
  • SpringBoot 2.0.3
  • SpringCloud Finchley.RELEASE

开发工具

IntelliJ IDEA

正文

commons 工程

commons 工程 - POM 文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?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.zwc</groupId>
    <artifactId>springcloud-config-eureka-commons</artifactId>
    <version>1.0</version>
    <!-- 工程名称和描述 -->
    <name>springcloud-config-eureka-commons</name>
    <description>公用工程</description>
    <!-- 打包方式 -->
    <packaging>jar</packaging>
    <!-- 在 properties下声明相应的版本信息,然后在dependency下引用的时候用 ${} 就可以引入该版本jar包了 -->
    <properties>
        <!-- 编码 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- jdk -->
        <java.version>1.8</java.version>
        <!-- SpringBoot -->
        <platform-bom.version>Cairo-SR3</platform-bom.version>
        <!-- SpringCloud -->
        <spring-cloud-dependencies.version>Finchley.RELEASE</spring-cloud-dependencies.version>
    </properties>
    <!-- 加入依赖 -->
    <dependencies>
    </dependencies>
    <!-- 依赖 jar 包版本管理的管理器 -->
    <!-- 如果 dependencies 里的 dependency 自己没有声明 version 元素,那么 maven 就此处来找版本声明。 -->
    <!-- 如果有,就会继承它;如果没有就会报错,告诉你没有版本信息 -->
    <!-- 优先级:如果 dependencies 里的 dependency 已经声明了版本信息,就不会生效此处的版本信息了 -->
    <dependencyManagement>
        <dependencies>
            <!-- SpringBoot -->
            <dependency>
                <groupId>io.spring.platform</groupId>
                <artifactId>platform-bom</artifactId>
                <version>${platform-bom.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- SpringCloud -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud-dependencies.version}</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>

配置一些共用依赖

commons 工程 - 项目结构

SpringCloud高可用配置中心Config详解

配置文件

创建一个 Git 库,里面存放配置文件,文件夹名称为:config-repo;这里模拟不同的环境,所以分别构建了

dev(开发)、uat(测试)以及online(线上)三种不同的配置文件;此文件夹存在此项目的根目录中

?
1
2
3
4
5
6
7
- springcloud-config-eureka
? - config-repo
? ? -?system-dev.properties
? ? -?system-online.properties
? ? -?system-uat.properties
  + springcloud-config-eureka-commons
  + springcloud-config-eureka-service

service 工程

① 此工程下有四个模块:一个注册中心,二个 server 以及一个 client

② 二个 server 除端口不一致以外,其他代码基本一致

registry-service(注册中心)

registry-service - POM 文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?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>com.zwc</groupId>
        <artifactId>springcloud-config-eureka-service</artifactId>
        <version>1.0</version>
    </parent>
    <!-- 三坐标 -->
    <groupId>com.zwc</groupId>
    <artifactId>springcloud-config-eureka-registry-service</artifactId>
    <version>1.0</version>
    <!-- 工程名称描述 -->
    <name>springcloud-config-eureka-registry-service</name>
    <description>注册中心</description>
    <!-- 打包方式 -->
    <packaging>jar</packaging>
    <!-- 在 properties下声明相应的版本信息,然后在dependency下引用的时候用 ${} 就可以引入该版本jar包了 -->
    <properties>
    </properties>
    <!-- 加入依赖 -->
    <dependencies>
        <!-- commons工程 依赖 -->
        <dependency>
            <groupId>com.zwc</groupId>
            <artifactId>springcloud-config-eureka-commons</artifactId>
            <version>1.0</version>
        </dependency>
        <!-- 服务注册中心 -->
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependencies>
    <!-- 插件依赖 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

主要加入spring-cloud-starter-netflix-eureka-server依赖

registry-service - application.yml配置文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 端口
server:
  port: 8761
# 应用名称
spring:
  application:
    name: eureka-server
eureka:
  instance:
    # 使用 ip 代替实例名
    prefer-ip-address: true
    # 实例的主机名
    hostname: ${spring.cloud.client.ip-address}
    # 实例的 ID 规则
    instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
  client:
    # 是否向注册中心注册自己
    registerWithEureka: false
    # 是否向注册中心获取注册信息
    fetchRegistry: false
    serviceUrl:
      # 注册中心地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

这里使用了默认的 8761 端口,当然也可以更改,不过在发现调用服务端的注册中心地址端口要与它一致

registry-service - 启动类

?
1
2
3
4
5
6
7
8
9
10
11
package com.zwc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class SpringcloudConfigEurekaRegistryServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringcloudConfigEurekaRegistryServiceApplication.class, args);
    }
}

在启动类中添加 @EnableEurekaServer 注解表示此工程是注册中心

registry-server - 启动项目

1. 项目启动成功后访问http://localhost:8761/即可看到eureka-server 主页面

SpringCloud高可用配置中心Config详解

server(获取远程的配置信息)

server- POM 文件

注:一共有两个 server,但是除端口以外的代码基本上一致,所有这里就列举其中一个

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?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>com.zwc</groupId>
        <artifactId>springcloud-config-eureka-service</artifactId>
        <version>1.0</version>
    </parent>
    <!-- 三坐标 -->
    <groupId>com.zwc</groupId>
    <artifactId>springcloud-config-eureka-serverfirst-service</artifactId>
    <version>1.0</version>
    <!-- 工程名称描述 -->
    <name>springcloud-config-eureka-serverfirst-service</name>
    <description>config server</description>
    <!-- 打包方式 -->
    <packaging>jar</packaging>
    <!-- 在 properties下声明相应的版本信息,然后在dependency下引用的时候用 ${} 就可以引入该版本jar包了 -->
    <properties>
    </properties>
    <!-- 加入依赖 -->
    <dependencies>
        <!-- commons工程 依赖 -->
        <dependency>
            <groupId>com.zwc</groupId>
            <artifactId>springcloud-config-eureka-commons</artifactId>
            <version>1.0</version>
        </dependency>
        <!-- config server 依赖 -->
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        <!-- 提供者消费者 -->
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependencies>
    <!-- 插件依赖 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
  • 加入spring-cloud-config-server依赖:config server
  • 加入spring-cloud-starter-netflix-eureka-client依赖:提供和注册服务

server- application.yml配置文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 端口
server:
  port: 8000
 
spring:
  application:
    # 应用名称
    name: config-eureka-server
  cloud:
    config:
      server:
        git:
          # 仓库地址
          uri: https://github.com/intomylife/SpringCloud
          # 对应 {label} 部分,即 Git 的分支
          label: master
          # 仓库文件夹名称,多个以逗号分隔
          search-paths: springcloud-config-eureka/config-repo
          # git 仓库用户名(公开库可以不用填写)
          username:
          # git 仓库密码(公开库可以不用填写)
          password:
eureka:
  instance:
    # 使用 ip 代替实例名
    prefer-ip-address: true
    # 实例的主机名
    hostname: ${spring.cloud.client.ip-address}
    # 实例的 ID 规则
    instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
  client:
    serviceUrl:
      # 注册中心地址
      defaultZone: http://${eureka.instance.hostname}:8761/eureka/
  • 注意这里 uri 只能写到仓库名
  • 如果配置文件在仓库中多层目录下,那么search-paths 就从根目录开始写起
  • 注意此处配置注册中心地址的端口为 8761 也就是上面注册中心工程配置的端口
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.zwc;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class SpringcloudConfigEurekaServerfirstServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringcloudConfigEurekaServerfirstServiceApplication.class, args);
    }
}
  • 添加@EnableConfigServer 注解表示开启配置中心
  • 添加 @EnableEurekaClient 注解表示此工程可以向注册中心提供服务

server- 启动项目

1.项目启动成功后访问:http://localhost:8761/(注册中心)可以看到服务已经被注册进来了

SpringCloud高可用配置中心Config详解

2. 访问地址:http://localhost:8000/config-repo/dev(获取完整配置信息)

3.输出内容:

{
    "name":"config-repo",
    "profiles":[
        "dev"
    ],
    "label":null,
    "version":"0d6480b5ba6f7f2be87b3130de8163dcb0af9f5f",
    "state":null,
    "propertySources":[

    ]
}

4. 访问地址:http://localhost:8000/system/dev(获取完整配置信息)

5. 输出内容:

{
    "name":"system",
    "profiles":[
        "dev"
    ],
    "label":null,
    "version":"0d6480b5ba6f7f2be87b3130de8163dcb0af9f5f",
    "state":null,
    "propertySources":[
        {
            "name":"https://github.com/intomylife/SpringCloud/springcloud-config-eureka/config-repo/system-dev.properties",
            "source":{
                "hello":"I'm dev."
            }
        }
    ]
}

6. 访问地址:http://localhost:8000/system-dev.properties(获取指定配置文件内容)

7. 输出内容:‘hello: I’m dev.’

8. 启动另一个 server(springcloud-config-eureka-serversecond-service)

9.项目启动成功后访问:http://localhost:8761/(注册中心)可以看到服务已经被注册进来了

SpringCloud高可用配置中心Config详解

10.访问地址:http://localhost:8001/config-repo/uat(获取完整配置信息)

11. 输出内容:

{
    "name":"config-repo",
    "profiles":[
        "uat"
    ],
    "label":null,
    "version":"0d6480b5ba6f7f2be87b3130de8163dcb0af9f5f",
    "state":null,
    "propertySources":[

    ]
}

12.访问地址:http://localhost:8001/system/uat(获取完整配置信息)

13.输出内容:

{
    "name":"system",
    "profiles":[
        "uat"
    ],
    "label":null,
    "version":"0d6480b5ba6f7f2be87b3130de8163dcb0af9f5f",
    "state":null,
    "propertySources":[
        {
            "name":"https://github.com/intomylife/SpringCloud/springcloud-config-eureka/config-repo/system-uat.properties",
            "source":{
                "hello":"I'm uat."
            }
        }
    ]
}

14.访问地址:http://localhost:8001/system-uat.properties(获取指定配置文件内容)

15. 输出内容:‘hello: I’m uat.’

16. 证明两个 server 都已经成功从远程仓库中获取到了配置信息

注:接口访问有如下规则:

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

client(读取注册中心的server 中的配置信息)

client- POM 文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?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>com.zwc</groupId>
        <artifactId>springcloud-config-eureka-service</artifactId>
        <version>1.0</version>
    </parent>
    <!-- 三坐标 -->
    <groupId>com.zwc</groupId>
    <artifactId>springcloud-config-eureka-client-service</artifactId>
    <version>1.0</version>
    <!-- 工程名称描述 -->
    <name>springcloud-config-eureka-client-service</name>
    <description>config client</description>
    <!-- 打包方式 -->
    <packaging>jar</packaging>
    <!-- 在 properties下声明相应的版本信息,然后在dependency下引用的时候用 ${} 就可以引入该版本jar包了 -->
    <properties>
    </properties>
    <!-- 加入依赖 -->
    <dependencies>
        <!-- commons工程 依赖 -->
        <dependency>
            <groupId>com.zwc</groupId>
            <artifactId>springcloud-config-eureka-commons</artifactId>
            <version>1.0</version>
        </dependency>
        <!-- config 依赖 -->
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        <!-- 提供者消费者 -->
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependencies>
    <!-- 插件依赖 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
  • 加入spring-cloud-starter-config依赖:config client
  • 加入spring-cloud-starter-netflix-eureka-client依赖:提供和注册服务

client- application.yml配置文件

?
1
2
3
4
5
6
7
8
# 端口
server:
  port: 8002
 
spring:
  application:
    # 应用名称
    name: config-eureka-client

client- bootstrap.yml 配置文件(注意)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
spring:
  cloud:
    config:
      # 对应 {label} 部分,即 Git 的分支
      label: master
      # 对应 {application} 部分
      name: system
      # 对应 {profile} 部分
      profile: dev
      discovery:
        # 开启 Config 服务发现与注册
        enabled: true
        # 指定 server
        service-id: config-eureka-server
eureka:
  instance:
    # 使用 ip 代替实例名
    prefer-ip-address: true
    # 实例的主机名
    hostname: ${spring.cloud.client.ip-address}
    # 实例的 ID 规则
    instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
  client:
    serviceUrl:
      # 注册中心地址
      defaultZone: http://${eureka.instance.hostname}:8761/eureka/
  • bootstrap.yml 配置文件加载先于application.yml 配置文件
  • 与 Spring Cloud Config 相关的属性必须配置在bootstrap.yml 中
  • 这里就不是直接指定 server 地址了,而是 server 的应用名(spring.application.name)
  • 从注册中心的 server 中读取 dev(开发)环境的配置文件信息
  • 注意此处配置注册中心地址的端口为 8761 也就是上面注册中心工程配置的端口

client- controller 前端控制器

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.zwc.client.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * @ClassName HelloController
 * @Desc TODO   读取 git 配置文件
 * @Date 2019/6/2 16:54
 * @Version 1.0
 */
@RestController
public class HelloController {
    @Value("${hello}")
    private String hello;
    /*
     * @ClassName HelloController
     * @Desc TODO   读取 git 配置文件
     * @Date 2019/6/2 16:56
     * @Version 1.0
     */
    @RequestMapping("/hello")
    public String hello() {
        return this.hello;
    }
}

直接使用@Value 注解就可以从注册中心的server 中读取到对应的配置信息

client- 启动类

?
1
2
3
4
5
6
7
8
9
10
11
package com.zwc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class SpringcloudConfigEurekaClientServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringcloudConfigEurekaClientServiceApplication.class, args);
    }
}

添加 @EnableEurekaClient 注解表示此工程可以向注册中心提供服务

client- 启动项目

1.项目启动成功后访问:http://localhost:8761/(注册中心)可以看到服务已经被注册进来了

SpringCloud高可用配置中心Config详解

2.访问地址:http://localhost:8002/hello

3. 输出内容:‘I’m dev.’

4. 证明 client 已经成功从注册中心的 server 中读取到了配置信息

5. 此时当 client 已经启动完毕后,配置文件就已经全部读取到了,所以即使停止其中一个 server 或者停止

全部 server,client 依然可以读取到配置文件。此处高可用应该是指在 client 启动时能保证有 server 可用

service 工程 -项目结构

SpringCloud高可用配置中心Config详解

把多工程项目使用 IntelliJ IDEA 打开

  • 把项目从 GitHub 中下载到你的本地
  • 打开IntelliJ IDEA
  • 点击 File -> Open
  • 打开你下载到本地的项目目录
  • springcloud-config-eureka ->springcloud-config-eureka-service(选择打开此工程)
  • 打开 service 工程后
  • 再次点击 File -> Project Structrue
  • 选择 Modules,点击 ‘+’ 符号
  • 点击 ImportModule
  • 还是打开你下载到本地的项目目录
  • springcloud-config-eureka ->springcloud-config-eureka-commons -> pom.xml
  • 点击 OK
  • 点击 Next,Finish
  • 点击 Apply,OK

希望能够帮助到你

到此这篇关于SpringCloud之配置中心Config(高可用)的文章就介绍到这了,更多相关SpringCloud配置中心Config内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/m0_67401606/article/details/124442420

延伸 · 阅读

精彩推荐