springcloud2 Hystrix熔断器(推荐Feign)

注:在Demo中的pom一样,这里推荐Feign,因为Feign集合了Hystrix熔断器

主要是在客户端(接收端)配置

主要是在application配置里开启,然后在接口和实现类配置。

 

1.主方法

@SpringBootApplication
@EnableEurekaClient //可加可不加,加了默认是euraka
@EnableFeignClients //开启Fiegn
//@EnableHystrix//不要打这个注解,因为Feign集成了注解,不然会报错
public class UserProviderApplication_4444 {
    public static void main(String[] args) {
        SpringApplication.run(UserProviderApplication_4444.class, args);
    }
}

 

2.配置

server:
  port: 4444
eureka:
  client:
     # registerWithEureka: false #作为一个注册中心不需要注册自己是否要注册到eureka
      #fetchRegistry: false #表示是否从Eureka Server获取注册信息
      serviceUrl:
        defaultZone:  http://peer1000:1000/eureka/,http://peer1001:1001/eureka/
        #defaultZone:  http://peer1000:1000/eureka/
spring:
  application:
    name: getting1
#开启hystirx,豪猪保护系统
feign:
  hystrix:
    enabled: true
ribbon:
  ConnectTimeout: 3000
  ReadTimeout: 60000
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 3000 #hystrix超时时间

 

3.Feign的接口

/**
 * @FeignClient("SENDING") 代表这个接口是一个feign的客户端
 * SENDING 是这个接口要调用的远程服务的名字
 * fallback = FeignClientEmployeeImpl.class就是出错后会调用一个回调方法
 */
@FeignClient(value = "SENDING",fallback = FeignClientEmployeeImpl.class )
public interface FeignClientEmployee {

    @RequestMapping("/employee/{id}")
    //将{id}的值转换成传入的参数
    public Employee employee(@PathVariable("id") Long id);
}

3.Feign的接口的实现类(出错的回调类)

@Service//交给spring管理
public class FeignClientEmployeeImpl implements FeignClientEmployee {
    @Override
    public Employee employee(Long id) {
        return new Employee(-1L,"失败");
    }
}

4.controller层同demo中Feign的一样。

5.测试:开启注册中心,开启发送端,开启客户端(Feign),然后关闭发送端,看是否有失败。信息

转载于:https://my.oschina.net/u/4083694/blog/3078333

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值