SpringCouldConfig 配置

用途

为一些大型的分布式系统,提供统一的配置管理。


配置集中管理中心.png

功能介绍

提供 服务端 和 客户端 支持
集中式 管理分布式环境下的应用配置
基于 Spring 环境,无缝 与 Spring 应用集成
可用于 任何 语言开发的程序
默认实现基于 git 仓库,可以进行 版本管理
可替换 自定义实现
Spring Cloud Config Server 作为配置中心服务端

拉取配置时更新 git 仓库副本,保证是最新结果
支持数据结构丰富,yml, json, properties 等
配合 eureke 可实现服务发现,配合 cloud bus 可实现配置推送更新
配置存储基于 git 仓库,可进行版本管理
简单可靠,有丰富的配套方案
Spring Cloud Config Client 默认客户端实现

SpringBoot 项目不需要改动任何代码,加入一个启动配置文件指明使用 ConfigServer 上哪个配置文件即可

项目使用实例.png

使用流程

1.引入依赖
2.加入注解
3.进行配置(加载路径)

路径规则

/{name}-{profiles}.yml
/{label}/{name}-{profiles}.yml

name 服务名
profiles 环境
label 分支(branch)

原理采用git公共配置的原理,让order服务去获取远程服务器上的配置信息,并保存在本地使用。如果没有获取到远程端的,就直接使用本地保存的配置。

服务端配置

1)添加Maven依赖:

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

2)Application添加注解

@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigApplication {

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

}

3)添加配置路径

spring:
  application:
    name: config  服务名称
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/peijinliang_7/could-config
          username: XXXXXXXX
          password: XXXXXXXXX
          basedir:  F:\GitHub\SpringCouldConfig\config 下载git文件的路径

客户端配置

1)添加Maven依赖:

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

2)添加配置

spring:
      application:
        name: order
      cloud:
        config:
          discovery:
            enabled: true
            service-id: CONFIG
          profile: dev
eureka:
  client:
    service-url:
       defaultZone :  http://localhost:8761/eureka/
#server:
#  port: 8082

3)修改Application.yml 文件名称为bootstrap.yml

默认先加载远程端的 但是两边就有冲突了,既然不能这么搞,那就换一种方式

注意事项Ps:

order.yml
order-dev.yml
order-test.yml

order 是指存放的公共的配置信息。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,347评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,053评论 6 342
  • 入门 介绍 Spring Boot Spring Boot 使您可以轻松地创建独立的、生产级的基于 Spring ...
    Hsinwong阅读 16,989评论 2 89
  • Spring Web MVC Spring Web MVC 是包含在 Spring 框架中的 Web 框架,建立于...
    Hsinwong阅读 22,679评论 1 92
  • 文/薛薛薛 风止了,小森起雾了。 潮汽上涌,像微重的薄雨。 山丘隐秘,像遮蔽的面纱。 时间静止与沸腾互换。 山林深...
    兄弟哦秘密阅读 2,999评论 1 2