当前位置:网站首页 > 数据科学与大数据 > 正文

spring数据库密码加密解密(springcloud数据库密码加密)



Spring Cloud Config项目是一个解决分布式系统的配置管理方案。它包含了Client和Server两个部分,Server提供配置文件的存储以接口的形式将配置文件的内容提供出去,Client通过接口获取数据并依据此数据初始化自己的应用。目前SpringCloud Config的Server主要是通过Git方式做一个配置中心,然后每个服务从Server获取自身配置所需的参数。

实现基于config来管理项目的配置文件

pom.xml

<?xml version="1.0" encoding="UTF-8"?><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 会:

  1. 启动配置服务器:通过它,应用程序可以接收和处理配置请求,为其他应用提供配置数据。
  2. 支持多种存储方式:配置服务器可以从多种配置源(如 Git、文件系统、本地仓库或数据库等)加载配置文件,并根据请求路径或参数动态选择配置文件。
  3. 支持不同环境配置:它能根据客户端应用的 spring.profiles.active 等参数提供不同环境(如 devprod)的配置。

配合 @EnableConfigServer 注解的配置服务器可以通过 REST API 提供配置数据,客户端应用只需指定配置服务器的地址,即可从服务器拉取和加载所需的配置信息

package com.et;
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication@EnableConfigServerpublic class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); }}

服务端配置文件

  • spring.cloud.config.server:表示该服务将作为配置服务器,用于向其他应用提供集中化的配置管理。
  • native:指定配置服务器的模式为“本地模式”,即配置文件保存在应用内部的目录或类路径下。这样,应用可以从本地文件系统读取配置,而不是从外部 Git 仓库或其他远程配置源读取。
  • search-locations:定义本地配置文件的路径,这里设置为 classpath:/config-repo/crmclasspath:/config-repo/client,表示配置服务器会从应用程序的类路径中的这两个文件夹加载配置文件。
  • bootstrap: true:启用 bootstrap.yml 加载过程,确保配置加载的优先级较高,通常用于在应用启动时获取外部配置源的信息。
server: port: 8888
spring: profiles: active: native cloud: config: server: # git: # uri: https://github.com/chinoukin/SpringcloudConfig native: search-locations: classpath:/config-repo/crm,classpath:/config-repo/client bootstrap: true

pom.xml

<?xml version="1.0" encoding="UTF-8"?><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 */@RestControllerpublic class DemoController { @Value("${example.property}") private String exampleProperty;
@GetMapping("/property") public String getProperty() { return exampleProperty; }}

客户端启动类

Spring Cloud 使用 ConfigServicePropertySourceLocator 类来实现配置自动装载。这个类会在引导阶段根据 spring.cloud.config.uri 连接配置服务器,并获取指定的配置文件内容:

  1. 配置文件加载:客户端应用在启动时,会自动向配置服务器发送 HTTP 请求,获取以 application-nameprofile 为参数的配置文件(如 /client-app/dev)。
  2. 配置源添加ConfigServicePropertySourceLocator 会将从配置服务器获取的配置信息转换为 Spring 的 PropertySource,并添加到应用的 Environment 中,供应用程序使用。
package com.et;

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplicationpublic 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-client name: config-client profile: dev

以上只是一些关键代码,所有代码请参见下面代码仓库

  • https://github.com/Harries/springcloud-demo(spring cloud config)

  1. 启动server应用(http://127.0.0.1:8888/config-client-dev.yml)
  2. 启动客户端应用
  3. 访问http://127.0.0.1:8080/property
  4. 返回配置文件中配置值client-dev

  • https://docs.spring.io/spring-cloud-config/docs/current/reference/html/
  • https://www.liuhaihua.cn/archives/711631.html

到此这篇spring数据库密码加密解密(springcloud数据库密码加密)的文章就介绍到这了,更多相关内容请继续浏览下面的相关推荐文章,希望大家都能在编程的领域有一番成就!

版权声明


相关文章:

  • 自动驾驶数据集 yolo(自动驾驶数据集2024)2025-06-13 22:45:07
  • spss23.0数据分析教程(spss22数据分析教程)2025-06-13 22:45:07
  • 大数据培训哪些课程(大数据培训哪些课程好)2025-06-13 22:45:07
  • 学术数据库没有购买就不能检索的有(中国学术期刊全文数据库不提供的文献外表特征的途径有)2025-06-13 22:45:07
  • 数据中台建设方案规划(数据中台设计方案)2025-06-13 22:45:07
  • 3dtiles文件下载(3dtiles数据下载)2025-06-13 22:45:07
  • 大数据技术是学什么的以后干什么(大数据技术主要是学什么)2025-06-13 22:45:07
  • 数据库技术基础知识(数据库技术基础思维导图)2025-06-13 22:45:07
  • 电子发票在税控盘怎样打印数据(税控盘中电子发票该如何打印)2025-06-13 22:45:07
  • impdp 命令(impdp命令导入数据库)2025-06-13 22:45:07
  • 全屏图片