【微服务笔记12】微服务组件之Hystrix和OpenFeign结合使用

这篇文章,主要介绍微服务组件之Hystrix和OpenFeign结合使用。

目录

一、Hystrix和OpenFeign结合使用

1.1、引入依赖

1.2、openfeign启用hystrix

1.3、编写FeignClient接口

1.4、编写FeignClient实现类

1.5、编写Controller控制器

1.6、启动类启动OpenFeign

1.7、运行测试

1.8、捕获异常信息


一、Hystrix和OpenFeign结合使用

在实际开发中,Hystrix都是和OpenFeign组件一起结合使用的,OpenFeign组件中已经包含了Hystrix,但是默认情况下,OpenFeign是没有开启Hystrix的功能,我们需要在application.yml配置文件中手动的开启Hystrix的功能。

1.1、引入依赖

  • 引入openfeign组件中已经引入了hystrix的依赖,所以这里只需要引入openfeign依赖即可。
<!-- 引入 OpenFeign 依赖 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
  • 需要注意的是,OpenFeign虽然集成了Hystrix,但是如果你要使用Hystrix中的 @HystrixCommand等注解,那还是需要引入下面的依赖。
  • 注意:hystrix的依赖根据需要自行引入,如果你项目中要使用hystrix的注解,那就引入这个依赖。
<!-- 引入 hystrix 依赖 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

1.2、openfeign启用hystrix

在application.yml配置文件中,添加如下配置:

# openfeign 开启 hystrix 的支持
feign:
  hystrix:
    enabled: true

1.3、编写FeignClient接口

  • 在@FeignClient注解中,使用 fallback 属性指定服务降级之后的处理类。
package com.gitcode.hystrix.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

/**
 * @version 1.0.0
 * @Date: 2023/4/13 11:14
 * @Copyright (C) ZhuYouBin
 * @Description: openfeign 和 hystrix 结合使用
 */
@FeignClient(name = "hystrix-provider", fallback = HystrixFallback.class)
public interface HystrixFeignClient {

    @GetMapping("/hystrix/provider/getById")
    Map<String, String> getUserInfoById(@RequestParam("id") String id);
    
}

1.4、编写FeignClient实现类

  • 需要注意的是:这个实现类必须注入到IOC容器里面。
package com.gitcode.hystrix.service;

import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;

/**
 * @version 1.0.0
 * @Date: 2023/4/13 11:16
 * @Copyright (C) ZhuYouBin
 * @Description: 用于 hystrix 实现服务降级的类
 */
// 将当前类注入到 IOC 容器里面
@Component
public class HystrixFallback implements HystrixFeignClient {

    @Override
    public Map<String, String> getUserInfoById(String id) {
        System.out.println("服务降级,请稍后重试!");
        Map<String, String> map = new HashMap<>();
        map.put("uname", "服务降级");
        map.put("pass", "000");
        map.put("age", "0");
        return map;
    }

}

1.5、编写Controller控制器

package com.gitcode.hystrix.controller;

import com.gitcode.hystrix.service.HystrixFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @version 1.0.0
 * @Date: 2023/4/13 11:27
 * @Copyright (C) ZhuYouBin
 * @Description:
 */
@RestController
@RequestMapping("/api/openfeign")
public class HystrixOpenFeignController {
    @Autowired
    private HystrixFeignClient hystrixFeignClient;
    
    @GetMapping("/hystrix")
    public Map<String, String> hystrix(String id) {
        return hystrixFeignClient.getUserInfoById(id);
    }
}

1.6、启动类启动OpenFeign

package com.gitcode.hystrix;

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

/**
 * @version 1.0.0
 * @Date: 2023/4/13 11:11
 * @Copyright (C) ZhuYouBin
 * @Description:
 */
// 启动类启动 OpenFeign 扫描
@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
public class FeignHystrixApp {
    public static void main(String[] args) {
        SpringApplication.run(FeignHystrixApp.class, args);
    }
}

1.7、运行测试

到这里,Hystrix和OpenFeign就基本上可以一起使用啦,启动工程,访问接口,当访问超时或者出现异常时候,就会调用服务降级之后的方法。

1.8、捕获异常信息

采用上面那种方式,当接口调用出现异常的时候,控制台是不会抛出异常的,所以为了能够获取到异常信息,OpenFeign中的@FeignClient注解提供了一个fallbackFactory属性。fallbackFactory属性需要指定一个服务降级工厂类,自定义的工厂类需要实现【FallbackFactory】接口,并且重写其中的【create()】方法,这个方法中会有一个Throwable类型的参数,这个参数就是运行过程中出现的一些异常信息。

package com.gitcode.hystrix.service;

import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @version 1.0.0
 * @Date: 2023/4/13 11:35
 * @Copyright (C) ZhuYouBin
 * @Description: 自定义的服务降级工厂类
 */
@Component
public class HystrixFallbackFactory implements FallbackFactory<HystrixFeignClient> {

    @Override
    public HystrixFeignClient create(Throwable ex) {
        return new HystrixFeignClient() {
            @Override
            public Map<String, String> getUserInfoById(String id) {
                System.out.println("[HystrixFallbackFactory]服务降级,请稍后重试!出现的异常是: " + ex.getMessage());
                Map<String, String> map = new HashMap<>();
                map.put("uname", "服务降级");
                map.put("pass", "000");
                map.put("age", "0");
                return map;
            }
        };
    }

}

在对应的Feign接口中,使用fallbackFactory属性。

@FeignClient(name = "hystrix-provider", fallbackFactory = HystrixFallbackFactory.class)

到此,Hystrix和OpenFeign结合使用就介绍完啦。

综上,这篇文章结束了,主要介绍微服务组件之Hystrix和OpenFeign结合使用。

猜你喜欢

转载自blog.csdn.net/qq_39826207/article/details/130125661