8.1、Config Server 本地读取配置文件

Config Server 可以从本地仓库读取配置文件,也可以从远处 Git 仓库读取。
 
本地仓库是指将所有的配置文件统 写在 Config Server 工程目录下。
Config Sever 暴露 HttpAPI 接口, Config Client 通过调用
Config Sever 的Http API 接口来读取配置文件
 
Config Server:
新建的工程目录:

pom文件一如的依赖:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

application.yml

spring:
application:
name: config-server
cloud:
config:
server:
native:
search-locations: classpath:/shared
profiles:
active: native server:
port:
 
spring. profiles.active=native 来配置 onfig Server 本地读取配置,读取配置
的路径为 classpath 下的 hared 目录。
 
#Spring Cloud Config也提供本地存储配置的方式。
#我们只需要设置属性spring.profiles.active=native,
#Config Server会默认从应用的src/main/resource目录下检索配置文件。
#也可以通过spring.cloud.config.server.native.searchLocations=file:F:/properties/属性来指定配置文件的位置。
#虽然Spring Cloud Config提供了这样的功能,但是为了支持更好的管理内容和版本控制的功能,还是推荐使用git的方式
  • spring.application.name:对应前配置文件中的{application}部分
  • spring.cloud.config.profile:对应前配置文件中的{profile}部分
  • spring.cloud.config.label:对应前配置文件的git分支
  • spring.cloud.config.uri:配置中心的地址

config-client-dev.yml

server:
port: foo: foo verssion
主配置类:
@EnableConfigServer:开启 Config Server 的功能
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication { public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}

  

Config Client
工程的目录:

pom文件的依赖:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

bootstrap.yml

spring:
application:
name: config-client
cloud:
config:
uri: http://localhost:8769
fail-fast : true profiles:
active: dev
其配置文件bootstrap.yml 中做程序的配置
bootstrap 相对 application 有优先的执行顺序
bootstrap.yml 配置文件中指定了程序名为config-cleint
 
向URL地址为http://localhost:8769的config-server读取配置文件
如果没有读取成功,则快速失败(fail-fast)读取的是dev文件
 
bootstrap.yml配置文件中的变量 spring. application.name 和变 量sprig.profiles.active }
两者以“-”相连,构成了向Config Server读取的配置文件名,所以本实例中:读取的是config-client-dev. yml

TestController.java

@RestController
public class TestController { @Value("${foo}")
String foo; @GetMapping("/hi")
public String hi(){
return foo;
}
}
启动config-server服务
启动config=client可以在日志中发现:向其地址读取配置文件

此时是在controller中读取的值

8.2、ContigSe刚从远程侃仓库读取配置文件

 待补充......

8、Spring Cloud-配置中心 Spring Cloud Config(待补充)的更多相关文章

  1. Spring Cloud配置中心(Config)

    Spring Cloud配置中心(Config) Spring Cloud是现在流行的分布式服务框架,它提供了很多有用的组件.比如:配置中心.Eureka服务发现. 消息总线.熔断机制等. 配置中心在 ...

  2. 第六篇: 分布式配置中心(Spring Cloud Config)

    一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件. 在Spring Cloud中,有分布式配置中心组件spring cloud confi ...

  3. 一起来学Spring Cloud | 第七章:分布式配置中心(Spring Cloud Config)

    上一章节,我们讲解了服务网关zuul,本章节我们从git和本地两种存储配置信息的方式来讲解springcloud的分布式配置中心-Spring Cloud Config. 一.Spring Cloud ...

  4. Spring Cloud Config(一):聊聊分布式配置中心 Spring Cloud Config

    目录 Spring Cloud Config(一):聊聊分布式配置中心 Spring Cloud Config Spring Cloud Config(二):基于Git搭建配置中心 Spring Cl ...

  5. 配置中心 Spring Cloud config

    配置管理工具包,让你可以把配置放到远程服务器,集中化管理集群配置,目前支持本地存储.Git以及Subversion. 1.服务端 创建spring boot 项目 主要依赖 <dependenc ...

  6. (七)Spring Cloud 配置中心config

      spring cloud config是一个基于http协议的远程配置实现方式. 通过统一的配置管理服务器进行配置管理,客户端通过http协议主动的拉取服务的的配置信息,完成配置获取. 下面我们对 ...

  7. 记录一个 spring cloud 配置中心的坑,命令行端口参数无效,被覆盖,编码集问题无法读取文件等.

    spring cloud 配置中心 结合GIT , 可以运行时更新配置文件.发送指令让应用重新读取配置文件. 最近在测试服务器实现了一套,结果CPU 实用率暴增,使用docker compose启动 ...

  8. spring cloud 配置中心

    1. spring cloud配置中心server 1.1 创建git仓库 首先在github上搭建一个存储配置中心的仓库,需要创建两个分支,一个是master,一个是dev分支.自己学习可以用公开库 ...

  9. springcloud(五):Spring Cloud 配置中心的基本用法

    Spring Cloud 配置中心的基本用法 1. 概述 本文介绍了Spring Cloud的配置中心,介绍配置中心的如何配置服务端及配置参数,也介绍客户端如何和配置中心交互和配置参数说明. 配置中心 ...

  10. springcloud(六):Spring Cloud 配置中心采用数据库存储配置内容

    Spring Cloud 配置中心采用数据库存储配置内容 转自:Spring Cloud Config采用数据库存储配置内容[Edgware+] Spring Cloud Server配置中心采用了G ...

随机推荐

  1. 七、spark核心数据集RDD

    简介 spark RDD操作具体参考官网:http://spark.apache.org/docs/latest/rdd-programming-guide.html#overview RDD全称叫做 ...

  2. linux的环境变量与文件查找

    1. 环境变量 1.1 变量 shell 中的变量有不同类型,可参与运算,有作用域限定 变量的作用域即变量的有效范围(比如一个函数中.一个源文件中或者全局范围),在该范围内只能有一个同名变量.一旦离开 ...

  3. Groovy中的操作符重载

    操作者 方法 a + b a.plus(b)中 a - b a.minus(b)中 a * b a.multiply(b)中 a ** b a.power(b)中 a / b a.div(b)中 a ...

  4. 错误:Attempted to load applicationConfig: [classpath:/application.yml] but snakeyaml was not found on the classpath

    MyEclipse导入工程,报错如下: ::42.187 [main] ERROR org.springframework.boot.SpringApplication - Application r ...

  5. WinFrom柱形图

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  6. DOM基础操作(一)

    DOM的基本操作有四种,我们会逐一给大家进行展示 增加操作 1.创建元素节点 createElement 我们可以通过document.createElement(‘div’);这个方法来创建一个元素 ...

  7. CentOS6下docker的安装和使用

    CentOS6下docker的安装和使用 Docker是一个开源的应用容器引擎,可以轻松的为任何应用创建一个轻量级的.可移植的.自给自足的容器.利用Linux的LXC.AUFS.Go语言.cgroup ...

  8. localStorage跟cookie的使用

    最近做了记住密码功能,用localStorage跟cookie都尝试用了一下,感觉都挺好哈,很方便,特此记录 html代码: <input type="text" id=&q ...

  9. webapi 实体作为参数,自动序列化成xml的问题

    原文:http://bbs.csdn.net/topics/392038917 关注 Ray_Yang Ray_Yang 本版等级:   #6 得分:0回复于: 2016-10-27 21:30:51 ...

  10. kernel update

    CentOS/RHEL更新包:https://rhn.redhat.com/errata/RHSA-2017-1382.html yum makecache --更新源 yum update sudo ...