springcloud(七)断路器(Hystrix)基于feign

版权声明:菜鸟_zhengke的博客 https://blog.csdn.net/qq_42014192/article/details/89496047

1.springcloud(五)服务消费者(Feign)负载均衡进行修改

https://blog.csdn.net/qq_42014192/article/details/89377430

2.Feign是自带断路器的,在D版本的Spring Cloud之后,它没有默认打开。需要在配置文件中配置打开它。

server:
  port: 8765 #服务端口

spring:
  application:
    name: service-feign

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

feign:
  hystrix:
    enabled: true

3.改造HelloService类,只需要在FeignClient的HelloService接口的注解中加上fallback的指定类就行了:

@FeignClient(value = "service-client",fallback = SchedualServiceClientHystric.class)
public interface HelloService {
    @RequestMapping(value = "/client",method = RequestMethod.GET)
     String clientService(@RequestParam(value = "name") String name);
}

4.SchedualServiceClientHystric要实现HelloService接口,并注入到Ioc容器中

@Component
public class SchedualServiceClientHystric implements HelloService {
    @Override
    public String clientService(String name) {
        return "sorry "+name;
    }
}

测试步骤:

  1. 启动注册中心
  2. 启动服务提供者,修改端口号达到集群效果,宕机一个服务
  3. 启动服务消费者断路器服务测试。

这就说明当 service-client 工程不可用的时候,service-ribbon调用 service-client 的API接口时,会执行快速失败,直接返回一组字符串,而不是等待响应超时,这很好的控制了容器的线程阻塞。

猜你喜欢

转载自blog.csdn.net/qq_42014192/article/details/89496047