Hystrix: service downgrade

Cloud is the foundation, eureka is service registration and discovery, consumer is the consumer to consume the things in the provider, the consumption method is Feign and Ribbon, feign interface consumption, ribbon Rest consumption

The service degradation occurs on the client side, and the client requests to shut down the server, allowing him to access another degraded service

The service fusing occurs on the server side. If the server reports an exception, it will let the request go to the second service.

First write an implementation class

package com.kuang.springcloud.service;

import com.kuang.springcloud.pojo.Dept;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;

import java.util.List;
//降级
@Component
public class DeptClientServiceFallbackFactory implements FallbackFactory {

    @Override
    public DeptClientService create(Throwable throwable) {
        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 String addDept(Dept dept) {
                return null;
            }
        };
    }
}

Add an annotation to the interface in feign

package com.kuang.springcloud.service;

import com.kuang.springcloud.pojo.Dept;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

@Component
@FeignClient(value = "SPRINGCLOUD-PROVIDER-DEPT",fallbackFactory = DeptClientServiceFallbackFactory.class)
public interface DeptClientService {

    @GetMapping("/dept/get/{id}")
    Dept queryById(@PathVariable("id") Long id);

    @GetMapping("/dept/list")
    List<Dept> queryAll();

    @PostMapping("/dept/add")
    String addDept(Dept dept);



}

Add configuration to the yml file in consumer fegin 

server:
  port: 80

#开启降级feign.hystrix
feign:
  hystrix:
    enabled: true



#Eureka 配置
eureka:
  client:
    register-with-eureka: false #不向Eureka注册自己
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

 

Then shut down the server.

Service downgrade takes effect 

 

 That is, when the server A is full, you need to close other servers to run the service of A in order to support it, but when someone else accesses C, you must use service downgrade to let him know that the server is closed, instead of throwing an exception directly give him.

 

Guess you like

Origin blog.csdn.net/qq_53374893/article/details/132503039