• Spring Cloud Config 分布式配置中心


    1、分布式配置中心应用场景

      往往,我们使用配置文件管理⼀些配置信息,比如application.yml

    单体应用架构:配置信息的管理、维护并不会显得特别麻烦,手动操作就可以,因为就一个工程;

    微服务架构:因为我们的分布式集群环境中可能有很多个微服务,我们不可能一个一个去修改配置然后重启生效,在一定场景下我们还需要在运行期间动态调整配置信息,比如:根据各个微服务的负载情况,动态调整数据源连接池大小,我们希望配置内容发生变化的时候,微服务可以自动更新。

    场景总结如下:

    1. 集中配置管理,一个微服务架构中可能有成百上千个微服务,所以集中配置管理是很重要的(一次修改、到处生效)
    2. 不同环境不同配置,比如数据源配置在不同环境(开发dev,测试test,⽣产prod)中是不同的
    3. 运行期间可动态调整。例如,可根据各个微服务的负载情况,动态调整数据源连接池大小等配置修改后可自动更新
    4. 如配置内容发生变化,微服务可以自动更新配置

    那么,我们就需要对配置文件进行集中式管理(相同配置),这也是分布式配置中心的作用。

    2、Spring Cloud Config

    2.1、Config简介

       Spring Cloud Config是一个分布式配置管理方案,包含了 Server端和 Client端两个部分。

    • Server 端:提供配置文件的存储、以接口的形式将配置文件的内容提供出去,通过使用@EnableConfigServer注解在 Spring boot 应用中非常简单的嵌⼊
    • Client 端:通过接口获取配置数据并初始化自己的应用 

    2.2、Config分布式配置应用

            说明:Config Server是集中式的配置服务,用于集中管理应用程序各个环境下的配置。 默认使用Git存储配置文件内容,也可以SVN。

            比如,我们要对“简历微服务”的application.yml进行管理(区分开发环境、测试环境、生产环境)

    • 登录Github,创建项目lagou-config-repo
    • 上传yml配置文件,命名规则如下:
      • {application}-{profile}.yml 或者 {application}-{profile}.properties
      • 其中,application为应用名称,profile指的是环境(用于区分开发环境,测试环境、生产环境等)
      • 示例:lagou-service-resume-dev.yml、lagou-service-resume-test.yml、lagouservice-resume-prod.yml

    2.3、构建Config Server统一配置中心

      新建SpringBoot工程,引入依赖坐标(需要注册自己到Eureka)

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <parent>
    6. <artifactId>lagou-parentartifactId>
    7. <groupId>com.lagou.edugroupId>
    8. <version>1.0-SNAPSHOTversion>
    9. parent>
    10. <modelVersion>4.0.0modelVersion>
    11. <artifactId>lagou-cloud-configserver-9006artifactId>
    12. <dependencies>
    13. <dependency>
    14. <groupId>org.springframework.cloudgroupId>
    15. <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
    16. dependency>
    17. <dependency>
    18. <groupId>org.springframework.cloudgroupId>
    19. <artifactId>spring-cloud-config-serverartifactId>
    20. dependency>
    21. dependencies>
    22. project>

      配置启动类,使用注解@EnableConfigServer开启配置中心服务器功能

    1. package com.lagou.edu;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    5. import org.springframework.cloud.config.server.EnableConfigServer;
    6. @SpringBootApplication
    7. @EnableDiscoveryClient
    8. @EnableConfigServer // 开启配置中心功能
    9. public class ConfigServerApplication9006 {
    10. public static void main(String[] args) {
    11. SpringApplication.run(ConfigServerApplication9006.class,args);
    12. }
    13. }

      application.yml配置

    1. server:
    2. port: 9006
    3. #注册到Eureka服务中心
    4. eureka:
    5. client:
    6. service-url:
    7. # 注册到集群,就把多个Eurekaserver地址使用逗号连接起来即可;注册到单实例(非集群模式),那就写一个就ok
    8. defaultZone: http://LagouCloudEurekaServerA:8761/eureka,http://LagouCloudEurekaServerB:8762/eureka
    9. instance:
    10. prefer-ip-address: true #服务实例中显示ip,而不是显示主机名(兼容老的eureka版本)
    11. # 实例名称: 192.168.1.103:lagou-service-resume:8080,我们可以自定义它
    12. instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
    13. spring:
    14. application:
    15. name: lagou-cloud-configserver
    16. # =================config核心配置==============
    17. cloud:
    18. config:
    19. server:
    20. git:
    21. uri: https://github.com/5173098004/lagou-config-repo.git #配置git服务地址
    22. username: 517309804@qq.com #配置git用户名
    23. password: yingdian12341 #配置git密码
    24. search-paths:
    25. - lagou-config-repo
    26. # 读取分支
    27. label: master
    28. # =================config核心配置==============
    29. #针对的被调用方微服务名称,不加就是全局生效
    30. #lagou-service-resume:
    31. # ribbon:
    32. # NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule #负载策略调整
    33. # springboot中暴露健康检查等断点接口
    34. management:
    35. endpoints:
    36. web:
    37. exposure:
    38. include: "*"
    39. # 暴露健康接口的细节
    40. endpoint:
    41. health:
    42. show-details: always

    测试访问:http://localhost:9006/master/lagou-service-resume-dev.yml,查看到配置文件内容

    2.4、构建Client客户端(在已有简历微服务基础上)

      已有工程中添加依赖坐标

    1. <dependency>
    2. <groupId>org.springframework.cloudgroupId>
    3. <artifactId>spring-cloud-config-clientartifactId>
    4. dependency>

      application.yml修改为bootstrap.yml配置文件

            bootstrap.yml是系统级别的,优先级比application.yml高,应用启动时会检查这个配置文件,在这个配置文件中指定配置中心的服务地址,会自动拉取所有应用配置并且启用。

    (主要是把与统⼀配置中心连接的配置信息放到bootstrap.yml)

      注意:需要统一读取的配置信息,从集中配置中心获取

      bootstrap.yml

    1. server:
    2. port: 8080
    3. spring:
    4. application:
    5. name: lagou-service-resume
    6. datasource:
    7. driver-class-name: com.mysql.jdbc.Driver
    8. url: jdbc:mysql://localhost:3306/lagou?useUnicode=true&characterEncoding=utf8
    9. username: root
    10. password: 123456
    11. jpa:
    12. database: MySQL
    13. show-sql: true
    14. hibernate:
    15. naming:
    16. physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl #避免将驼峰命名转换为下划线命名
    17. # ===========核心配置==========
    18. cloud:
    19. # config客户端配置,和ConfigServer通信,并告知ConfigServer希望获取的配置信息在哪个文件中
    20. config:
    21. name: lagou-service-resume #配置文件名称
    22. profile: dev #后缀名称
    23. label: master #分支名称
    24. uri: http://localhost:9006 #ConfigServer配置中心地址
    25. # ===========核心配置==========
    26. #注册到Eureka服务中心
    27. eureka:
    28. client:
    29. service-url:
    30. # 注册到集群,就把多个Eurekaserver地址使用逗号连接起来即可;注册到单实例(非集群模式),那就写一个就ok
    31. defaultZone: http://LagouCloudEurekaServerA:8761/eureka,http://LagouCloudEurekaServerB:8762/eureka
    32. instance:
    33. prefer-ip-address: true #服务实例中显示ip,而不是显示主机名(兼容老的eureka版本)
    34. # 实例名称: 192.168.1.103:lagou-service-resume:8080,我们可以自定义它
    35. instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
    36. # 自定义Eureka元数据
    37. metadata-map:
    38. cluster: cl1
    39. region: rn1
    40. management:
    41. endpoints:
    42. web:
    43. exposure:
    44. include: "*"

            这种配置完成后,当我们启动项目后,config配置会自动根据我们配置的url,借助于url会去github上,将我们所配置在github上的文件加载到本地配置yml文件中,我们可以定义一个类来尝试获取这个文件。

    # Github上文件内容如下

    mysql:

            url:  dev-http://localhost:3306/db600

    lagou:

            message:  hello lagou,600

    1. package com.lagou.edu.controller;
    2. import org.springframework.beans.factory.annotation.Value;
    3. import org.springframework.cloud.context.config.annotation.RefreshScope;
    4. import org.springframework.web.bind.annotation.GetMapping;
    5. import org.springframework.web.bind.annotation.PathVariable;
    6. import org.springframework.web.bind.annotation.RequestMapping;
    7. import org.springframework.web.bind.annotation.RestController;
    8. /**
    9. * 该类用于模拟,我们要使用共享的那些配置信息做一些事情
    10. */
    11. @RestController
    12. @RequestMapping("/config")
    13. @RefreshScope
    14. public class ConfigController {
    15. // 和取本地配置信息一样
    16. @Value("${lagou.message}")
    17. private String lagouMessage;
    18. @Value("${mysql.url}")
    19. private String mysqlUrl;
    20. // 内存级别的配置信息
    21. // 数据库,redis配置信息
    22. @GetMapping("/viewconfig")
    23. public String viewconfig() {
    24. return "lagouMessage==>" + lagouMessage + " mysqlUrl=>" + mysqlUrl;
    25. }
    26. }
  • 相关阅读:
    嵌入式开发笔记:STM32的外设GPIO知识学习
    Flutter系列文章-Flutter在实际业务中的应用
    8-6选择排序-简单选择排序
    【Python数据分析 - 6】:Numpy中的逻辑运算
    异步编程解决方案 Generator生成器函数、iterator迭代器、async/await、Promise
    基于Java+vue前后端分离大学学术交流论坛设计实现(源码+lw+部署文档+讲解等)
    Tuxera NTFS2023Mac读写ntfs磁盘工具
    LabVIEW如何使用热键去触发自定义的事件
    【Rust—LeetCode题解】1656. 设计有序流
    prompt提示词:影响力营销文案,让AI 帮你写营销文案
  • 原文地址:https://blog.csdn.net/weixin_52851967/article/details/126566465