Spring Cloud Config项目是一个解决分布式系统的配置管理方案。它包含了Client和Server两个部分,Server提供配置文件的存储以接口的形式将配置文件的内容提供出去,Client通过接口获取数据并依据此数据初始化自己的应用。目前SpringCloud Config的Server主要是通过Git方式做一个配置中心,然后每个服务从Server获取自身配置所需的参数。
实现基于config来管理项目的配置文件
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"><parent><artifactId>spring-cloud-config</artifactId><groupId>com.et</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>config-server</artifactId><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency></dependencies></project>
server
@EnableConfigServer 注解的作用是启用 Spring Cloud Config Server 功能,使当前应用能够作为一个配置服务器来集中管理配置文件。使用 @EnableConfigServer 注解后,应用程序可以提供集中化的配置服务,允许客户端应用从该服务器拉取配置文件。 具体来说,@EnableConfigServer 会:
- 启动配置服务器:通过它,应用程序可以接收和处理配置请求,为其他应用提供配置数据。
- 支持多种存储方式:配置服务器可以从多种配置源(如 Git、文件系统、本地仓库或数据库等)加载配置文件,并根据请求路径或参数动态选择配置文件。
- 支持不同环境配置:它能根据客户端应用的
spring.profiles.active等参数提供不同环境(如dev、prod)的配置。
配合 @EnableConfigServer 注解的配置服务器可以通过 REST API 提供配置数据,客户端应用只需指定配置服务器的地址,即可从服务器拉取和加载所需的配置信息
package com.et;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;public class ConfigServerApplication {public static void main(String[] args) {SpringApplication.run(ConfigServerApplication.class, args);}}
服务端配置文件
spring.cloud.config.server:表示该服务将作为配置服务器,用于向其他应用提供集中化的配置管理。native:指定配置服务器的模式为“本地模式”,即配置文件保存在应用内部的目录或类路径下。这样,应用可以从本地文件系统读取配置,而不是从外部 Git 仓库或其他远程配置源读取。search-locations:定义本地配置文件的路径,这里设置为classpath:/config-repo/crm和classpath:/config-repo/client,表示配置服务器会从应用程序的类路径中的这两个文件夹加载配置文件。bootstrap: true:启用bootstrap.yml加载过程,确保配置加载的优先级较高,通常用于在应用启动时获取外部配置源的信息。
server:port: 8888spring:profiles:active: nativecloud:config:server:# git:# uri: https://github.com/chinoukin/SpringcloudConfignative:: classpath:/config-repo/crm,classpath:/config-repo/clientbootstrap: true
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"><parent><artifactId>spring-cloud-config</artifactId><groupId>com.et</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>config-client</artifactId><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId></dependency></dependencies></project>
controller
获取服务端托管的配置文件值
package com.et.controller;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;/* @author liuhaihua* @version 1.0* @ClassName DemoController* @Description todo* @date 2024/11/01/ 13:47*/public class DemoController {private String exampleProperty;public String getProperty() {return exampleProperty;}}
客户端启动类
Spring Cloud 使用 ConfigServicePropertySourceLocator 类来实现配置自动装载。这个类会在引导阶段根据 spring.cloud.config.uri 连接配置服务器,并获取指定的配置文件内容:
- 配置文件加载:客户端应用在启动时,会自动向配置服务器发送 HTTP 请求,获取以
application-name和profile为参数的配置文件(如/client-app/dev)。 - 配置源添加:
ConfigServicePropertySourceLocator会将从配置服务器获取的配置信息转换为 Spring 的PropertySource,并添加到应用的Environment中,供应用程序使用。
package com.et;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;public class ConfigClientApplication {public static void main(String[] args) {SpringApplication.run(ConfigClientApplication.class, args);}}
客户端配文件bootstrap.yml
spring.application.name:指定客户端的应用名称,这个名称用于从配置服务器上找到对应的配置文件。spring.cloud.config.uri:配置服务器的 URI 地址。客户端会从这个地址获取配置。
spring:cloud:config:uri: http://127.0.0.1:8888/#name: crm,config-clientname: config-clientprofile: dev
以上只是一些关键代码,所有代码请参见下面代码仓库
- https://github.com/Harries/springcloud-demo(spring cloud config)
- 启动server应用(http://127.0.0.1:8888/config-client-dev.yml)
- 启动客户端应用
- 访问http://127.0.0.1:8080/property
- 返回配置文件中配置值
client-dev
- https://docs.spring.io/spring-cloud-config/docs/current/reference/html/
- https://www.liuhaihua.cn/archives/711631.html
版权声明:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权、违法违规、事实不符,请将相关资料发送至xkadmin@xkablog.com进行投诉反馈,一经查实,立即处理!
转载请注明出处,原文链接:https://www.xkablog.com/sjkxydsj/50795.html