Spring Cloud与微服务之配置文件中心Spring Cloud Config

本文介绍了Spring Cloud Config作为配置中心的使用,包括Config Server和Config Client的配置,以及如何与Git集成。此外,还讨论了Spring Could Bus在微服务架构中的作用,特别是如何利用它实现配置的动态更新,并提到了RabbitMQ的配置和使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring Cloud Config简介

  Spring Cloud Config为分布式系统外部化配置提供了服务器端和客户端的支持,它包括Config Service和Config Client两部分。由于Config Service和Config Client都实现了对Spring Environment和PropertySource抽象的映射,因此,Spring Cloud Config非常适合Spring应用程序,当然也可以与其它任何语言编写的应用程序配合使用。

  Config Server是一个可横向扩展、集中式的配置服务器,它用于集中管理应用程序各个环境下的配置,默认使用Git存储配置文件内容,也可以使用SVN存储,或者是本地文件存储。

  Config Client是Config Server的客户端,用于操作存储在ConfigServer中的配置内容。微服务在启动时会请求Config Server获取配置文件的内容,请求到后再启动容器。

Git上的配置文件springcloud-config-resources

springcloud-config-resources项目结构

  由于Spring Cloud Config Server默认采用的是Git,所以说我们就创建一个存放配置文件的目录,在这里,我将配置文件创建到了springcloud-config-resources中,其项目结构如下;

在这里插入图片描述

springcloud-config-resources源码

  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>springcloud-parent</artifactId>
        <groupId>com.lyc</groupId>
        <version>1.0-RELEASE</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-config-resources</artifactId>

    <name>SpringCloud微服务::配置文件</name>
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <finalName>${project.artifactId}</finalName>
    </build>

</project>

  microservice-dev.properties源码:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.1.1:3306/taobao?useUnicode=true
jdbc.username=root
jdbc.password=root

  由于这个项目是用来演示的,所以说这里的生产环境microservice-production.properties与测试环境microservice-test.properties都与开发环境microservice-dev.properties中的内容是一样的,因而我就不重复贴代码了。

  最后要做的就是将springcloud-config-resources发布到Git中,供配置文件中心Spring Cloud Config Server来访问。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值