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> 更新配置项目需 ...
随机推荐
- 向eclipse的JavaWeb项目中导入jar包
一: 在你所需的jar包网站下载对应的jar包.如org.apache.commons.lang.jar. 二:复制粘贴到该JavaWeb的WEB-INF目录下的lib目录下,如: 三:右键 ...
- ODAC(V9.5.15) 学习笔记(十一)TOraEncryptor、TOraPackage和TOraAlerter
TOraEncryptor 名称 类型 说明 DataHeader TCREncDataHeader 一些附加信息放入加密数据中,包括: ehNone 无附加信息 ehTag GUID和随机生成的 ...
- L2-001:dijskstra + 多条最短路径 + 记录中间路径
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805073643683840 思路: dijkstra算出最短路 ...
- CodeForces 459C Pashmak and Buses(构造)题解
题意:n个人,k辆车,要求d天内任意两人都不能一直在同一辆车,能做到给出构造,不能输出-1 思路:我们把某一个人这d天的车号看成一个d位的数字,比如 1 1 2 3代表第一天1号车.第二天1号车.第三 ...
- ssm项目部署到服务器过程
ssm项目部署到服务器过程 特别篇 由于准备春招,所以希望各位看客方便的话,能去github上面帮我Star一下项目 https://github.com/Draymonders/Campus-Sho ...
- Centos 7 官网下载安装mysql server 5.6
Centos 7 官网下载安装 mysql server # wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rp ...
- SalGAN: Visual saliency prediction with generative adversarial networks
SalGAN: Visual saliency prediction with generative adversarial networks 2017-03-17 摘要:本文引入了对抗网络的对抗训练 ...
- eclipse创建springBoot项目
创建Spring Boot 工程 先在eclipse中安装spring -tool -suite插件,然后根据以下步骤可以创建1.新建Spring Starter Project 2.Packagin ...
- ASP.NET MVC AntiForgeryToken
你开发一个网站,其中有个功能:新闻发布. 你是这样实现的: 1.保存新闻的方法是:/News/Save POST提交 2.接受两个参数:title和content 有一天,你登录网站(浏览器会保存相 ...
- SAP-批量修改主数据(客户、供应商、物料)
SAP-批量修改主数据(客户.供应商.物料) TCODE: MASS 对于批量修改主数据如客户,供应商等,可以试用一下Mass , 它所能修改的范围如下: 选定要修改的对象后,点击运行,会要求选择需要 ...