Config Server从本地读取配置文件

将所有的配置文件统一写带Config Server过程的目录下,Config Server暴露Http API接口,Config Client调用Config Server的Http API来读取配置文件

1、引入依赖

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

2、启动类添加@EnableConfigServer注解

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

3、工程配置文件配置本地读取

spring:
cloud:
config:
server:
native:
search-locations: classpath:/shared
profiles:
active: native
application:
name: config-server
server:
port: 9001

4、创建共享配置文件config-client-dev.yml

server:
port: 9002
foo: foo version 1

==========================

1、引入依赖

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

2、bootstrap.yml作为程序的配置文件(bootstrap相对于application具有优先的执行顺序)

spring:
application:
name: config-client
cloud:
config:
uri: http://localhost:9001
fail-fast: true
profiles:
active: dev

这样的客户端程序已经在9002端口启动,也可以像本地文件一样读取远程的配置文件:

 @Value("${foo}")
String foo; @GetMapping("/hi")
public String hi() {
return foo;
}

Config Server从远程Git读取配置文件

spring:
cloud:
config:
server:
git:
uri: https://github.com/forezp/springcloudConfig
search-paths: respo
username: aaa
password: aaa
label: master
application:
name: config-server

高可用Config Server集群

1、配置Eureka Server

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
server:
port: 9003
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:${server.port}/eureka
@EnableEurekaServer
@SpringBootApplication
public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

2、改造config-server

spring:
cloud:
config:
server:
native:
search-locations: classpath:/shared
profiles:
active: native
application:
name: config-server
server:
port: 9001
eureka:
client:
service-url:
defaultZone: http://localhost:9003/eureka
@EnableConfigServer
@SpringBootApplication
@EnableEurekaClient
public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

3、改造config-client

spring:
application:
name: config-client
cloud:
config:
fail-fast: true
discovery:
enabled: true
service-id: config-server
profiles:
active: dev
server:
port: 9002
eureka:
client:
service-url:
defaultZone: http://localhost:9003/eureka
@SpringBootApplication
@RestController
@EnableEurekaClient
public class DemoApplication { @Value("${foo}")
String foo; @GetMapping("/hi")
public String hi() {
return foo;
} public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

Spring Cloud Bus刷新配置

只需要改造config-client部分,消息驱动

1、引入依赖

 <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

2、配置文件添加mq连接信息

spring:
application:
name: config-client
cloud:
config:
fail-fast: true
discovery:
enabled: true
service-id: config-server
profiles:
active: dev
rabbitmq:
host:
port:
username:
password:
management:
security:
enabled: false

3、在需要更新的配置类添加@RefreshScope,只有添加了该注解,才会服务不重启的情况下更新配置

4、访问一个应用,http://localhost:9003/bus/refresh即可更新所有集成了config-client的应用的配置信息

微服务深入浅出(8)-- 配置中心Spring Cloud Config的更多相关文章

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

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

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

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

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

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

  4. 配置中心 Spring Cloud config

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

  5. SpringCloud(6)分布式配置中心Spring Cloud Config

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

  6. .NET Core微服务之基于Steeltoe使用Spring Cloud Config统一管理配置

    Tip: 此篇已加入.NET Core微服务基础系列文章索引 =>  Steeltoe目录快速导航: 1. 基于Steeltoe使用Spring Cloud Eureka 2. 基于Steelt ...

  7. Spring Cloud(九)高可用的分布式配置中心 Spring Cloud Config 集成 Eureka 服务

    上一篇文章,讲了SpringCloudConfig 集成Git仓库,这一篇我们讲一下SpringCloudConfig 配和 Eureka 注册中心一起使用 在分布式系统中,由于服务数量巨多,为了方便 ...

  8. springboot+cloud 学习(五)统一配置中心 spring cloud config + cloud bus + WebHooks +RibbitMQ

    前言 微服务要实现集中管理微服务配置.不同环境不同配置.运行期间也可动态调整.配置修改后可以自动更新的需求,Spring Cloud Config同时满足了以上要求.Spring Cloud Conf ...

  9. 史上最简单的SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)

    上一篇文章讲述了一个服务如何从配置中心读取文件,配置中心如何从远程git读取配置文件,当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用,架构图如 ...

随机推荐

  1. pygame学习笔记(4)——声音

    转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi pygame.mixer是一个用来处理声音的模块,其含义为“混音器”.游戏中对声音的处理一般包括制造声音和播放声音 ...

  2. notepad++ 安装go插件

    1. 想学习go语言 使用notepad++ 但是发现无法安装 gonpp的插件 花了很长时间. 发现问题为: 前几天将notepad++ 升级到了 7.6 的版本 然后使用 plugin manag ...

  3. [转帖]22个必须学习的Linux安全命令

    22个必须学习的Linux安全命令 http://os.51cto.com/art/201808/581401.htm Linux系统的安全性涉及很多方面,从设置帐户到确保用户合法,限制比完成工作所需 ...

  4. Java 使用 dom4j 读取 xml文档 demo

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://www. ...

  5. 官方下拉刷新控件SwipeRefreshLayout的使用

    今天看博客,发现有了这个下拉刷新的控件,效果看上去还蛮好的,于是我也想研究的是使用一下,写个demo.其实使用很简单的,但就是为了能使用这个新组建我下了好久的更新,后来还是直接去官网下载最新的ADT得 ...

  6. MySQL 大表备份、改表

    0.背景: 需要对一个千万行数据的表新增字段,具体操作: a.dump 数据 b.delete 数据 c.alter 表 MySQL  版本为5.5,alter表时MySQL会锁表:表行数虽多,当数据 ...

  7. javascript和php使用ajax通信传递JSON

    JS和PHP直接通信常用ajax完成,以实现js上UI的动态变化.通信使用JSON或者XML传递数据.下面详细描述两者直接JSON字符串的传递. 下面案例是要传递这样的json数据: { " ...

  8. 使用SUID二进制文件进行Linux权限升级技巧

      0x00 基础知识 众所周知,在Linux中一切都以文件存在,包括具有允许或限制三个执行操作(即读/写/执行)权限的目录和设备.因此,当给任何文件设置权限时,应该需要了解允许的Linux用户或限制 ...

  9. UVA.11806 Cheerleaders (组合数学 容斥原理 二进制枚举)

    UVA.11806 Cheerleaders (组合数学 容斥原理 二进制枚举) 题意分析 给出n*m的矩形格子,给出k个点,每个格子里面可以放一个点.现在要求格子的最外围一圈的每行每列,至少要放一个 ...

  10. [HAOI2011]防线修建

    题目描述 近来A国和B国的矛盾激化,为了预防不测,A国准备修建一条长长的防线,当然修建防线的话,肯定要把需要保护的城市修在防线内部了.可是A国上层现在还犹豫不决,到底该把哪些城市作为保护对象呢?又由于 ...