3、配置中心

简介: spring cloud config可以实现微服务中的所有系统的配置文件的统一管理,而且还可以实现当配置文件发生变化的时候,系统会自动更新获取新的配置。

1、当一个系统中的配置文件发生改变的时候,经常的做法是重新启动该服务,才能使得新的配置文件生效。


spring cloud config可以实现微服务中的所有系统的配置文件的统一管理,而且还可以实现当配置文件发生变化的时候,系统会自动更新获取新的配置。

微信图片_20220430231029.png

 

将配置文件放入git或者svn等服务中,通过一个Config Server服务来获取git或者svn中的配置数据,二其他服务需要配置数据时在通过Config Client从Config Server获取。


2、在git仓库新建如下图目录


微信图片_20220430231032.png

 

具体内容查看:https://gitee.com/hjj520/spring-cloud-2.x/tree/master/config-repos


3、 新建maven项目sc-config-server,对应pom.xml


<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>spring-cloud</groupId>
   <artifactId>sc-config-server</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>
   <name>sc-config-server</name>
   <url>http://maven.apache.org</url>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.0.4.RELEASE</version>
   </parent>
   <dependencyManagement>
      <dependencies>
        <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-dependencies</artifactId>
           <version>Finchley.RELEASE</version>
           <type>pom</type>
        </dependency>
      </dependencies>
   </dependencyManagement>
   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <maven.compiler.source>1.8</maven.compiler.source>
      <maven.compiler.target>1.8</maven.compiler.target>
   </properties>
   <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
        <version>2.0.1.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        <version>2.0.1.RELEASE</version>
      </dependency>
   </dependencies>
</project>


4、 新建类ConfigServerApplication.java


package sc.config.server;
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 ConfigServerApplication {
public static void main(String[] args) {
       SpringApplication.run(ConfigServerApplication.class, args);
}
}


5、创建bootstrap.yml文件


#服务端口
server:
  port: 8100
#服务注册中心
eureka:
  client:
    registerWithEureka: true #是否将自己注册到Eureka服务中,默认为true
    fetchRegistry: true #是否从Eureka中获取注册信息,默认为true
    serviceUrl:
      defaultZone: http://localhost:5001/eureka/
  instance:
    prefer-ip-address: true #将自己的ip地址注册到Eureka服务中
    ipAddress: 127.0.0.1
spring:
  application:
    name: sc-config-server #服务名称
  cloud:
    config:
      label: master #配置文件所在的分支
      server:
        git:
          uri: https://gitee.com/hjj520/spring-cloud-2.x.git #服务的git仓库地址
          #git仓库的用户名
          #username: huangjinjin
          #git仓库的密码
          #password: ********
          search-paths: /config-repos/sc-consumer-config  #配置文件所在的目录


备注:search-paths可以使用占位符{application},不过需要注意的必须使用这样的方式:’{application}’ (单引号引起来),不然可能出现https://blog.csdn.net/weixin_35022258/article/details/79019033帖子说的问题,具体这个占位符以后会说到。


6、启动注册中心Eureka,然后在启动sc-config-server项目


微信图片_20220430231035.png

 

http请求地址和资源文件映射如下:


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


具体可以使用哪种http请求地址和资源文件映射可以在config server的日志可以看到


微信图片_20220430231038.png

 

7、验证获取仓库中的配置数据


http://127.0.0.1:8100/application/dev

微信图片_20220430231041.png

 

http://127.0.0.1:8100/application/prd


微信图片_20220430231044.png

 







相关文章
|
3天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8085 2
|
12天前
|
域名解析 人工智能 Linux
|
20天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
10405 13
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
6天前
|
人工智能 安全 网络安全
|
10天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1569 5
|
9天前
|
云安全 数据采集 人工智能
|
17天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2719 8
|
9天前
|
Web App开发 缓存 安全
巨魔商店 TrollStore 安装教程(2026):免电脑在线安装与全机型安装器矩阵_iOS巨魔安装
巨魔商店(TrollStore)是iOS 14–17.0(特定版本)的永久签名工具,无需越狱,利用CoreTrust漏洞实现IPA签名长期有效,彻底告别7天重签。支持免电脑在线安装(TrollHelperOTA),适配多机型/系统组合,开源免费,安全可控。(239字)
2517 0