spring-cloud入门之spring-cloud-config(配置中心)

yizhihongxing

下面是 “spring-cloud入门之spring-cloud-config(配置中心)” 的完整攻略。

简介

Spring Cloud Config 是一个分布式配置管理工具,它可以让您在不同的应用程序和服务之间共享和管理应用程序的配置。它可以轻松地管理不同环境下的配置(如开发、测试、生产环境)。

Spring Cloud Config 可以使用多种后端存储来存储配置,包括本地文件系统、Git 存储库等。

在本文中,我们将介绍如何在 Spring Boot 应用程序中使用 Spring Cloud Config 来集中管理配置。

准备工作

使用 Spring Cloud Config,需要准备如下环境:

  • JDK 1.8 或更高版本;
  • Maven 3.2 或更高版本;
  • Git;
  • 一个本地文件系统或一个远程 Git 存储库。

操作步骤

1. 创建一个 Spring Boot 应用程序

在开始之前,请确保已按照准备工作中的要求进行了配置。

使用 Spring Initializr 创建一个新的 Spring Boot 应用程序。项目依赖中添加 spring-cloud-config-server 和 spring-boot-starter-web 两个依赖项。

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

2. 添加配置

在 application.properties 文件中添加以下配置:

server.port=8888
spring.cloud.config.server.git.uri=<git-repo-url>
spring.cloud.config.server.git.username=<git-username>
spring.cloud.config.server.git.password=<git-password>
spring.cloud.config.server.git.search-paths=config-repo

其中,

  • server.port:配置服务器端口;
  • spring.cloud.config.server.git.uri:Git 存储库的 URL;
  • spring.cloud.config.server.git.username:Git 存储库的用户名;
  • spring.cloud.config.server.git.password:Git 存储库的密码;
  • spring.cloud.config.server.git.search-paths:在存储库中的搜索路径。

3. 创建 Git 存储库并添加配置文件

在 Git 存储库中创建一个名为 config-repo 的目录。在这个目录中添加一个名为 application.properties 的文件,并设置一些属性值:

message=Hello World!

4. 启动服务

在项目根目录下执行以下命令:

mvn spring-boot:run

5. 测试

打开 Web 浏览器,输入 http://localhost:8888/application/default。您将看到一个 JSON 字符串,其中包含从 Git 存储库中读取的属性值。

{
    "name": "application",
    "profiles": [
        "default"
    ],
    "label": null,
    "version": null,
    "state": null,
    "propertySources": [
        {
            "name": "<git-repo-url>/config-repo/application.properties",
            "source": {
                "message": "Hello World!"
            }
        }
    ]
}

您还可以使用 curl 命令从命令行检索配置:

curl http://localhost:8888/application/default

输出:

{
    "name" : "application",
    "profiles" : [ "default" ],
    "label" : null,
    "version" : "76ed25889e3b0cd23d3eba60a3f30907b53c4385",
    "state" : null,
    "propertySources" : [ {
        "name" : "https://github.com/baeldung/spring-cloud-config-repo/config-repo/application.properties",
        "source" : {
            "message" : "Hello World!"
        }
    } ]
}

示例说明

示例 1:多环境配置

可以使用 Spring Cloud Config 管理不同环境中的配置文件。例如,在 Git 存储库中添加一个名为 application-dev.properties 的文件并设置一些属性值:

message=Hello World! - Dev

然后,在命令行中使用以下命令启动应用程序:

mvn spring-boot:run -Dspring.profiles.active=dev

现在,您可以使用 curl 命令检索 dev 环境的配置:

curl http://localhost:8888/application/dev

输出:

{
    "name": "application",
    "profiles": [
        "dev",
        "default"
    ],
    "label": null,
    "version": "7d2336d2368477c7fd3a933df148e94d426c4bbb",
    "state": null,
    "propertySources": [
        {
            "name": "<git-repo-url>/config-repo/application-dev.properties",
            "source": {
                "message": "Hello World! - Dev"
            }
        },
        {
            "name": "<git-repo-url>/config-repo/application.properties",
            "source": {
                "message": "Hello World!"
            }
        }
    ]
}

示例 2:加密配置

Spring Cloud Config 还支持加密配置。例如,在 Git 存储库中添加一个名为 application.yml 的文件并设置一些属性值:

secret:
   password: '{cipher}AgDp7RDbFtXUbg5wvxiF+6xoq+aNeRaUd1HfB5nPlfo='

这里的 {cipher} 表示 password 属性是加密的。现在,我们需要一个密钥来解密配置。可以使用以下命令生成一个密钥:

keytool -genkeypair -alias mytestkey -keyalg RSA \
  -dname "CN=Web Server,OU=Unit,O=Organization,L=City,S=State,C=US" \
  -keypass changeme -keystore server.jks -storepass letmein

然后,我们需要在 bootstrap.yml 中添加以下配置:

encrypt:
  key-store:
    location: classpath:/server.jks
    password: letmein
    alias: mytestkey
    store-password: letmein

现在,可以使用 curl 命令检查加密的密码:

curl http://localhost:8888/application/default

输出:

{
    "name": "application",
    "profiles": [
        "default"
    ],
    "label": null,
    "version": "7d2336d2368477c7fd3a933df148e94d426c4bbb",
    "state": null,
    "propertySources": [
        {
            "name": "<git-repo-url>/config-repo/application.properties",
            "source": {
                "message": "Hello World!"
            }
        },
        {
            "name": "<git-repo-url>/config-repo/application.yml",
            "source": {
                "secret.password": "hello"
            }
        }
    ]
}

以上就是 “spring-cloud入门之spring-cloud-config(配置中心)” 的完整攻略,希望对您有帮助。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:spring-cloud入门之spring-cloud-config(配置中心) - Python技术站

(0)
上一篇 2023年6月25日
下一篇 2023年6月25日

相关文章

  • C++实现LeetCode(21.混合插入有序链表)

    C++实现LeetCode(21.混合插入有序链表) 题目描述 给你两个有序链表的头节点 l1 和 l2,请你将它们合并成一个新的有序链表,并返回新链表的头节点。 示例 1: 输入:l1 = [1,2,4], l2 = [1,3,4] 输出:[1,1,2,3,4,4] 示例 2: 输入:l1 = [], l2 = [] 输出:[] 题解 这道题的思路比较简单…

    other 2023年6月27日
    00
  • Android统一依赖管理的三种方式总结

    下面是将“Android统一依赖管理的三种方式总结”做完整讲解的攻略: Android统一依赖管理的三种方式总结 在Android开发中,依赖库是非常重要的,它们可以帮助我们更快地完成项目并提高代码的质量。但在Android项目中使用了越来越多的第三方类库,不同的模块和版本之间的依赖关系变得更加复杂,需要一个好的依赖管理工具才能够有效的管理这些依赖关系。这篇…

    other 2023年6月27日
    00
  • Java快速入门掌握类与对象及变量的使用

    Java快速入门掌握类与对象及变量的使用攻略 本攻略将帮助你快速入门Java编程语言中的类与对象以及变量的使用。以下是详细的步骤和示例说明。 步骤1:了解类与对象的概念 在Java中,类是一种定义对象的模板,而对象是类的实例。类定义了对象的属性和行为。下面是一个简单的类的示例: public class Person { String name; int a…

    other 2023年8月15日
    00
  • cmd findstr 字符串查找增强使用说明

    用 findstr 命令可以在文本文件中查找字符串的匹配情况。它是在 Windows 系统中常用的一个命令,并支持正则表达式的语法。本攻略将详细讲解 findstr 命令的使用方法。 命令语法 findstr 命令的基本语法如下: findstr [options] <string> [<filename>…] 其中,<st…

    other 2023年6月26日
    00
  • 搭建pikachu平台及暴力破解

    搭建Pikachu平台及暴力破解 Pikachu是一款基于Python的开源漏洞扫描器,它可以对各类Web应用程序进行漏洞扫描,包括SQL注入、XSS、漏洞探测等。 Pikachu平台可以让我们更好的测试和评估我们的Web应用程序的安全性。本文内容将介绍如何在Linux系统下搭建Pikachu平台以及如何使用暴力破解功能扫描漏洞。 步骤一:安装Python和…

    其他 2023年3月28日
    00
  • 字符串拼接的批处理

    下面是关于“字符串拼接的批处理”的完整攻略。 什么是字符串拼接的批处理? 字符串拼接的批处理是指将多个字符串连接成一个或多个长字符串的操作,该操作通常在Windows批处理或CMD(命令提示符)环境中使用。字符串拼接的批处理通常使用“set”命令与“+”运算符组合来实现。 字符串拼接的基本语法 下面是基本的字符串拼接语法: set string1=这是第一个…

    other 2023年6月20日
    00
  • Android Kotlin全面详细类使用语法学习指南

    Android Kotlin全面详细类使用语法学习指南 本攻略旨在帮助Kotlin初学者快速了解Kotlin中类的相关语法以及应用场景,让你能够轻松写出优雅、简洁、易读的Kotlin代码。 类的基本语法 Kotlin中,类被定义为一种特殊的函数。类名通常采用Pascal命名法,即首字母大写。类可以包含构造函数、属性、函数等内容。以下是一个示例: class …

    other 2023年6月27日
    00
  • 腾讯云服务器部署前后分离项目之前端部署

    下面我将详细讲解“腾讯云服务器部署前后分离项目之前端部署”的完整攻略,具体步骤如下: 准备工作 在腾讯云上购买一台云服务器,并开启相关服务。 安装Node.js和Git工具。 前端代码打包 安装前端依赖: bash npm install 执行前端代码打包命令: bash npm run build 执行完命令后,将会在项目根目录下生成一个dist文件夹,里…

    other 2023年6月27日
    00
合作推广
合作推广
分享本页
返回顶部