Spring Cloud之配置中心搭建
一、配置中心服务端搭建
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之配置中心搭建的更多相关文章
- 跟我学SpringCloud | 第七篇:Spring Cloud Config 配置中心高可用和refresh
SpringCloud系列教程 | 第七篇:Spring Cloud Config 配置中心高可用和refresh Springboot: 2.1.6.RELEASE SpringCloud: Gre ...
- 微服务SpringCloud之Spring Cloud Config配置中心Git
微服务以单个接口为颗粒度,一个接口可能就是一个项目,如果每个项目都包含一个配置文件,一个系统可能有几十或上百个小项目组成,那配置文件也会有好多,对后续修改维护也是比较麻烦,就和前面的服务注册一样,服务 ...
- 微服务SpringCloud之Spring Cloud Config配置中心服务化
在前面两篇Spring Cloud Config配置中心的博客中都是需要指定配置服务的地址url:spring.cloud.config.uri,客户端都是直接调用配置中心的server端来获取配置文 ...
- spring cloud --- config 配置中心 [本地、git获取配置文件]
spring boot 1.5.9.RELEASE spring cloud Dalston.SR1 1.前言 spring cloud config 配置中心是什么? 为了统一管理配 ...
- 玩转Spring Cloud之配置中心(config server &config client)
本文内容导航: 一.搭建配置服务中心(config server) 1.1.git方式 1.2.svn方式 1.3.本地文件方式 1.4.解决配置中包含中文内容返回乱码问题 二.搭建配置消费客户端( ...
- Spring Cloud之注册中心搭建
一.注册中心服务端搭建 1)引入相关Maven坐标 <dependency> <groupId>org.springframework.cloud</groupId> ...
- Spring Cloud Config 配置中心高可用
详细参见 <Spring Cloud 与 Docker微服务架构实战> p163-9.10 Spring Cloud Config 与 Eureka 配合使用 p163-9.12 Conf ...
- Spring Cloud Config 配置中心
请将远程配置文件的格式写对: 比如使用 *.yml 或者 *.properties yml: testconfig: testvalue properties: testconfig=testvalu ...
- Spring Cloud Config配置中心的使用
一.概述 1. 为什么使用? 1> 配置文件太多,不方便维护 2> 配置文件一般都保存这各种明文显示的密码,无法保证配置内容的安全性,也无法做到按权限分配给个人 3> 更新配置项目需 ...
随机推荐
- python --- 22 初始模块 random time collections functools
一 .初始模块 1.从⼩到⼤的顺序: ⼀条代码 < 语句块 < 代码块(函数, 类) < 模块 2.引入模块的方式 ① import 模块 ② from 模块 im ...
- Junit Framework -TestRule,自动化测试中如何再次运行失败的测试用例
有时由于服务器,浏览器等问题,会导致自动化测试用例运行失败,此处通过案例讲解如何使用Junit框架中的TestRule来实现重复运行失败的测试用例. 首先定义一个类并让它实现TestRule,代码如下 ...
- ODAC(V9.5.15) 学习笔记(四)TCustomDADataSet(2)
2.连接相关 名称 类型 说明 Connection 指向一个数据库连接对象 Disconnected 设置为True将在数据库关闭后继续保持数据集的开启状态. 3. 数据获取 名称 类型 说明 Fe ...
- 弄懂linux shell对包含$的变量的执行过程?
参考: http://www.linuxidc.com/Linux/2012-04/58095.htm 在包含变量的命令中, 命令是怎么执行的呢? 首先, 它会原封不动的, 只是按原样替换变量的内容. ...
- Hadoop【单机安装-测试程序WordCount】
Hadoop程序说明,就是创建一个文本文件,然后统计这个文本文件中单词出现过多少次! (MapReduce 运行在本地 启动JVM ) 第一步 创建需要的文件目录,然后进入该文件中进行编辑 ...
- Python数据类型补充1
一.可变和不可变类型 可变类型: 值变了,但是id没有变,证明没有生成新的值而是在改变原值,原值是可变类型 不可变类型:值变了,id也跟着变,证明是生成了新的值而不是在改变原值,原值是不可变 # x= ...
- MySql查询功能梳理
CREATE DATABASE CristinMysql Create table employee( eId int(9) not null auto_increment, eName varcha ...
- 设计模式之组合模式(composite)
概念: 将对象组合成树形结构以表示“部分-整体”的层次结构.使用户对单个对象和组合对象的使用更具有一致性. 适用性:想表示对象的部分-整体层次结构.
- CentOS6.5下搭建LAMP+FreeRadius+Daloradius Web管理和TP-LINK路由器、H3C交换机连接,实现,上网认证和记账功能
什么是RADIUS服务: RADIUS:(Remote Authentication Dial In User Service)中文名为远程用户拨号认证服务,简称RADIUS,是目前应用最广泛的AAA ...
- mysql 5.7.18 winx64安装配置方法
在mysql-5.7.18-winx64文件夹下新建my.ini文件 [mysql] # 设置mysql客户端默认字符集 default-character-set=utf8 [mysqld] #设置 ...