Spring Cloud系列教程四 :服务容错保护Spring Cloud Hystrix(F版)

介绍

微服务中通常设计多个服务之间的调用,当一些基础服务不可用时,可能会导致级联故障,最终导致服务整个服务不可用,称为服务雪崩。

为了解决这个问题,业内提出了断路器的概念,如果多次调用失败,则直接返回预设好的调用失败的结果,不在进行服务调用。详细过程请参考源码解析系列的文章

github地址:https://github.com/erlieStar/spring-cloud-learning

在ribbon使用hystrix

示例项目:consumer-ribbon-hystrix(spring-cloud-hystrix)

1.项目配置如下
pom.xml

<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-netflix-ribbon</artifactId>
</dependency>

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

application.yaml

server:
  port: 9002

spring:
  application:
    name: consumer-ribbon

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

2.启动类加@EnableHystrix注解,通过@HystrixCommand注解指定失败回调的方法

@RestController
@EnableHystrix
@EnableEurekaClient
@SpringBootApplication
public class ConsumerRibbonHystrix {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerRibbonHystrix.class);
    }

    @Autowired
    private RestTemplate restTemplate;

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @GetMapping("hello")
    @HystrixCommand(fallbackMethod = "helloError")
    public String hello(@RequestParam String name) {
        return restTemplate.getForObject("http://PRODUCER-SIMPLE/hello?name=" + name, String.class);
    }

    public String helloError(String name) {
        return "hello " + name + ", an error occur";
    }
}

启动eureka-service(spring-cloud-eureka)
启动producer-simple(spring-cloud-ribbon)

访问http://localhost:9002/hello?name=xiaoshi 显示

hello xiaoshi, I am from port: 8001

关闭producer-simple(spring-cloud-ribbon)
访问http://localhost:9002/hello?name=xiaoshi 显示

hello xiaoshi, an error occur

在feign使用hystrix

示例项目:consumer-feign-hystrix(spring-cloud-hystrix)

1.项目配置如下

pom.xml

<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>

application.yaml

server:
  port: 9003

spring:
  application:
    name: consumer-feign-hystrix

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

feign:
  hystrix:
    enabled: true

feign自带断路器,需要设置feign.hystrix.enabled=true打开断路器

2.通过注解@FeignClient的fallback属性指定失败回调的类

@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
public class ConsumerFeignHystrix {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerFeignHystrix.class);
    }

    @Autowired
    private SimpleClient simpleClient;

    @FeignClient(value = "producer-simple", fallback = SimpleClientHystrix.class)
    public interface SimpleClient {
        @RequestMapping("hello")
        String hello(@RequestParam("name") String name);
    }

    @Component
    public class SimpleClientHystrix implements SimpleClient {
        public String hello(String name) {
            return "hello " + name + ", an error occur";
        }
    }

    @RequestMapping("hello")
    public String hello(@RequestParam String name) {
        return simpleClient.hello(name);
    }
}

启动eureka-service(spring-cloud-eureka)
启动producer-simple(spring-cloud-ribbon)

访问http://localhost:9003/hello?name=xiaoshi 显示

hello xiaoshi, I am from port: 8001

关闭producer-simple(spring-cloud-ribbon)
访问http://localhost:9003/hello?name=xiaoshi 显示

hello xiaoshi, an error occur

关注

在这里插入图片描述

参考博客

发布了375 篇原创文章 · 获赞 1223 · 访问量 70万+

猜你喜欢

转载自blog.csdn.net/zzti_erlie/article/details/104096419