(三)服务容错保护 Hystrix

一、简介

分布式系统中经常会出现某个基础服务不可用造成整个系统不可用的情况,这种现象被称为服务雪崩效应。为了应对服务雪崩,一种常见的做法是手动服务降级。而 Hystrix 的出现,给我们提供了另一种选择——熔断。

具体原理请前往https://windmt.com/2018/04/15/spring-cloud-4-hystrix/查看,写得非常详细。

二、创建项目

pom文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>eureka-feign-hystrix</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-feign-hystrix</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.yml如下:

spring:
  application:
    name: eureka-consumer-feign-hystrix
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7000/eureka/
server:
  port: 9000
feign:
  hystrix:
    enabled: true  #开启hystrix

feign调用类如下:

package com.example.eurekafeignhystrix.feign;

import com.example.eurekafeignhystrix.hystrix.HelloRemoteHystrix;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(name = "eureka-producer",fallback = HelloRemoteHystrix.class)
public interface HelloRemote {

    @GetMapping("/hello")
    String hello(@RequestParam(value = "name") String name);
}

断路器类如下:

package com.example.eurekafeignhystrix.hystrix;

import com.example.eurekafeignhystrix.feign.HelloRemote;
import org.springframework.stereotype.Component;

@Component
public class HelloRemoteHystrix implements HelloRemote {
    @Override
    public String hello(String name) {
        return "Hello World!";
    }
}

这个类的作用是,当feign调用失败的时候,自动返回该类的hello方法执行结果。

controller类如下:

package com.example.eurekafeignhystrix.controller;

import com.example.eurekafeignhystrix.feign.HelloRemote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @Autowired
    private HelloRemote helloRemote;

    @GetMapping("/hello")
    public Object hello(String name){
        String hello = helloRemote.hello(name);
        return hello;
    }
}

入口类如下:

package com.example.eurekafeignhystrix;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableFeignClients
@SpringBootApplication
public class EurekaFeignHystrixApplication {

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

}

以上,项目就创建完成了,其实该项目可以基于上篇博客的消费者项目改造,只需添加hystrix依赖,修改配置文件,添加hystrix类熔断。

三、测试

分别运行上篇博客的eureka项目,producer项目。然后再运行刚才创建的项目。

访问http://localhost:9000/hello?name=asd

可以看到我们访问成功了,这个时候我们把producer项目关了,然后再次访问

可以发现,没有报错,返回了一个Hello World! 

说明使用测试成功。

学习借鉴自https://windmt.com/2018/04/15/spring-cloud-4-hystrix/

发布了107 篇原创文章 · 获赞 1 · 访问量 5326

猜你喜欢

转载自blog.csdn.net/daziyuanazhen/article/details/104989801
今日推荐