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熔断...";
            }
        };
    }
}
阅读 110

后端交流与学习
后端php,java,go学习
67 声望
7 粉丝
0 条评论
67 声望
7 粉丝
宣传栏