• (二)springcloud实战之config配置中心


    本项目中:
    jdk版本:jdk1.8
    springboot版本:2.3.10.RELEASE
    springcloud版本:Hoxton.SR11

    一、项目结构

    父工程
    父工程下的config-center-8010(配置中心服务端)
    父工程下的student-service-8011(配置中心客户端)
    在这里插入图片描述

    二、config配置中心服务端

    2.1、引入服务端依赖

    
    
        
             org.springframework.cloud
             spring-cloud-config-server
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    没有写版本号是因为:父工程中声明了springcloud,springcloud中声明了spring-cloud-config-dependencies

    2.2、添加配置文件

    此处以本地存储配置文件为例:
    application.properties

    # 服务端口
    server.port=8010
    
    # 服务名称
    spring.application.name=config-center
    
    # 配置config文件位置,native表示本地存储配置文件(还有svn,git方式等)
    spring.profiles.active=native
    spring.cloud.config.server.native.search-locations=classpath:/myconfig
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    图解:
    在这里插入图片描述

    2.3、启动类添加注解

    @EnableConfigServer

    在这里插入图片描述

    到这里,配置中心服务端就搭建好了!

    三、客户端

    3.1、配置中心客户端添加依赖

        
        
        
            org.springframework.cloud
            spring-cloud-starter-config
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3.2、bootstrap.yml配置文件

    在resource文件夹下新建bootstrap.yml配置文件:

    spring:
      application:
        name: student-service
      profiles:
        active: tzq
    
      # 配置中心存放配置文件格式:${application.name}-${profiles.active}.yml
      # 例如student-service-tzq.yml、student-service-tzq.properties
      # 通过上述两个配置去配置中心读取对应的配置文件
      cloud:
        config:
          # uri 配置中心服务地址
          uri: http://localhost:8010
          fail-fast: true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述

    这里为什么用bootstrap.yml来添加配置,而不是application.properties。
    因为:bootstrap.yml比application.yml的优先级要高,项目启动的时候就会执行,适合配置中心客户端使用,项目启动的时候去配置中心拉取配置信息。

    bootstrap.yml和application.yml的优先级(加载顺序)
    • bootstrap 由spring父上下文加载,比application配置文件优先加载,而application配置文件由子上下文加载
    • bootstrap加载的配置信息不能被application的相同配置覆盖

    3.3、在配置中心服务规定的位置新建所需配置文件

    在resource文件夹下新建文件夹myconfig,在myconfig文件夹下新建:student-service-tzq.properties文件
    在这里插入图片描述
    student-service-tzq.properties文件:

    server.port=8011
    
    # 服务名称
    spring.application.name=student-service
    
    # mysql数据库连接
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/tzq?serverTimezone=GMT%2B8
    spring.datasource.username=root
    spring.datasource.password=Admin@123
    
    # 配置actuator的访问端口,如果不配置则默认跟该服务运行端口一样
    management.server.port=7802
    # 配置actuator的info信息,info.后面可以自己随便定义
    info.name=${spring.application.name}
    info.tzq=tzq
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    到这里配置中心客户端也完成!
    先启动配置中心服务端,再启动客户服务。
    在这里插入图片描述
    控制台显示:Fetching config from server at : http://localhost:8010
    配置中心成功!!!

  • 相关阅读:
    8 路数字量输入兼容干接点、湿节点多功能RTU
    【.NET深呼吸】用代码写WPF控件模板
    面向对象编程原则(09)——迪米特法则
    【设计模式】第3讲--工厂方法模式
    Mybatis改成Mybatis-plus需要更改地方
    LeetCode 2511 最多可以摧毁的敌人城堡数目
    10.Linear Map transformation rules
    论文《面向大规模日志数据分析的自动化日志解析》翻译
    SpringBoot-Shiro安全权限框架
    SpringBoot整合Quartz示例
  • 原文地址:https://blog.csdn.net/m0_67391683/article/details/126620741