一、配置中心服务端搭建

1)引入相关Maven坐标

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

2)加入相关注解

//加上@EnableConfigServer注解开启配置服务器的功能
@EnableConfigServer
@EnableEurekaClient

3)相关配置属性

server:
port: 8989
spring:
application:
# 需要指明spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name
name: zbq-config-server
# 指定主机名称
# cloud:
# client.hostname: localhost # 在配置文件中注明自己的服务注册中心的地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8781/eureka
# 开启除了主机名也可以通过ip来定义注册中心地址
instance:
prefer-ip-address: true
ip-address: localhost
# 定义服务续约任务的调用间隔时间,默认为30秒
lease-renewal-interval-in-seconds: 30
# 定义服务失效的时间,默认为90秒
lease-expiration-duration-in-seconds: 90
# preferIpAddress: true
# hostname: ${spring.cloud.client.ipAddress}
# instance-id: ${spring.cloud.client.ipAddress}:${server.port} # 配置config中心
spring.cloud.config:
server:
# 配置git仓库地址 http方式
# git.uri: https://github.com/zhangboqing/zbq-config-center.git
# 配置git仓库地址 ssh方式
git:
uri: ssh://git@github.com:zhangboqing/zbq-config-center.git
# 跳过ssh校验
# skipSslValidation: true
ignoreLocalSshSettings: true
# 下面两个参数没怎么弄明白,配上不好用,感兴趣的可以研究下
# hostKey: someHostKey
# hostKeyAlgorithm: ssh-rsa
privateKey: |
-----BEGIN RSA PRIVATE KEY-----
你的私钥
-----END RSA PRIVATE KEY-----
# 配置仓库路径下相对搜索位置,可以配置多个
git.searchPaths: zbq/myconfig1,zbq/myconfig2
# 如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写
# 访问git仓库的用户名
# username:
# 访问git仓库的用户密码
# password:
# 配置仓库的分支
label: master
#logging:
logging:
level:
root: info
additivity: off
file: /data/home/logs/zbq-config-server/zbq-config-server.log

二、配置中心客户端搭建

1)将项目配置迁移到zbq-config-center中(统一存放配置的项目)

  配置文件的命名必须按照下面的规范

  {application}-{profile}.properties 或 {application}-{profile}.properties 

  application和profile代表什么,看4点!!!

2)引入相关Maven坐标

 <!--spring cloud-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 连接配置中心 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- retry -->
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- actuator监控模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

3)加入相关注解

@EnableDiscoveryClient

4)相关配置属性,删除原来项目的application.yml文件,加入bootstrap.yml配置文件,这样就可以读取到远程的配置文件了

##在配置文件中注明自己的服务注册中心的地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8781/eureka # 注意:下面配置必须配置在bootstrap.yml或.properties中
# 访问配置信息URL与配置文件的映射关系
# /{application}/{profile}/{label}
# /{application}/{profile}.yml
# /{label}/{application}-{profile}.yml
# /{application}-{profile}.properties
# /{label}/{application}-{profile}.properties
spring:
# 需要指明spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name
# 对应配置文件规则中的{application}部分
application.name: zbq-backend
cloud.config:
# 对应配置文件规则中的{profile}部分
profile: dev
# 对应配置文件规则中的{label} 部分
label: master
# 配置中config-server的地址
# 1.通过URI指定配置中心
# uri: http://localhost:8783/
# 2.通过注册中心,来发现注册中心
discovery:
# 开启通过服务访问Config Server的功能
enabled: true
# 指定Config Server注册的服务名
serviceId: zbq-config-server # 失败快速响应与重试
# 开启客户端优先判断Config Server获取是否正常,并快速响应失败内容
fail-fast: true # 设置重试参数
retry:
# 下一间隔的乘数
multiplier: 1.1
# 初始重试间隔时间(单位毫秒),默认为1000
initial-interval: 1000
# 最大间隔时间,默认2000毫秒
max-interval: 2000
# 最大重试次数,默认6次
max-attempts: 6 # 动态刷新配置
# config-client中新增spring-boot-starter-actuator监控模块,其中包含了/refresh端点实现
# 该端点可用于实现客户端重新获取和刷新配置信息
#log
logging:
level:
root: info
additivity: off
file: /data/home/logs/zbq-backend/zbq-backend.log
pattern:
console: "%d{yyyy-MM-dd-HH:mm:ss} [%thread] %-5level %logger- %msg%n"

Spring Cloud之配置中心搭建的更多相关文章

  1. 跟我学SpringCloud | 第七篇:Spring Cloud Config 配置中心高可用和refresh

    SpringCloud系列教程 | 第七篇:Spring Cloud Config 配置中心高可用和refresh Springboot: 2.1.6.RELEASE SpringCloud: Gre ...

  2. 微服务SpringCloud之Spring Cloud Config配置中心Git

    微服务以单个接口为颗粒度,一个接口可能就是一个项目,如果每个项目都包含一个配置文件,一个系统可能有几十或上百个小项目组成,那配置文件也会有好多,对后续修改维护也是比较麻烦,就和前面的服务注册一样,服务 ...

  3. 微服务SpringCloud之Spring Cloud Config配置中心服务化

    在前面两篇Spring Cloud Config配置中心的博客中都是需要指定配置服务的地址url:spring.cloud.config.uri,客户端都是直接调用配置中心的server端来获取配置文 ...

  4. spring cloud --- config 配置中心 [本地、git获取配置文件]

    spring boot      1.5.9.RELEASE spring cloud    Dalston.SR1 1.前言 spring cloud config 配置中心是什么? 为了统一管理配 ...

  5. 玩转Spring Cloud之配置中心(config server &config client)

     本文内容导航: 一.搭建配置服务中心(config server) 1.1.git方式 1.2.svn方式 1.3.本地文件方式 1.4.解决配置中包含中文内容返回乱码问题 二.搭建配置消费客户端( ...

  6. Spring Cloud之注册中心搭建

    一.注册中心服务端搭建 1)引入相关Maven坐标 <dependency> <groupId>org.springframework.cloud</groupId> ...

  7. Spring Cloud Config 配置中心高可用

    详细参见 <Spring Cloud 与 Docker微服务架构实战> p163-9.10 Spring Cloud Config 与 Eureka 配合使用 p163-9.12 Conf ...

  8. Spring Cloud Config 配置中心

    请将远程配置文件的格式写对: 比如使用 *.yml 或者 *.properties yml: testconfig: testvalue properties: testconfig=testvalu ...

  9. Spring Cloud Config配置中心的使用

    一.概述 1. 为什么使用? 1> 配置文件太多,不方便维护 2> 配置文件一般都保存这各种明文显示的密码,无法保证配置内容的安全性,也无法做到按权限分配给个人 3> 更新配置项目需 ...

随机推荐

  1. C#趋势图(highcharts插件)

    <!--图表效果展现--> <div class="TUI-layout-center" style="overflow: auto;" id ...

  2. extjs使用笔记-21

    yii是后台php框架 而yui是前端界面框架,是ajax框架 Extjs是建立在yui基础上的.不要搞混了. jquery的ready方法,是在文档已经加载完毕\图像完全显示后, 才载入执行的. 由 ...

  3. 题解——洛谷P2734 游戏A Game 题解(区间DP)

    题面 题目背景 有如下一个双人游戏:N(2 <= N <= 100)个正整数的序列放在一个游戏平台上,游戏由玩家1开始,两人轮流从序列的任意一端取一个数,取数后该数字被去掉并累加到本玩家的 ...

  4. 论文阅读: End-to-end Learning of Action Detection from Frame Glimpses in Videos

      End-to-End Learning of Action Detection from Frame Glimpses in Videos  CVPR 2016  Motivation:    本 ...

  5. Unity3D学习笔记(三十六):Shader着色器(3)- 光照

    光照模型:用数学的方法模拟现实世界中的光照效果.   场景中模型身上的光反射到相机中的光线: 1.漫反射:产生明暗效果 2.高光反射:产生镜面反射,物体中有最亮且比较耀眼的一部分 3.自发光: 4.环 ...

  6. 【Hadoop 分布式部署 六:环境问题解决和集群基准测试】

    环境问题: 出现Temporary  failure  in  name  resolutionp-senior-zuoyan.com 的原因有很多,主要就是主机没有解析到, 那就在hadoop的sl ...

  7. js url参数和对象互转

    function param(a) { var s = [], rbracket = /\[\]$/, isArray = function(obj) { return Object.prototyp ...

  8. awk详解2

    7.控制语句 if(condition) {statments} 单分支语句 if(condition) {statments} else {statements}组合语句 while(condito ...

  9. Lintcode455-StudentID-Easy

    Implement a class Class with the following attributes and methods: A public attribute students which ...

  10. python中包的语法

    1.什么是包? 包是一种通过".模块名"来组织python模块名称空间的方式.我们穿件的每个文件夹都可以被称为包. 但是要注意, 在python2中规定. 包内必须存在__init ...