Spring Boot 集成Fegin和Hystrix实现接口调用以及容错处理

一、Spring Boot 简介

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是简化新的Spring应用的初始搭建以及开发过程。该框架使用了特定的方式进行配置,从而使开发人员不再需要定义样板化的配置。Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。 

在使用Spring Boot之前,我们需要搭建一个项目架构并配置各种第三方库的依赖,还需要在XML中配置很多内容,繁杂且容易出错。Spring Boot 完全打破了我们之前的使用习惯,一分钟就可以创建一个Web开发的项目了通过Starter的方式轻松集成第三方的框架;去掉了XML的配置,全部用注解代替。

Spring Boot Starter 是用来简化jar包依赖的,集成一个框架只需要引入一个Starter,然后在属性文件中配置一些值,整个集成的过程就结束了。不得不说,Spring Boot 在内部做了很多的处理,让开发人员使用起来更加简单了。

下面总价性列几个使用Spring Boot 开发的优点:

  • 基于 Spring 开发 Web 应用更加容易;
  • 采用基于注解方式的配置,避免了编写大量重复的 XML 配置;
  • 可以轻松集成 Spring 家族的其他框架,比如:Spring JDBC、Spring Data等等;
  • 提供嵌入式服务器,令开发和部署都变的非常简单。

二、声明式 REST 客户端 Fegin

在 JAVA 项目中接口调用怎么做?比较常用的有 Httpclient 、Okhttp、HttpURLConnection、RestTemplate,这是几种比较常见的调用接口的方法,下面介绍的方法比这几个更简单、方便,它就是 Fegin。

Fegin 是一个声明式的REST客户端,它能让REST调用更加简单。Fegin 提供了 HTTP 请求的模板,通过编写简单的接口和插入注解,就可以定义好 HTTP 请求的参数、格式、地址等信息。

Fegin 会完全代理 HTTP 请求,我们只需要像调用方法一样调用它就可以完成服务请求以及相关处理。Spring Cloud 对 Fegin 进行了封装,使其支持 SprngMVC 标准注解和 HttpMessageConverters 。Fegin 可以与 Eureka 和 Ribbon 组合使用以支持负载均衡。

三、Hystrix 服务容错处理

Hystrix 是 Netflix 针对微服务分布式系统采用的熔断保护中间库,相当于电路中的保险丝。在微服务架构下,很多微服务都相互依赖,如果不能对依赖的服务进行隔离,那么服务本身也有很能发生故障,Hystrix 通过 HystrixCommand 对调用进行隔离,这样可以阻止故障的连锁效应,能够让接口调用快速失败并迅速恢复正常,或者回退并优雅降级。

四、简单使用

1、添加依赖

注意版本,这里添加了依赖版本管理

        <!-- feign -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-openfeign</artifactId>
		</dependency>

		<!-- hystrix断路器 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
		</dependency>

		<!-- Actuator监控 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>

		<!-- 查看监控数据 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
		</dependency>

	<!-- 依赖版本管理 -->
	<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>

2、application.properties

# feign
# 接口地址
feign.config.address.url=http://localhost:8080/bourse-web
# 名称
feign.config.address.name=bourse-web

# 开启hystrix熔断
feign.hystrix.enabled=true

3、定义一个 Fegin 的客户端

package com.modules.scistor.feign;

import com.modules.scistor.hystrix.HystrixClientFallback;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @Auther: lc
 * @Date: 2020/3/14 13:45
 * @Description: 使用fegin调用第三方远程接口
 */
@FeignClient(url = "${feign.config.address.url}", name = "${feign.config.address.name}", fallback = HystrixClientFallback.class)
public interface FeignClientService {

    /**
     * 违规交易行为
     * @return
     */
    @GetMapping(value="serviceapi/v1/riskMoniter/keywordstats")
    String riskMoniter();

}

4、Fegin 整合 Hystrix 服务容错

package com.modules.scistor.hystrix;

import com.modules.scistor.feign.FeignClientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @Auther: lc
 * @Date: 2020/3/15 13:36
 * @Description: hystrix接口调用熔断器
 */
@Component
@Slf4j
public class HystrixClientFallback implements FeignClientService {

    @Override
    public String riskMoniter() {
        log.error("riskMoniter:第三方接口调用失败!");
        return "第三方接口调用失败!";
    }
}

5、Controller 控制层

package com.modules.scistor.controller;

import com.modules.scistor.entity.Result;
import com.modules.scistor.feign.FeignClientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: lc
 * @Date: 2020/3/15 11:54
 * @Description: 风险监测管理
 */
@Api(tags = "风险监测管理")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/rest/risk")
public class RiskMonitorController extends BaseController{

    @Autowired
    private FeignClientService feignClientService;

    /**
     * 违规交易行为
     * @return
     */
    @ApiOperation(value="违规交易行为",notes = "违规交易行为")
    @GetMapping(value = "/riskMoniter")
    public Result riskMoniter() {
        String str = feignClientService.riskMoniter();
        return success(str);
    }
}

6、Swagger 调用测试

7、Hystrix 监控

在微服务架构中,Hystrix 除了实现容错外,还提供了实时监控功能。在服务调用时,Hystrix 会实时累积关于 HystrixCommand 的执行信息,比如每秒的请求书,成功数等。更多信息可查看官方文档,在上面 XML 我已经加入了依赖 Actuator。

 8、整合 Dashboard 查看监控数据

我们已经知道 Hystrix 提供了监控功能,可以通过 hystrix .stream 端点来获取监控数据,但是这些数据是以字符串的形式展示的,实际使用中不方便查看。我们可以借助 Hystrix-dashboard 对监控进行图形化展示。在上面 XML 我已经加入了依赖 Hystrix-dashboard。

发布了92 篇原创文章 · 获赞 100 · 访问量 23万+

猜你喜欢

转载自blog.csdn.net/lovelichao12/article/details/104899953