乐趣区

关于hystrix: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 熔断...";}
        };
    }
}
退出移动版