场景

SpringCloud-服务注册与实现-Eureka创建服务注册中心(附源码下载):

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102535957

SpringCloud-服务注册与实现-Eureka创建服务提供者(附源码下载):

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102558004

SpringCloud-创建服务消费者-Ribbon方式(附代码下载):

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102558080

SpringCloud-创建服务消费者-Feign方式(附代码下载)::

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102595895

在上面已经实现服务注册中心、服务提供者和以Ribbon方式和Fign方式实现服务消费者的前提下,使用熔断器防止服务雪崩。

在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以通过 RPC 相互调用,在 Spring Cloud 中可以用 RestTemplate + Ribbon 和 Feign 来调用。为了保证其高可用,单个服务通常会集群部署。由于网络原因或者自身的原因,服务并不能保证 100% 可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,Servlet 容器的线程资源会被消耗完毕,导致服务瘫痪。服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这就是服务故障的 “雪崩” 效应。

熔断器打开后,为了避免连锁故障,通过 fallback 方法可以直接返回一个固定值。

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi

关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

Ribbon中使用熔断器

SpringCloud-创建服务消费者-Ribbon方式(附代码下载):

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102558080

在上面使用Ribbon实现创建服务消费者。

我们在pom.xml中加入hystrix的依赖:

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

完整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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.</modelVersion> <parent>
<groupId>com.badao</groupId>
<artifactId>hello-spring-cloud-dependencies</artifactId>
<version>1.0.-SNAPSHOT</version>
<relativePath>../hello-spring-cloud-dependencies/pom.xml</relativePath>
</parent> <artifactId>hello-spring-cloud-web-admin-ribbon</artifactId>
<packaging>jar</packaging> <name>hello-spring-cloud-web-admin-ribbon</name>
<url>https://blog.csdn.net/badao_liumang_qizhi</url>
<inceptionYear>-Now</inceptionYear> <dependencies>
<!-- Spring Boot Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot End --> <!-- Spring Cloud Begin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</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>
<!-- Spring Cloud End --> <!-- 解决 thymeleaf 模板引擎一定要执行严格的 html5 格式校验问题 -->
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.funtl.hello.spring.cloud.web.admin.ribbon.WebAdminRibbonApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>

然后在应用启动类Application中增加@EnableHystrix注解

package com.badao.hello.spring.cloud.web.admin.ribbon;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class WebAdminRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(WebAdminRibbonApplication.class, args);
}
}

然后在Service中添加@HystrixCommand注解

在 Ribbon 调用方法上增加 @HystrixCommand 注解并指定 fallbackMethod
熔断方法。

package com.badao.hello.spring.cloud.web.admin.ribbon.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; @Service
public class AdminService { @Autowired
private RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "hiError")
public String sayHi(String message) {
return restTemplate.getForObject("http://hello-spring-cloud-service-admin/hi?message=" + message, String.class);
} public String hiError(String message) {
return "Hi,your message is :\"" + message + "\" but request error.";
}
}

测试熔断器

为了测试熔断器效果,我们将服务提供者关闭,此时再次请求:

http://localhost:8764/hi?message=HelloRibbon

Feign中使用熔断器

Feign自带熔断器,所以不用添加依赖,只需要在配置文件中配置打开。

feign:
hystrix:
enabled: true

完整配置文件:

spring:
application:
name: hello-spring-cloud-web-admin-feign
thymeleaf:
cache: false
mode: LEGACYHTML5
encoding: UTF-
servlet:
content-type: text/html server:
port: eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ feign:
hystrix:
enabled: true

然后再Service中增加fallback指定类

package com.badao.hello.spring.cloud.web.feign.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; @FeignClient(value = "hello-spring-cloud-service-admin", fallback = AdminServiceHystrix.class)
public interface AdminService { @RequestMapping(value = "hi", method = RequestMethod.GET)
public String sayHi(@RequestParam(value = "message") String message);
}

此时再service包下创建熔断器并实现对应的Feign接口

package com.badao.hello.spring.cloud.web.feign.service;

import org.springframework.stereotype.Component;

@Component
public class AdminServiceHystrix implements AdminService { @Override
public String sayHi(String message) {
return "Hi,your message is :\"" + message + "\" but request error.";
}
}

然后将服务提供者关闭,再次请求:

http://localhost:8765/hi?message=HelloFeign

代码下载

https://download.csdn.net/download/badao_liumang_qizhi/11871136

SpringCloud-使用熔断器防止服务雪崩-Ribbon和Feign方式(附代码下载)的更多相关文章

  1. SpringCloud-创建服务消费者-Feign方式(附代码下载)

    场景 SpringCloud-服务注册与实现-Eureka创建服务注册中心(附源码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...

  2. SpringCloud-创建服务消费者-Ribbon方式(附代码下载)

    场景 SpringCloud-服务注册与实现-Eureka创建服务注册中心(附源码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...

  3. Dubbo搭建HelloWorld-搭建服务提供者与服务消费者并完成远程调用(附代码下载)

    场景 Dubbo简介与基本概念: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103555224 Dubbo环境搭建-ZooKe ...

  4. 从实例一步一步入门学习SpringCloud的Eureka、Ribbon、Feign、熔断器、Zuul的简单使用(附代码下载)

    场景 SpringCloud -创建统一的依赖管理: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102530574 Sprin ...

  5. SpringCloud基础概念学习笔记(Eureka、Ribbon、Feign、Zuul)

    SpringCloud基础概念学习笔记(Eureka.Ribbon.Feign.Zuul) SpringCloud入门 参考: https://springcloud.cc/spring-cloud- ...

  6. spring cloud 微服务调用--ribbon和feign调用

    这里介绍ribbon和feign调用两种通信服务调用方式,同时介绍如何引入第三方服务调用.案例包括了ribbon负载均衡和hystrix熔断--服务降级的处理,以及feign声明式服务调用.例子包括s ...

  7. 微服务之服务注册与发现--Eureka(附代码)

    该贴为入门贴,看完可快速知道服务注册与发现是什么?怎么用?至于深入的内容不在此篇文章所述之内,请自行百度. 内容来自:https://blog.csdn.net/nanbiebao6522/artic ...

  8. SpringCloud微服务基础 Eureka、Feign、Ribbon、Zuul、Hystrix、配置中心的基础使用

    1.单点系统架构 传统项目架构 传统项目分为三层架构,将业务逻辑层.数据库访问层.控制层放入在一个项目中. 优点:适合于个人或者小团队开发,不适合大团队开发. 分布式项目架构 根据业务需求进行拆分成N ...

  9. springcloud微服务实战:Eureka+Zuul+Feign/Ribbon+Hystrix Turbine+SpringConfig+sleuth+zipkin

    相信现在已经有很多小伙伴已经或者准备使用springcloud微服务了,接下来为大家搭建一个微服务框架,后期可以自己进行扩展.会提供一个小案例: 服务提供者和服务消费者 ,消费者会调用提供者的服务,新 ...

随机推荐

  1. day 13 生成器函数 表达式 推导式

    今日主要内容 1. 生成器和生成器函数 生成器的本质就是迭代器 生成器的三种创建办法: 1.通过生成器函数 2.通过生成器表达式创建生成器 3.通过数据转换 2. 生成器函数: 函数中包含了yield ...

  2. Debug 利器:pstack & strace

    工作中难免会遇到各种各样的 bug,对于开发环境 or 测试环境的问题还好解决,可以使用 gdb 打断点或者在代码中埋点来定位异常; 但是遇到线上的 bug 就很难受了,由于生产环境不能随意替换.中断 ...

  3. java面试常见题目

    JAVA相关基础知识面向对象的特征有哪些方面 1.抽象:抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分,暂时不用 ...

  4. 相关性不一定等于因果性:从 Yule-Simpson’s Paradox 讲起

    1. 两件事伴随发生,不代表他们之间有因果关系 - 从一些荒诞相关性案例说起 在日常生活和数据分析中,我们可以得到大量相关性的结论,例如: 输入X变量,有98%置信度得到Y变量 只要努力,就能成功 只 ...

  5. 二叉树的操作--C语言实现

    树是一种比较复杂的数据结构,它的操作也比较多.常用的有二叉树的创建,遍历,线索化,线索化二叉树的遍历,这些操作又可以分为前序,中序和后序.其中,二叉树的操作有递归与迭代两种方式,鉴于我个人的习惯,在这 ...

  6. WebMagic 实现爬虫入门教程

    本示例实现某电影网站最新片源名称列表及详情页下载地址的抓取. webmagic是一个开源的Java垂直爬虫框架,目标是简化爬虫的开发流程,让开发者专注于逻辑功能的开发. WebMagic 特点: 完全 ...

  7. .Net Core Web Api使用模型验证验证参数合法性

    在接口开发过程中免不了要去验证参数的合法性,模型验证就是帮助我们去验证参数的合法性,我们可以在需要验证的model属性上加上Data Annotations特性后就会自动帮我们在action前去验证输 ...

  8. 最新Navicat Premium12 破解方法,亲测可用

    1.下载Navicat Premium 官网https://www.navicat.com.cn/下载最新版本下载安装(文末,网盘地址有64位安装包和注册机下载) 2.激活Navicat Premiu ...

  9. 使用curl创建简单的性能监控工具

    cURL,全称Command Line URL viewer,是一种命令行工具,用来发送网络请求,然后得到和提取数据,显示在标准输出(stdout). 我们可以使用curl来获取网页的源码,显示头信息 ...

  10. ES6——async函数

    目录 1.async 函数是 Generator 函数的语法糖. 2.async函数对 Generator 函数的改进,体现在以下四点. 3.基本用法 一个获取股票报价的函数 指定多少毫秒后输出一个值 ...