Spring Cloud 学习纪要四:Config

本文详细介绍SpringCloudConfig配置中心的搭建与使用,包括环境配置、依赖添加、配置文件上传及客户端代码修改,实现微服务配置统一管理,适用于SpringBoot 2.1.0和SpringCloud Greenwich.M1版本。

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

Spring Cloud Config:配置中心

  普通应用中,当一个系统的配置文件发生改变时,需要重启该服务才能使新配置生效。而Spring Cloud Config可以实现微服务中所有系统配置文件的统一管理,当配置文件发生变化的时候,系统会自动更新获取新的配置。

开发环境版本
IDEA2018.2.6
JDK1.8
Spring Boot2.1.0
Spring CloudGreenwich.M1

特别注意:本系列纪要环环相扣,建议从第一节开始阅读 点击跳转

新建配置中心服务

  在IDEA中通过Spring Initializr创建项目,选择Spring Boot 2.1.0版本、选择Maven依赖Eureka Discovery(Cloud Discovery中)和Config Server(Cloud Config中)。

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

加入注解

  在入口类添加@EnableEurekaClient@EnableConfigServer注解。

@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class, args);
    }
}

添加配置

  在文件application.yml添加配置。

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/ #,http://localhost:8762/eureka/
  instance:
    prefer-ip-address: true
    #    hostname: ProviderName
    instance-id: ${spring.cloud.client.ip-address}:${server.port}

#server:
#  port: 8071

spring:
  application:
    name: config
  cloud:
    config:
      server:
        git:
          uri: ${your_config_repository} #https://github.com/chung567115/SpringCloudConfig
          username: ${your_username}
          password: ${your_password}
          basedir: ${your_dir} # D:\IDEACode\Spring Cloud\config\basedir

上传配置文件

  在自己的git仓库中添加配置文件项目,本例中将前几章中的provider和consumer项目的yml配置文件上传到https://github.com/chung567115/SpringCloudConfig

运行config项目

  在VM options设置-DServer.port=8071,运行config项目,可以在Eureka页面看到本项目注册成功,访问http://localhost:8071/provider-dev.yml可以看到配置详情。实际上,我们访问xxxx/provider-dev.properties或xxxx/provider-dev.json,配置中心都将转换成对应格式的配置文件返回。其命名格式为{name}-{profiles}.{type},其中name为文件名,一般定义为项目名;profiles为环境,例如开发环境、测试环境、生产环境等;type为文件格式,例如yml、properties、json等。
yml
properties
json

修改客户端代码

  导入Spring Cloud Config客户端Maven依赖,在provider和consumer项目的pom.xml文件中加入以下代码并reimport:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-client</artifactId>
</dependency>

  为了能在项目启动的第一时间就应用远程git的配置,重命名provider和consumer项目的配置文件为bootstrap.yml,删除旧内容,保留必要信息。

spring:
  application:
    name: ${project_name} # provider/consumer
  # SpringCloudConfig
  cloud:
    config:
      discovery:
        enabled: true
        service-id: config
      profile: dev

运行provider或consumer项目

  运行provider或consumer项目,发现项目依然能够正常注册和被发现,服务间也能正常通信,证明远程配置中心的内容生效。

以上只是达到了统一管理配置,关于修改配置之后如何免重启自动生效,小博将在《Spring Cloud 学习纪要五:Bus》中讲解。

附件

本系列纪要博客源码:跳转到github
本系列纪要博客配置文件:跳转到github

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值