简介

有时客户端需要在 config server 无响应时进行重试,以给 config server 时间进行恢复。利用 spring 提供的重试组件,我们可以方便的配置重试机制,包括重试间隔,重试次数等。

项目源码

Gitee码云

为 web 项目添加依赖

开启客户端重试功能需要两个新依赖,spring-retryspring-boot-starter-aop,把如下代码添加到 web 项目的 pom.xml 文件中:

<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>1.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>

然后在 bootstrap.yml 文件中添加如下配置:

spring:
application:
name: web-client
cloud:
config:
uri: http://localhost:8888
fail-fast: true
retry:
initial-interval: 1000
max-attempts: 6
max-interval: 2000
multiplier: 1.1

首先把 spring.cloud.config.fail-fast 为true,即在获取不到远程配置时,立即失败,但是用下边的配置进行重试。

spring.cloud.config.retry 所有子项均为默认值:

  • initial-interval: 最初重试间隔为 1000 毫秒
  • max-attempts: 最多重试 6 次
  • max-interval: 最长重试间隔为 2000 毫秒
  • multiplier: 每次重试失败后,重试间隔所增加的倍数

测试

如果使用了本教程的项目,我们需要首先启动 configserver 项目,然后再启动 registry 项目开启 eureka,因为 web 客户端使用了 eureka 服务,之后关闭 configserver,然后启动 web 项目,会看到如下 log:

2018-05-15 16:04:58.421  INFO 2663 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: http://localhost:8888

重试 6 次失败后,客户端启动失败,如果中途开启 configserver,则 web 客户端启动成功。

细粒度控制重试

我们可以在代码中实现更精细的控制重试机制,在 web 项目中创建一个新的 java 类 cn.zxuqian.configurations.RetryConfiguration,添加如下代码:

package cn.zxuqian.configurations;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.retry.interceptor.RetryInterceptorBuilder;
import org.springframework.retry.interceptor.RetryOperationsInterceptor; public class RetryConfiguration {
private static Logger log = LoggerFactory.getLogger(RetryConfiguration.class); @Bean
@ConditionalOnMissingBean(name = "configServerRetryInterceptor")
public RetryOperationsInterceptor configServerRetryInterceptor() {
log.info(String.format(
"configServerRetryInterceptor: Changing backOffOptions " +
"to initial: %s, multiplier: %s, maxInterval: %s",
1000, 1.2, 5000));
return RetryInterceptorBuilder
.stateless()
.backOffOptions(1000, 1.2, 5000)
.maxAttempts(10)
.build();
}
}

这里我们定义了configServerRetryInterceptor方法用于 Spring Retry 使用我们自定义的重试拦截器。方法使用 RetryInterceptorBuilder 按要求创建了一个 stateless 的 RetryOperationsInterceptor,并设置了初始重试间隔为 1000 毫秒,增加倍数为 1.2 倍,最大重试间隔为 5000 毫秒,最大重试次数为 10 次,builder 还提供了诸如配置重试机制之类的接口,有兴趣的读者可自行研究。

@ConditionalOnMissingBean 标明当 BeanFactory 中没有名为 configServerRetryInterceptor 的 bean 时才匹配此 Bean。

最后在 src/main/resources/META-INF/ (没有可创建此文件夹) 新建一个 spring.factories 文件,指定我们刚创建类为启动时的配置,以在获取远程配置之前生效:

org.springframework.cloud.bootstrap.BootstrapConfiguration=cn.zxuqian.configurations.RetryConfiguration

测试

最后在关闭 configserver 的条件下启动 web 项目,然后就会看到重试十次之后,项目启动失败。

欢迎访问我的博客:张旭乾的博客

Spring Cloud Config Client 超时与重试的更多相关文章

  1. Spring cloud config client获取不到配置中心的配置

    Spring cloud client在配置的时候,配置文件要用 bootstrap.properties 贴几个说明的链接.但是觉得说的依然不够详细,得空详查. 链接1 链接2 链接3 原文地址:h ...

  2. Spring Cloud config之一:分布式配置中心入门介绍

    Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持.配置服务器为各应用的所有环境提供了一个中心化的外部配置.它实现了对服务端和客户端对Spring Environm ...

  3. spring cloud连载第二篇之Spring Cloud Config

    Spring Cloud Config Spring Cloud Config为分布式服务提供了服务侧和客户侧的外部配置支持.通过Spring Cloud Config你可以有一个统一的地方来管理所有 ...

  4. Spring Cloud Config(配置中心)

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

  5. Spring Cloud Config中文文档

    https://springcloud.cc/spring-cloud-config.html 目录 快速开始 客户端使用 Spring Cloud Config服务器 环境库 健康指标 安全 加密和 ...

  6. Spring Cloud搭建手册(2)——Spring Cloud Config

    ※在Dalston.SR2版本以后,均不能正常加密,如果必须使用此功能,需要降级到SR1或Camden SR7. 1.首先需要创建一个config-server工程,作为配置中心的服务器,用来与git ...

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

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

  8. Spring Boot + Spring Cloud 构建微服务系统(九):配置中心(Spring Cloud Config)

    技术背景 如今微服务架构盛行,在分布式系统中,项目日益庞大,子项目日益增多,每个项目都散落着各种配置文件,且随着服务的增加而不断增多.此时,往往某一个基础服务信息变更,都会导致一系列服务的更新和重启, ...

  9. Spring Cloud Config 配置中心 自动加解密功能 jasypt方式

    使用此种方式会存在一种问题:如果我配置了自动配置刷新,则刷新过后,加密过后的密文无法被解密.具体原因分析,看 SpringCloud 详解配置刷新的原理 使用  jasypt-spring-boot- ...

随机推荐

  1. [jvm] -- 类文件结构篇

    类文件结构 结构图  魔数 头四个字节,作用是确定这个文件是否为一个能被虚拟机接收的 Class 文件. Class 文件版本 第五和第六是次版本号,第七和第八是主版本号. 高版本的 Java 虚拟机 ...

  2. 乌班图16 配置nginx

    阿里云 乌班图16 安装ngnix sudo apt install nginx nginx 启动 重启 关闭 sudo service nginx start restart stop status ...

  3. Nginx配置中文参数说明

    #定义Nginx运行的用户和用户组 user www www; # #nginx进程数,建议设置为等于CPU总核心数. worker_processes 8; # #全局错误日志定义类型,[ debu ...

  4. python---filecmp 实现文件,目录,遍历子目录的差异对比功能。

    python---filecmp ilecmp可以实现文件,目录,遍历子目录的差异对比功能. 自带filecmp模块,无需安装. 常用方法说明 filecmp提供3个操作方法,cmp(单文件对比),c ...

  5. Java容器学习之List

    List接口继承了Collcetion接口,Collection接口又继承了超级接口Iterable,List是有序列表,实现类有ArrayList.LinkedList.Vector.Stack等. ...

  6. reverse 字符串翻转

    头文件 algorithm string s="hello"; reverse(s.begin(),s.end()); char c[]="hello"; re ...

  7. IntelliJ IDEA 2019.3.4永久破解(持续更新)--已更新

    第一步,下载最新破解包: 链接: https://pan.baidu.com/s/1djUF9TiNZC4rIfxczxfIew 提取码: f521 把破解包两个文件放进bin目录下,这一步极为重要! ...

  8. Python定义一个函数

    Python函数:实现某种功能的代码段 定义一个函数需要遵循的规则: 1.使用 def 关键字 函数名和( ),括号内可以有形参 匿名函数使用 lambda 关键字定义 2.任何传入参数和自变量必须放 ...

  9. PHP array_intersect_key() 函数

    实例 比较两个数组的键名,并返回交集: <?php$a1=array("a"=>"red","b"=>"gree ...

  10. JS&ES6学习笔记(持续更新)

    ES6学习笔记(2019.7.29) 目录 ES6学习笔记(2019.7.29) let和const let let 基本用法 let 不存在变量提升 暂时性死区 不允许重复声明 块级作用域 级作用域 ...