当前位置: 首页 > article >正文

配置管理 —— SpringCloud Config

概述

Config 为分布式系统提供统一的配置管理工具,应用程序在使用过程中可以像使用本地配置一样方便地添加、访问、修改配置中心的配置

Config 支持将配置存储在配置中心的本地服务器或 Git 仓库,通常将配置文件集中放置在一个 Git 仓库,然后通过配置中心(Config Server)来管理所有的配置文件。当某个服务实例需要添加或更新配置时,只需将该服务实例的本地配置文件进行修改,然后推送到 Git 仓库,其他服务实例通过配置中心(Config Server)就可以从 Git 仓库获取最新的配置信息。对于配置中心(Config Server)来说,每个服务实例都相当于客户端(Config Client)。为了保证系统的稳定,配置中心(Config Server)可以进行多副本集群部署


Config Server 的定义及应用

假设 Git 仓库的地址为 https://github.com/xxx/xxxxx,在该仓库的 SpringCloudConfig 路径下有一个配置文件 application.properies

创建一个 Config Server 分为以下三步:

在 pom.xml 文件中加入依赖

<dependencies>
  <!-- starter-config 依赖 -->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
  </dependency>
  <!-- spring-boot-starter-web 表示项目为 Web 工程 -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactld>
  </dependency>
  <!-- 系统运维监控组件 -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
  </dependeney>
  <!-- config-server 依赖 -->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
  </dependeney>
</dependencies>

添加 @EnableConfigServer

@SpringBootApplication
@EnableConfigServer
public class BootApplication {
  public static void main(String[] args) {
    SpringApplication.run(BootApplication.class, args) ;
  }
}

配置 application.properties 文件,设置 Git 仓库的基本信息

#设置配置中心的端口号
server.port=9000
#设置配置中心的名称
spring.cloud.config.server.default-application-name=config-server
#设置Git仓厍的地址
spring.cloud.config.server.git.uri=https://github.com/xxx/xxxxx
#设置仓库的路径,配置文件的路径为https://github.com/xxx/xxxxx/SpringCloudConfig
spring.cloud.config.server.git.search-paths=SpringCloudConfig
#设置仓库的分支
spring.cloud.config.label=master
#访问Git仓库的用户名
spring.cloud.config.server.git.username=username
#访间Git仓库的用户密码,如果Git仓库为公开仓库,则可以不填写用户名和密码
spring.cloud.config.server.git.password=password

Config Client 的定义及应用

配置中心的使用分为以下三步:

在 pom.xml 文件中加入依赖

<dependencies>
  <!-- starter-config 依赖 -->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
  </dependency>
  <!-- spring-boot-starter-web 表示项目为 Web 工程 -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactld>
  </dependency>
  <!-- 系统运维监控组件 -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
  </dependeney>
</dependencies>

配置 bootstrap.properties 文件,设置应用程序从哪个服务获取配置信息,并且可以通过 spring.cloud.config.profile 指定运行环境

server.port=9001
spring.application.name=config-client
#远程仓库的分支
spring.cloud.config.label=master
#运行环境
spring.cloud.config.profile=dev
#设置服务中心的地址
spring.cloud.config.uri=http://localhost:9000/

使用配置信息

//将Key为spring.datasource.url的值映射到springDatasourceURL变量上
//其中spring.datasource.url为Git配置文件中的Key
@Value("${spring.datasource.url}")
String springDatasourceURL;

http://www.kler.cn/news/289651.html

相关文章:

  • CSS - 搜索框小动效
  • 重头开始嵌入式第三十二天(TCP多客户端模型)
  • 文件包含PHP伪协议利用方法
  • SEO外链自动发布外链工具网站源码
  • 深度学习应用 - 语音识别篇
  • 基于web知识库管理系统设计与实现
  • 通过EasyExcel设置自定义表头及设置特定单元格样式、颜色
  • C++:关于反向迭代器的学习分享
  • 缓存类型以及读写策略
  • RocketMQ高级特性三-消费者分类
  • java 常用并发队列- ArrayBlockingQueue
  • malab 将数据导入到excell文件代码的几种方法
  • LeetCode hot100刷题记录
  • LACP链路聚合
  • Android 9.0 SystemUI状态栏/快捷设置介绍
  • 【网络原理】Udp 的报文结构,保姆式教学,快速入门
  • 计算机网络 第2章 物理层
  • 多个Node.js版本之间切换
  • css spacing设置间距
  • redis缓存和数据库通过延迟双删除实现数据一致性
  • Ghidra逆向工具之旅与二进制代码分析【4】
  • 通过设置JVM参数来启用GC(垃圾回收)日志
  • Super Image 2.1.0 图像处理软件,修复老照片、无损放大、智能修复,本地处理保护隐私
  • 如何选择SSD
  • java实现ocr功能(Tesseract OCR)
  • 数据库中LIKE 和 NOT LIKE的用法辨析
  • 嵌入式学习(数据结构:链表)
  • Apache Storm:入门了解
  • 图片去噪及边缘检测
  • Java设计模式——工厂模式