spring cloud hystrix 简易配置

一般和feign一块处理

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

启用hystrix

# 启用hystrix
feign.hystrix.enabled=true

Service a 调用 service b

package com.itheima.hystrix.servicea.agent;

import feign.hystrix.FallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(name = "feign-hystrix-service-b",fallbackFactory = ServiceBAgentHystrix.class)
public interface ServiceBAgent {
    @GetMapping("/service-b/service")
    String service();
}

@Component
class ServiceBAgentHystrix implements FallbackFactory<ServiceBAgent>{

    @Override
    public ServiceBAgent create(Throwable cause) {
        return new ServiceBAgent() {
            @Override
            public String service() {
                return "service-b熔断...";
            }
        };
    }
}