Spring Cloud Alibaba 从零开始学习(七) ------ 配置中心 config

本文介绍了SpringCloudConfig作为分布式配置中心的使用,包括Server端和Client端的功能。通过引入相关依赖,启用配置服务器,并配置Git存储库,实现了配置文件的集中管理和动态更新。文章详细展示了如何创建ConfigServer,以及配置服务的启动和访问方式。
摘要由CSDN通过智能技术生成

配置中心组件Spring Cloud Config

1. 简介

在微服务架构中,通常都会有很多个微服务,当某些配置发生改变时,我们不可能⼀个⼀个去修改配置然后重启⽣效,在大多的场景下我们还需要在运⾏期间动态调整配置信息,⽐如:根据各个微服务的负载情况,动态调整数据源连接池⼤⼩,我们希望
配置内容发⽣变化的时候,微服务可以⾃动更新。因此,我们就需要对配置⽂件进⾏集中式管理,这就是分布式配置中⼼的作⽤。
Spring Cloud Config是⼀个分布式配置管理⽅案,分为Server端和 Client端两个部分。

  • **Server 端:**提供配置⽂件的存储、以接⼝的形式将配置⽂件的内容提供出去,通过使⽤@EnableConfigServer注解在Spring Boot 应⽤嵌⼊
  • **Client 端:**通过接⼝获取配置数据并初始化⾃⼰的应⽤

2. Spring Cloud Config的使用

Spring Cloud Config Server是集中式的配置服务,⽤于集中管理应⽤程序各个环境下的配置。 默认使⽤Git存储配置⽂件内容。

2.1 建立Config Server配置中⼼微服务

2.1.1 引入依赖
 <!--config配置中⼼服务端-->
 <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-config-server</artifactId>
 </dependency>
2.1.2 启用配置服务器

使⽤注解@EnableConfigServer开启配置中⼼服务器。

/**
 * @Author 笔墨画诗
 * @Version 1.0.0
 * @Create 2022/10/23 21:38
 * @Desc
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class,args);
    }
}
2.1.3 服务配置
server:
  port: 10024
eureka:
  instance:
    prefer‐ip‐address: true
    instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
  client:
    service-url:
      defaultZone: http://localhost1:10010/eureka,http://localhost2:10011/eureka
spring:
  application:
    name: config-server-mic
  cloud:
    config:
      server:
        git:
          uri: https://github.com/自己的账户/config-server-mic-repo.git
          username: 用自己的账号
          password: 用自己的密码
          search-paths:
            - config-server-mic-repo
          default-label: master
management:
  endpoint:
    health:
      show-details: always
  endpoints:
   web:
    exposure:
     include: "*"

访问:http://localhost:10024/master/config-server-mic-dev.yml,查看配置⽂件内容.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

笔墨画诗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值