统一配置中心

为什么需要统一配置中心?

统一配置中心顾名思义,就是将配置统一管理,配置统一管理的好处是在日后大规模集群部署服务应用时相同的服务配置一致,日后再修改配置只需要统一修改全部同步,不需要一个一个服务手动维护

统一配置中心的架构图:

服务者消费者集群,路由集群Zuul的配置文件可以全部交由config管理,Eureka Server配置是绝对不行的,因为Config配置中心也是作为一个Client服务注册到Eureka Server的,先有Server。

1.配置中心服务器的开发

1.添加依赖
      <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
2.添加注解支持@EnableConfigServer
@EnableEurekaClient
@EnableConfigServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
3.在远程的仓库创建配置文件(yml,properties,yaml)

在这里我对两个服务者的配置文件进行管理

规则说明:加入你在github仓库添加了一个名为product.properties的配置文件,并且开启了配置中心服务器(假设端口为9999),使用浏览器测试访问:http://localhost:8766/product.properties 是访问不到任何内容的,访问http://localhost:8766/product-xxxxx.properties(xxx随便写)是可以访问到的,这是Config配置中心的规则,下面会说到,另一方面配置文件合并规则,在学习springboot时我们可以将配置文件拆分:主配置文件 application.yml存放公共配置 测试环境:testapp.yml 生产环境:product.yml 使用时根据情况主配置引入不同的副配置文件,这里Config配置中心规则与springboot是相似的。

如下仓库文件:

[product.properties]

eureka.client.service-url.defaultZone=http://peer:8761/eureka,http://peer1:8765/eureka
spring.application.name=eureka-provider

[product-8763.properties]

server.port=8763

[product-8764.properties]

server.port=8764

访问: http://localhost:9999/product-xxxxx.properties 得到公共配置文件

访问: http://localhost:9999/product-8763.properties 公共配置与8763私有配置的合并

访问: http://localhost:9999/product-8764.properties 公共配置与8764私有配置的合并

.properties后缀是可以修改为yml,yaml,json 以对应的格式返回在浏览器上。

4.配置相关的配置文件
#注册到注册中心
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
#默认端口号 8888
server.port=9999
# 实例名
spring.application.name=config
#配置git的远程仓库 https 暂时不支持ssh
spring.cloud.config.server.git.uri=https:xxxxxxx
5.启动配置中心服务

http://localhost:9999/order-a.yml

http://localhost:9999/order-a.yaml

http://localhost:9999/order-a.properties

http://localhost:9999/order-a.json

以上四种访问方式都可以

{name}/{profiles:.[^-].}

{name}-{profiles}.json

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

{name}/{profiles}/{label:.*}

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

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

{name}/{profile}/{label}/**

{name}/{profile}/{label}/**

说明:

label: 分支名称 默认是master分支

name:文件的服务的名称(自定义的名称)

profiles:不同环境

2.配置中心客户端使用

凡是交由配置中心管理的Client,想要获取配置文件需要添加以下依赖

1.导入相关依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
2.添加配置
#开启配置中心
spring.cloud.config.enabled=true
#找到配置中心实例
spring.cloud.config.discovery.service-id=CONFIG
#指定名字
spring.cloud.config.name=product
#指定环境 8763或8764 由客户端的不同而改变
spring.cloud.config.profile=8763
#指定分支
spring.cloud.config.label=master
#指定配置中心的uri
spring.cloud.config.uri=http://localhost:9999

注意:spring.cloud.config.uri=xxx必须指定Config配置中心的地址,如果不指定默认则从8888端口fetch配置是不成功的,除非你的配置中心端口就是8888。

3.注意将配置文件名修改为 bootstrap.yml 表示从云端获取配置文件

springcloud-spring cloud config统一配置中心的更多相关文章

  1. 跟我学SpringCloud | 第六篇:Spring Cloud Config Github配置中心

    SpringCloud系列教程 | 第六篇:Spring Cloud Config Github配置中心 Springboot: 2.1.6.RELEASE SpringCloud: Greenwic ...

  2. Spring Cloud Config的配置中心获取不到最新配置信息的问题

    Spring Cloud Config的配置中心获取不到最新配置信息的问题 http://blog.didispace.com/spring-cloud-tips-config-tmp-clear/

  3. Spring Cloud Config 实现配置中心,看这一篇就够了

    Spring Cloud Config 是 Spring Cloud 家族中最早的配置中心,虽然后来又发布了 Consul 可以代替配置中心功能,但是 Config 依然适用于 Spring Clou ...

  4. Spring Cloud Config(配置中心)

    每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code 一.简介 Spring Cloud Config为分布式系统中的外部配置提供服务器和客 ...

  5. Spring Cloud Config 分布式配置中心【Finchley 版】

    一. 介绍 1,为什么需要配置中心? 当服务部署的越来越多,规模越来越大,对应的机器数量也越来越庞大,靠人工来管理和维护服务的配置信息,变得困难,容易出错. 因此,需要一个能够动态注册和获取服务信息的 ...

  6. Spring Cloud Config 分布式配置中心使用教程

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

  7. Spring Cloud Config的配置中心使用非对称性加密

    首先,我们需要通过keytool工具来生成密钥对. keytool是JDK中的一个密钥和证书管理工具.它使用户能够管理自己的公钥/私钥对及相关证书,用于(通过数字签名)自我认证(用户向别的用户/服务认 ...

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

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

  9. 9.Spring Cloud Config统一管理微服务配置

    Spring Cloud Config统一管理微服务配置 9.1. 为什么要统一管理微服务配置 9.2. Spring Cloud Config简介 Spring Cloud Config为分布式系统 ...

随机推荐

  1. sitecore开发入门之如何在代码中获取SITECORE图像URL

    using Sitecore; using Sitecore.Data.Items; using Sitecore.Resources.Media; public string GetUrl() { ...

  2. localStorage,sessionStorage和cookie的区别

    sessionStorage 和 localStorage 是HTML5 Web Storage API 提供的,可以方便的在web请求之间保存数据.有了本地数据,就可以避免数据在浏览器和服务器间不必 ...

  3. VMware复制CentOS7,网络配置问题处理

    问题: vm安装CentOS7,静态IP配置完毕后,关闭虚机1,克隆虚拟机为2.克隆出来的虚拟机使用ip addr命令,发现网卡但和之前名称完全不一样变成ens33,也没有ip地址,但/etc/sys ...

  4. json转换导致金额失真问题解决

    平台的余额查询逻辑中,今天爆了个雷.因平台用户都是较大的商户,且资金一般都是整数(这也是埋雷这么久的原因吧),但今天有客户说他们今天充值金额有个0.63的零头,但为何页面展示的账户余额零头却只是0.6 ...

  5. 为fastdfs文件服务器新增一个storage

    一.前言: 前期,已经搭建好了一套fastdfs文件服务器,一个tracker和一个storage,且部署在同一台服务器上,已经正式投入运行快半年了,1T的空间现在只剩下100G容量了,现在需要扩容, ...

  6. Selenium WebDriver的工作原理

    先通过一个简单的类比说个好理解的,这个比喻是我从美版知乎Quora上看到的,觉得比较形象.好理解拿来用用. 我们可以把WebDriver驱动浏览器类比成出租车司机开出租车. 在开出租车时有三个角色: ...

  7. 2018-2019-2 《网络对抗技术》Exp0 Kali安装 Week1 20165335

    一.资源下载以及工具安装 1.下载虚拟机工具VMware. 下载链接 :https://www.baidu.com/link?url=uuaBW5ETUl3GrhUKvPbbEc7QlQvGHfpD8 ...

  8. postgresql 表触发器

    1.先建一个函数,用来执行触发器启动后要执行的脚本 CREATE OR REPLACE FUNCTION "public"."trigger_day_aqi"( ...

  9. 这个表明将http协议转成websocket协议

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 编译原理 #02# 简易递归下降分析程序(js实现)

    // 实验存档 截图: 代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"&g ...