Learning blog: [SpringCloud] Hystrix service downgrade

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
insert image description here


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;
            }
        };
    }
}


insert image description here
Enable downgrade in project structure application.yml

#开启降级
feign:
  circuitbreaker:
    enabled: true

Normal access
insert image description here
There is a fuse mechanism and the server is not closed
insert image description here
After the degraded service is enabled, close the server
insert image description here
insert image description here

The interface is closed and the service cannot be called

Guess you like

Origin blog.csdn.net/Aurinko324/article/details/125661666