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. ASP.NET 简单的柱形图实现(附带示例)

    对于一些内部系统的项目,各种图表是在所难免的,因为图表可以更加清晰的表达出想看到的数据. 因为之前从来没有做过关于图表的东西,唯一能想到的就是“验证码”,所以应该是一个思路,用GDI去搞. 数据懒着去 ...

  2. 撩课-Web大前端每天5道面试题-Day9

    1. 请用至少3中方式实现数组去重? 方法一: indexOf ,,,,,,,,]; function repeat1(arr){ ,arr2=[];i<arr.length;i++){ ){ ...

  3. C#操作xml文件:使用XmlDocument 实现读取和写入

    XML文件是一种常用的文件格式,例如WinForm里面的app.config以及Web程序中的web.config文件,还有许多重要的场所都有它的身影.Xml是Internet环境中跨平台的,依赖于内 ...

  4. SSM框架文件远程服务器下载

    1.首先你必须要建立连接 获取URL的输入流 2.之后就是文件读取和写入了 3.还有就是设置响应头,响应码等 代码 @RequestMapping("/fileDownLoad") ...

  5. JDBC数据库连接池

    用户每次请求都需要向数据库获得链接,而数据库创建连接通常需要消耗相对较大的资源,创建时间也较长.假设网站一天10万访问量,数据库服务器就需要创建10万次连接,极大的浪费数据库的资源,并且极易造成数据库 ...

  6. HBase入门教程

    # 背景 最近看到公司一个项目用到hbase, 之前也一直想看下hbase.个人理解Hbase作为一个nosql数据库,逻辑模型感觉跟关系型数据库有点类似.一个table,有row即行,列.不过列是一 ...

  7. RegExp对象的exec方法

    RegExp对象的exec方法和String对象的match方法用法十分相似,分两篇博客讲讲其各自的用法和它们之间的异同. 下一篇讨论match方法的用法和两者的异同. 定义及语法 [定义] exec ...

  8. <Android 基础(二十二)> EditText 无法显示完全以及尝鲜Android N

    前言 最近将Android Studio更新到了2.2 ,模拟器的Android版本也来到了最新的Nougat.很令人兴奋的一件事情呢! 对, 我就是这么没出息.文章结尾来几张图. 问题 最近遇到一个 ...

  9. 《APP移动终端决胜之道视觉设计艺术》学习笔记

    1.20-2.9 1.合理的层级化2.信息的整合(短信收发件箱),信息的整合就像创建文件夹,可以将相关的东西放在一起,以便于使用者搜索与查找3.(微信聊天界面)相比之下使用了对话框图形的界面,元素更加 ...

  10. python 并发socketserver模块

    1.源码class 1.server类:处理链接 +------------+ | BaseServer | +------------+ | v +-----------+ +----------- ...