SpringCloud——(2)Config

SpringCloud统一配置中心

【一】前言

分布式系统中,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件springCloud Config ,它支持从远程Git仓库中读取配置文件并存放到本地Git仓库。接下来我们来看一下服务端和客户端分别应该如何配置。

【二】统一配置中心概述

统一配置中心的架构图:
在这里插入图片描述


如果不采用统一配置中心会发生的问题:

  1. 件分散在各个项目里,不方便维护
  2. 配置内容安全与权限,实际开发中,开发人员是不知道线上环境的配置的
  3. 更新配置后,项目需要重启

【四】Config项目搭建

IDEA构建项目:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
构建成功以后,会是一下的结构目录:
在这里插入图片描述

【五】Config的Maven依赖

下面来说下添加的依赖配置:

  • eureka-client依赖,统一配置中心也是需要注册到注册中心
<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
  • config-server统一配置的依赖
<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-config-server</artifactId>
</dependency>
  • bus-amqp和amqp和我们的RabbitMQ实现配置中心的动态配置刷新
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
            <version>2.1.1.RELEASE</version>
</dependency>

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

这里是全部maven的配置:

<?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.2.0.M6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.feng</groupId>
    <artifactId>config</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.M2</spring-cloud.version>
        <fastjson.version>1.2.58</fastjson.version>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

        <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-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.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>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </pluginRepository>
    </pluginRepositories>

</project>

【六】Config的yml配置

这个地方需要注意的几个点:

  1. 需要在github上创建配置项目,并在配置项目中添加配置文件
  2. 需要配置rabbitmq的信息,以实现配置更改,配置中心发出消息,使得其他的服务实现动态的刷新
  3. 需要开放请求接口/actuator/bus-refresh
  4. 可定是要去启动上一节讲到的eureka(8761)的服务的才行
spring:
  application:
    name: config  #应用名称
  cloud:
    config:
      server:
        git:
          #从远端github上拉取配置到本地仓库中
          uri: https://github.com/fengsri/springcloud-config.git
          username: [email protected]
          password: xxxxxxxxxx
  #配置rabbitmq的信息
  rabbitmq:
    host: 47.100.xxxx
    port: 5672
    username: xxxx
    password: xxxx
#把配置中心注册到注册中心上
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

server:
  port: 8089

# 开放接口,好实现动态刷新
management:
  endpoints:
    web:
      exposure:
        include: "*"

这是在github上创建的配置文件项目:
在这里插入图片描述
在我们的rabbitmq的管理界面是可以看到连接的队列的:
在这里插入图片描述
ps: 如果有小伙伴没有安装rabbitmq的可以参考以下的博客:https://blog.csdn.net/wenge1477/article/details/100072974

如果以上的操作你都正确了,那么就可以通过浏览器访问到我们github的配置了:
在这里插入图片描述

【七】动态刷新配置

这个时候使用动态刷新的话就需要启动另外的order服务了,因为我们的配置中心从github是拉取配置到本地的仓库,并注册到eureka注册中心上,其他的服务从配置中心拉取配置进行使用。


  • 启动order服务
    在这里插入图片描述
    yml的配置
    在这里插入图片描述
    在这里插入图片描述
    在github上order的配置如下:
    在这里插入图片描述
  • 启动rabbitmq
    这个时候我们可以看到rabbitmq的管理界面上有两个队列,一个配置中心的,一个order服务的
    在这里插入图片描述
  • 请求接口
    1、这个时候修改github上面的order的配置文件
    在这里插入图片描述
    2、通过配置中心访问配置
    在这里插入图片描述
    3、发请求
    使用postman 请求接口,让config把消息发送到rabbitmq
    在这里插入图片描述
    可以看到消息被消费了
    在这里插入图片描述
    4、通过order服务访问配置
    这个时候发现order可以正确地访问到我们直接修改的配置,这个绝对是微服务架构应该具有的特性
    在这里插入图片描述

ps: 终极boss,可以在我们的github上设置webhook进行请求,当我们修改了配置,会制动触发我们的webhook发送请求,配置中心接受到请求,会把消息发送到mq上,其他的服务更新配置。这样就做到了动态刷新配置。
在这里插入图片描述

【八】Config的高可用

这个地方只演示在本地启动多个config实现高可用
在这里插入图片描述
在这里插入图片描述

【九】链接

https://www.jianshu.com/p/41c2cd7c498a
https://blog.51cto.com/zero01/2171735
https://blog.csdn.net/weixin_40470497/article/details/83780709
https://www.cnblogs.com/lfalex0831/p/9206605.html

发布了130 篇原创文章 · 获赞 88 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/wenge1477/article/details/102505658