SpringCloud 在Feign 上使用 Hystrix

SpringCloud 中使用 Hystrix 一般有两个形式:

  1. 在RestTemplate 和 Ribbon 上使用 Hystrix
  2. 在Feign 上使用 Hystrix

前一种方法可以看这篇文章,今天学习第二种方法。

一:创建 eureka-feign-hystrix-client 服务

1.1 在主Maven工程中创建一个新的 Module 工程,命名为eureka-feign-hystrix-client。采用Spring Initializr 的方式的方式创建。
在这里插入图片描述

1.2 eureka-ribbon-hystrix-client 的 pom.xml 的内容如下:

<?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>com.springcloud</groupId>
        <artifactId>springcloud-hx</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>eureka-feign-hystrix-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-feign-hystrix-client</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

2.3 主Module 的 的 pom.xml 加上:
在这里插入图片描述
2.4 eureka-feign-hystrix-client 的 配置文件 application.yml 的内容如下:

server:
  port: 8767

spring:
  #配置程序名为eureka-feign-hystrix-client
  application:
    name: eureka-feign-hystrix-client

eureka:
  client:
    #服务注册地址
    serviceUrl:
      #注意: Eureka Server 的注册地址
      #将服务提供者注册到三个Eureka Server中去
      #defaultZone: http://peer1:8001/eureka/,http://peer2:8002/eureka/,http://peer3:8003/eureka/
      #defaultZone: http://peer1:8001/eureka/
      defaultZone: http://localhost:8761/eureka/

feign:
  hystrix:
    enabled: true  #开启 Hystrix 功能

2.5 在启动类上加上 @EnableDiscoveryClient 开启服务注册于发现,代码如下:

package com.example.eurekafeignhystrixclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
//开启服务注册于发现
@EnableDiscoveryClient
//开启 Feign Client 功能
@EnableFeignClients
public class EurekaFeignHystrixClientApplication {

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

}

2.6 由于Feign 的起步依赖中已经引入了Hystrix 的依赖,所以在Feign 中使用Hystrix 不需要引入任何的依赖,只需要在配置文件 application.yml 中 配置开启 Hystrix 的功能。代码如下:

feign:
  hystrix:
    enabled: true  #开启 Hystrix 功能

2.7 写一个FeignHystrixInter 接口类 ,代码如下:

package com.example.eurekafeignhystrixclient.inter;
import com.example.eurekafeignhystrixclient.config.FeignConfig;
import com.example.eurekafeignhystrixclient.fallback.FallBack;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;


//在接口上加 @FeignClient 注解来声明 一个Feign Client,其中 value 为 远程调用其他服务的服务名
//FeignConfig.class 为 Feign Client 的配置类,注入Retryer类的实例,这样在远程调用失败后,feign会进行重试
//使用 Spring MVC 的注解来绑定具体该服务提供的 REST 接口

//fallback 配置回调处理类,该处理类是作为 Feign 熔断器的逻辑处理类,实现FeignHystrixInter 接口
@FeignClient(value = "eureka-client",configuration = FeignConfig.class,fallback = FallBack.class)
public interface FeignHystrixInter {

    //使用 Spring MVC 的注解来绑定具体该服务提供的 REST 接口
    @GetMapping(value = "/HiController/hi/{name}")
    String sayHiFromEurekaClient(@PathVariable(value = "name") String name);
}

fallback 配置回调处理类,该处理类是作为 Feign 熔断器的逻辑处理类,实现FeignHystrixInter 接口。
2.8 创建FeignConfig 配置类,代码如下:

package com.example.eurekafeignhystrixclient.config;
import feign.Retryer;
import org.springframework.context.annotation.Configuration;
import static java.util.concurrent.TimeUnit.SECONDS;

//注入Retryer类的实例,这样在远程调用失败后,feign会进行重试
@Configuration
public class FeignConfig {

    public Retryer feignRetryer(){
        //Feign 默认的配置在请求失败后,重试次数为0,即不重试。
        //重试间隔 为100毫秒,最大重试时间为1秒,重试次数为5次
        //return new Retryer.Default();
        return new Retryer.Default(100,SECONDS.toMillis(1),5);
    }
}

2.9 接着写FallBack 类,实现FeignHystrixInter 接口,代码如下:

package com.example.eurekafeignhystrixclient.fallback;
import com.example.eurekafeignhystrixclient.inter.FeignHystrixInter;
import org.springframework.stereotype.Component;

@Component //注入Spring 容器中
public class FallBack implements FeignHystrixInter {

    @Override
    //写处理熔断器的具体逻辑
    public String sayHiFromEurekaClient(String name) {
        return "哦,面对疾风吧,哈撒给";
    }
}

在类上 加上 @Component注解,注入Spring 容器中

扫描二维码关注公众号,回复: 8632412 查看本文章

2.10 分别创建 FeignHystrixService 和 FeignHystrixController,代码如下:

package com.example.eurekafeignhystrixclient.service;

import com.example.eurekafeignhystrixclient.inter.FeignHystrixInter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FeignHystrixService {

    @Autowired
    private FeignHystrixInter feignHystrixInter;

    public String helloFeign(String name) {
        return feignHystrixInter.sayHiFromEurekaClient(name);
    }
}

package com.example.eurekafeignhystrixclient.web;
import com.example.eurekafeignhystrixclient.service.FeignHystrixService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/FeignHystrixController")
public class FeignHystrixController {

    @Autowired
    private FeignHystrixService feignHystrixService;

    @GetMapping("/hi/{name}")
    public String helloFeign(@PathVariable("name") String name) {
        StringBuffer sb = new StringBuffer();
        sb.append(feignHystrixService.helloFeign(name));
        return sb.toString();
    }
}

2.11 分别启动 eureka-server , eureka-client 8762 端口, eureka-client 8763端口 ,eureka-feign-hystrix-client 服务。
浏览器访问 http://localhost:8761/
在这里插入图片描述
截止在浏览器上 输入 :http://localhost:8767/FeignHystrixController/hi/java,得到结果如下:
在这里插入图片描述
关掉eureka-client (8762,8763 两个端口 )服务,效果如图:
在这里插入图片描述
所以,当eureka-client 服务 出现问题后,eureka-feign-hystrix-client 进入了fallback 的逻辑处理类(即 FackBack.class),由这个类来执行熔断器打开时的处理逻辑。

发布了33 篇原创文章 · 获赞 42 · 访问量 3160

猜你喜欢

转载自blog.csdn.net/weixin_40991408/article/details/103892328