Spring Cloud(5) Feign - 熔断机制

目标

  • 模拟实现熔断机制

account增加Hystrix依赖


            org.springframework.cloud
            spring-cloud-starter-hystrix
        

account 增加fallBack处理

@Component
public class WalletRemoteFallBack implements WalletRemoteService {
    @Override
    public String getWallet() {
        return "fall back";
    }
}

配置开启hystrix

# 端口号
server.port=8000

# 服务名
spring.application.name=account

# eureka服务注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:5000/eureka/

spring.cloud.loadbalancer.retry.enabled=true

feign.hystrix.enabled=true

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=10000

ribbon.ConnectTimeout=3000
ribbon.ReadTimeout=3000
ribbon.OkToRetryOnAllOperations=true
ribbon.MaxAutoRetries=1
ribbon.MaxAutoRetriesNextServer=1

远程调用类修改

@FeignClient(value = "wallet",fallback = WalletRemoteFallBack.class)
public interface WalletRemoteService {

    @GetMapping("getWallet")
    String getWallet();

}

关闭wallet应用,并启动account

访问 http://localhost:8000/userInfo

image.png

可以看出触发熔断机制,返回 fallBack 信息

你可能感兴趣的:(Spring Cloud(5) Feign - 熔断机制)