The fuse is on the server side, a certain service times out or is abnormal, causing a fuse, and a backup solution (fuse) is provided if there is a problem
The downgrade is on the client side, considering the website request load, and temporarily closing some interfaces. After a service is blown or closed, the service will not be called. At this time, the client can prepare a FallbackFactory and return the default value
project structure
Enable annotations in the API interface
@FeignClient(value = "http://SPRINGCLOUD-PROVIDER-DEPT",fallbackFactory = DeptClientServiceFallbackFactory.class)
Implementation class
//降级
@Component
public class DeptClientServiceFallbackFactory implements FallbackFactory {
@Override
public DeptClientService create(Throwable cause) {
return new DeptClientService() {
@Override
public Dept queryById(Long id) {
return new Dept()
.setDeptno(id)
.setDname("id=>"+id+"无对应信息,降级处理,该服务已关闭")
.setDb_source("无数据");
}
@Override
public List<Dept> queryAll() {
return null;
}
@Override
public boolean addDept(Dept dept) {
return false;
}
};
}
}
Enable downgrade in project structure application.yml
#开启降级
feign:
circuitbreaker:
enabled: true
Normal access
There is a fuse mechanism and the server is not closed
After the degraded service is enabled, close the server
The interface is closed and the service cannot be called