Spring Cloud杜绝循环依赖
前言
大家在开发中有没有遇到过因循环依赖导致项目启动失败?在排查循环依赖的过程中有没困难?如何避免写出循环依赖的代码?
我没写过循环依赖的代码,作为稳定性负责人,我排查过多次。
有些逻辑简单的代码,循环依赖很容易排查。但是,我们的业务超级复杂,绝大多数循环依赖,一整天都查不出来。
起初我们遇到一个循环依赖处理一个,作为稳定性负责人,技术能干的事,不会让人做第二次,为此,我写了一段循环依赖巡检代码,把循环依赖扼杀在测试环境。
下面介绍下场景及处理思路,未必最优,欢迎交流。
背景
SpringCloud服务在上线时出现BeanCurrentlyInCreationException异常(服务本地启动无异常,测试环境启动也无异常,上线就偶尔异常)。
1,本地模拟如下:
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException:
Error creating bean with name
'studentA'
: Bean with name
'studentA'
has been injected into other beans [studentC] in its raw version as part of a circular reference, but has eventually been wrapped.
This means that said other beans
do
not use the
final
version of the bean.
This is often the result of over-eager type matching - consider using
'getBeanNamesOfType'
with the
'allowEagerInit'
flag turned off,
for
example.
2,生产场景如下图:

异常排查&模拟
经过生产排查和本地测试,产生此异常的场景如下:
1,如果类方法有@Async注解,则可能出现如上异常
2,如果存在循环依赖,并且类方法上有@Async注解,则必然会出现如上异常。
1,场景演示:
在spring中,基于field属性的循环依赖是可以的:
示例代码:
@Service
@Slf4j
public class StudentA { @Autowired
private StudentB studentB;
public String test(){
return studentB.test();
}
public String test1(){
return "Studenta1";
}
} @Slf4j
@Service
public class StudentB { @Autowired
private StudentC studentC; public String test() {
return "Studentb";
} public String test1() {
return studentC.test1();
}
} @Slf4j
@Service
public class StudentC { @Autowired
private StudentA studentA; public String test() {
return studentA.test();
} public String test1() {
return "Studentc1";
}
} @Autowired
private StudentA studentA ;
@Test
public void testA(){
String v= studentA.test();
log.info("testa={}",v);
}
以上代码输出正常
2,异常演示
如果我们的方法加上了@Async 注解。则抛出异常:
@Service
@Slf4j
public class StudentA { @Autowired
private StudentB studentB;
@Async
public String test(){
return studentB.test();
}
@Async
public String test1(){
return "Studenta1";
}
} @Slf4j
@Service
public class StudentB { @Autowired
private StudentC studentC;
@Async
public String test() {
return "Studentb";
}
@Async
public String test1() {
return studentC.test1();
}
} @Slf4j
@Service
public class StudentC { @Autowired
private StudentA studentA;
@Async
public String test() {
return studentA.test();
}
@Async
public String test1() {
return "Studentc1";
}
} @Autowired
private StudentA studentA ;
@Test
public void testA(){
String v= studentA.test();
log.info("testa={}",v);
}
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException:
Error creating bean with name 'studentA': Bean with name 'studentA' has been injected into other beans [studentC] in its raw version as part of a circular reference, but has eventually been wrapped.
This means that said other beans do not use the final version of the bean.
This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.
3,解决异常
A,B,C三个类中,至少一个类的field 必须加@Lazy
@Service
@Slf4j
public class StudentA { @Autowired
@Lazy
private StudentB studentB;
@Async
public String test(){
return studentB.test();
}
@Async
public String test1(){
return "Studenta1";
}
} @Slf4j
@Service
public class StudentB { @Autowired
@Lazy
private StudentC studentC;
@Async
public String test() {
return "Studentb";
}
@Async
public String test1() {
return studentC.test1();
}
} @Slf4j
@Service
public class StudentC { @Autowired
@Lazy
private StudentA studentA;
@Async
public String test() {
return studentA.test();
}
@Async
public String test1() {
return "Studentc1";
}
}
参考 :https://my.oschina.net/tridays/blog/805111
杜绝循环依赖的解决方案
1,查看bean的依赖

2,解析bean及其依赖beans
如上,我们可以查看某个bean的依赖项,由此,我们可以递归查找bean是否存在循环引用。
bean 信息模型
@Data
public class ApplicationBeans { private Map<String, ContextBeans> contexts;
} @Data
public class ContextBeans { private Map<String, BeanDescriptor> beans;
private String parentId;
}
3,代码实现
这里检测项目起名alarm-center,检测类为CheckCyclicDependenceService
@Slf4j
@RefreshScope
@Service
public class CheckCyclicDependenceService {
// 服务发现
@Autowired
private DiscoveryClient discoveryClient;
//默认检测 api,admin两个项目,如果想检测其它项目,可在config配置
@Value("${check-serviceids-value:api,admin}")
private String checkServiceIds;
//入口
public void checkCyclicDependence() {
if (Strings.isNullOrEmpty(checkServiceIds)) {
return;
}
List<String> serviceIds = Arrays.asList(checkServiceIds.split(","));
for (String serviceId : serviceIds) {
long start = System.currentTimeMillis();
RestTemplate restTemplate = new RestTemplate();
//根据服务名去consul找一个服务实例
ServiceInstance instance = discoveryClient.getInstances(serviceId).get(0);
String url = instance.getUri() + "/actuator/beans";
//所有的beans信息
String applicationBeansStr = restTemplate.getForObject(url, String.class);
ApplicationBeans applicationBeans = JSONObject.parseObject(applicationBeansStr, ApplicationBeans.class);
long end = System.currentTimeMillis();
log.info("checkCyclicDependence get applicationBeans end,serviceid={},coust={}", serviceId, (end - start));
Map<String, ContextBeans> contexts = applicationBeans.getContexts();
Map<String, BeanDescriptor> qualifiedBeans = new HashMap<>();
for (Map.Entry<String, ContextBeans> conEntry : contexts.entrySet()) {
if (!conEntry.getKey().startsWith(serviceId)) {
continue;
}
ContextBeans contextBeans = conEntry.getValue();
Map<String, BeanDescriptor> beans = contextBeans.getBeans(); for (Map.Entry<String, BeanDescriptor> entry1 : beans.entrySet()) {
String beanName = entry1.getKey().toLowerCase();
BeanDescriptor beanDescriptor = entry1.getValue();
if (!beanDescriptor.getType().startsWith("com.shuidihuzhu") || beanName.endsWith("aspect") || beanName.endsWith("controller")
|| beanName.endsWith("dao") || beanName.endsWith("datasource")
|| beanName.endsWith("fallback") || beanDescriptor.getDependencies().length == 0) {
continue;
}
qualifiedBeans.put(entry1.getKey(), beanDescriptor);
}
}
StringBuilder sb = new StringBuilder();
cyclicDependence(qualifiedBeans, sb);
if (sb.length() > 0) {
sb.append(System.getProperty("line.separator"));
sb.append("重注代码质量,尽量做到无循环依赖");
sb.append(System.getProperty("line.separator"));
sb.append("所属服务:" + serviceId);
//alarmClient.sendByUser(Lists.newArrayList("zhangzhi"), sb.toString());
alarmClient.sendByGroup("cf-server-alarm", sb.toString());
end = System.currentTimeMillis();
}
log.info("checkCyclicDependence end,serviceid={},coust={}", serviceId, (end - start));
} } public void cyclicDependence(Map<String, BeanDescriptor> beans, StringBuilder sb) { for (Map.Entry<String, BeanDescriptor> bean : beans.entrySet()) {
String beanName = bean.getKey();
check(beans, beanName, beanName, 0, sb);
}
} public void check(Map<String, BeanDescriptor> beans, String beanName, String dependenceName, int depth, StringBuilder sb) {
if (depth == 4) {//这里可以指定深度,层级过多,容易栈溢出
return;
}
depth++;
BeanDescriptor bean = beans.get(dependenceName);//依赖项的依赖项
if (bean != null) {
String[] deps = bean.getDependencies();//依赖项
for (String dep : deps) {
if (dep.equals(beanName)) {
sb.append(System.getProperty("line.separator"));
String str = String.format("%s和%s存在循环依赖;", beanName, dependenceName);
sb.append(str);
log.info(str);
} else {
check(beans, beanName, dep, depth, sb);
}
}
} }
}
效果
我们在测试环境有个job 每隔几分钟巡检,有循环依赖就企业微信报警,这里截取一段日志,如下:
最后
水滴保险商城-架构组招java实习生
要求:
1、具备较强的编程基本功,熟练掌握JAVA编程语言,熟悉常用数据结构与算法。
2、了解常用的开源框架和开源服务(Spring,Netty,MySQL,Redis,Tomcat,Nginx 等)。
3、熟悉网络编程、多线程编程、分布式等优先。
4、阅读过spring家族源码优先,有技术博客优先,熟悉spring-cloud技术栈优先。
5、 做事积极主动,有较强的执行能力和和较好的沟通能力。
Spring Cloud杜绝循环依赖的更多相关文章
- Spring源代码解析 ---- 循环依赖
一.循环引用: 1. 定义: 循环依赖就是循环引用,就是两个或多个Bean相互之间的持有对方,比方CircularityA引用CircularityB,CircularityB引用Circularit ...
- Spring源码-循环依赖源码解读
Spring源码-循环依赖源码解读 笔者最近无论是看书还是从网上找资料,都没发现对Spring源码是怎么解决循环依赖这一问题的详解,大家都是解释了Spring解决循环依赖的想法(有的解释也不准确,在& ...
- Spring中的循环依赖解决详解
前言 说起Spring中循环依赖的解决办法,相信很多园友们都或多或少的知道一些,但当真的要详细说明的时候,可能又没法一下将它讲清楚.本文就试着尽自己所能,对此做出一个较详细的解读.另,需注意一点,下文 ...
- Spring 如何解决循环依赖问题?
在关于Spring的面试中,我们经常会被问到一个问题,就是Spring是如何解决循环依赖的问题的. 这个问题算是关于Spring的一个高频面试题,因为如果不刻意研读,相信即使读过源码,面试者也不一定能 ...
- Spring如何解决循环依赖问题
目录 1. 什么是循环依赖? 2. 怎么检测是否存在循环依赖 3. Spring怎么解决循环依赖 本文主要是分析Spring bean的循环依赖,以及Spring的解决方式. 通过这种解决方式,我们可 ...
- Spring 如何解决循环依赖的问题
Spring 如何解决循环依赖的问题 https://blog.csdn.net/qq_36381855/article/details/79752689 Spring IOC 容器源码分析 - 循环 ...
- Spring如何解决循环依赖?
介绍 先说一下什么是循环依赖,Spring在初始化A的时候需要注入B,而初始化B的时候需要注入A,在Spring启动后这2个Bean都要被初始化完成 Spring的循环依赖有两种场景 构造器的循环依赖 ...
- 面试必杀技,讲一讲Spring中的循环依赖
本系列文章: 听说你还没学Spring就被源码编译劝退了?30+张图带你玩转Spring编译 读源码,我们可以从第一行读起 你知道Spring是怎么解析配置类的吗? 配置类为什么要添加@Configu ...
- spring怎么避免循环依赖
1.循环依赖 (1)概念 对象依赖分为强依赖和弱依赖: 强依赖指的是一个对象包含了另外一个对象的引用,例如:学生类中包含了课程类,在学生类中存在课程类的引用 创建课程类: @Data public c ...
随机推荐
- CF741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths 树上启发式合并(DSU ON TREE)
题目描述 一棵根为\(1\) 的树,每条边上有一个字符(\(a-v\)共\(22\)种). 一条简单路径被称为\(Dokhtar-kosh\)当且仅当路径上的字符经过重新排序后可以变成一个回文串. 求 ...
- Jmeter JDBC Request 使用详解
本篇博文讲解以MySQL为例,搞懂JDBC Request中MySQL的使用方法,换成其它数据库, 如Oracle.PSQL也会很容易上手. 一.基本配置 1.首先我们先了解一下,不同数据库的驱动类和 ...
- HNOI 2015 【亚瑟王】
看着洛谷里那一排任务计划,瑟瑟发抖...... 题目大意: 你有n张牌,每一张牌有一个发动的概率和造成的伤害值,游戏一共有r轮.对于每一轮游戏,你只能发动一张牌(在之前回合发动过的牌会被跳过,不予考虑 ...
- 多测师讲解html _链接标签004_高级讲师肖sir
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>链 ...
- day39 Pyhton 并发编程02 后
一.开启子进程的另一种方式 import os from multiprocessing import Process class MyProcess(Process): def __init__(s ...
- Django的安装和项目的启动
一.安装(安装最新LTS版): 1.命令行安装 pip install django==1.11.18 -i 源 2.pycharm 安装 二.创建项目 1.命令行创建 下面的命令创建了一个名为 ...
- 无法访问GitHub
我们开发者经常用的最大的同性交流平台--GitHub忽然访问不了了,很尴尬 可以打开控制台 ping一下 github.com 果不其然 不通 不过幸运的是里面有github的ip地址,好像是美国某个 ...
- MySQL锁详细讲解
本文章向大家介绍MySQL锁详细讲解,包括数据库锁基本知识.表锁.表读锁.表写锁.行锁.MVCC.事务的隔离级别.悲观锁.乐观锁.间隙锁GAP.死锁等等,需要的朋友可以参考一下 锁的相关知识又跟存 ...
- matplotlib 设置标题 xy标题等
import matplotlib.pyplot as plt import matplotlib as mpl baseclass=[1,2,3,4] name = ['class1','class ...
- JS获取DropDownList选择项的值
var dropDownList= document.getElementById("<%=DropDownListID.ClientID %>");//获取DropD ...