spring-cloud-config 配置中心及Zuul路由

本文详细介绍SpringCloudConfig的搭建过程及其实现原理,包括服务端与客户端的配置,利用Git作为配置信息存储,以及如何在微服务架构中管理与业务相关的配置内容。

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

写在前面

Spring Cloud Config是Spring创建的一个全新的服务端/客户端项目,为应用提供分布式的配置存储,提供集中化的外部配置支持。它除了适用于Spring构建的应用程序外,也可以在其他语言运行的应用程序中使用。

Spring Cloud Config分为服务端和客户端两部分。其中服务端称为分布式配置中心,用来连接配置仓库并为客户端提供获取配置信息、加密/解密信息的功能;客户端则是微服务架构中的各个微服务应用,它们通过指定的配置中心来管理与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。

Spring Cloud Config实现的配置中心默认采用Git来存储配置信息,但也提供了对其他存储方式的支持,比如SVN仓库、本地文件系统。在本例中构建了一个基于Git存储的分布式配置中心,并在客户端中演示了如何制定应用所属的配置中心,并能够从配置中心获取配置

搭建git服务器
创建用户 gitHub官网
新建项目 (git仓库)

测试配置中心环境
第一步:依赖管理


org.springframework.cloud
spring-cloud-config-server


org.springframework.boot
spring-boot-starter-web


org.springframework.cloud
spring-cloud-starter-eureka


org.mybatis.spring.boot


第二步:main方法
@SpringBootApplication
@EnableConfigServer
public class ConfigMain {
public static void main(String[] args) {
SpringApplication.run(ConfigMain.class,args);
}
}
***第三步:*application.yml 文件
这里路径指向git仓库(配置中心)写在resources里
server:
port: 8888
spring:
cloud:
config:
server:
git:
uri: https://github.com/用户名/configcentre

***第四步:*将uservice部署到GitHub仓库中
spring:
application:
name: uservice
profiles:
#环境切换
active: dev
# 配置中心 的ip和端口
# 这个文件等价于 http://localhost:8888/uservice-dev.yml
cloud:
config:
uri: http://localhost:8888

@SpringBootApplication
//启用微服务客户端
@EnableEurekaClient
@RestController
public class UserServiceMain {
public static void main(String[] args) {
new SpringApplicationBuilder(UserServiceMain.class).web(true).run(args);
}

}

测试
注册中心→配置中心→UserServiceMain → 依次启动
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值