springcloud(九):熔断器Hystrix和Feign的应用案例
因为 feign 中已经支持了 Hystrix ,所以在 Feign 中使用 Hystrix 时,不需要导包,也不需要在入口类上面增加额外的注解;
Feign 虽然支持了 Hystrix ,但是默认情况下是关闭的,需要在 配置文件配置
1.创建项目
2. 选择项目类型
3.选择项目名称,可以随便写,但是不能有大写
4.在最左侧菜单选择大项,中间列表会选择需要的组件,右侧是已选的组件列表
5.输入项目名和模块名
6.项目结构如下
7. 查看我们依赖的pom.xml 里面需要手动添加一个我们公共组件的依赖,因为controller里面要用到实体类
<dependencies>
<!--引入我们的公共组件-->
<dependency>
<groupId>cn.kgc</groupId>
<artifactId>eureka-common-school</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<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-openfeign</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
8.编辑我们的属性文件
#eureka的相关配置
#使用feign时报错Service id not legal hostname(xx_sss)
#原因是feign不支持下划线"_",支持"-",改成xx-sss即可
#spring.application.name表示当前微服务注册到Eureka Server中的名字,同事需要制定Eureka Server地址
spring.application.name=client-student-findstudata
server.port=8764
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
# 将feign集成的断路器设置成有效状态
feign.hystrix.enabled=true
9.在cn.kgc.feign包下编写StudentFeign业务接口,好多人在这里添加service注解,木有看懂,但是我这里也能拿到数据
package cn.kgc.feign;
import cn.kgc.vo.Classes;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping; import java.util.List;
import java.util.Map;
/*FeignClient的name属性值和eureka_client_product的appliaction属性文件找那个的spring.application.name保持一致
fallback属性指定容错处理类
springcloud默认已经为feign整合了hystrix,只要hystrix在项目中,使用feign就会
默认使用熔断器处理所欲的请求
熔断器模式类似于生活中的电路保险丝,当电流抄在可能银帆危险时就会自动断开,使用熔断器模式,
如果请求出现异常,所有的请求都会直接返回而不会等待或阻塞,这样可以减少资源的浪费。
熔断器还有一种半开的状态,当熔断器发现异常后悔进入半打开状态,此时会定时接受
一个请求来检测系统是否恢复,如果请求调用成功,代表系统已经恢复正常,救护关掉熔断器,
否则继续打开*/
@FeignClient(name="client-school-provider",fallback = StudentFeignFallBack.class)
public interface StudentFeign {
//下面的调用接口标准要和eureka-client-provider中的controller请求方法必须保持一致
@RequestMapping("/data.do")
public String stuData(); }
10..在cn.kgc.feign包下编写StudentFeignFallBack容错处理类
package cn.kgc.feign; import org.springframework.stereotype.Component; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; //容错处理类
@Component
public class StudentFeignFallBack implements StudentFeign{
@Override
public String stuData() {
return "服务器异常,请稍后在尝试登陆";
}
}
11.在cn.kgc.controller包下编写StudentController控制器类
package cn.kgc.controller; import cn.kgc.feign.StudentFeign;
import cn.kgc.vo.Classes;
import cn.kgc.vo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.List;
import java.util.Map; @RestController
public class StudentController {
@Autowired
private StudentFeign studentFeign; @RequestMapping("/studata.do")
public String showOptionsData(){
return studentFeign.stuData();
}
}
12.编辑启动类
package cn.kgc; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
//EnableFeignClient开启Feign声明式REST调用
//EnableEurekaClient开启注册中心客户端
@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
public class EurekaClientFindstudataApplication { public static void main(String[] args) {
SpringApplication.run(EurekaClientFindstudataApplication.class, args);
} }
13.先正常依次启动服务eureka-server,eureka-provider,eureka-client-findstudata ,然后查看注册中心,看下是否后2个客户端注册到服务中心
看到如下两个服务就说明启动注册成功啦
14.由于数据是由eureka-provider提供,eureka-client-findstudata 调用了eureka-provider 的数据,在以上服务正常启动的情况下,可以得到如下数据
15.同理,由于数据是由eureka-provider提供,现在停掉eureka-provider服务, 则此时我们eureka-client-findstudata 的数据调用不到,feign的熔断器发挥作用
此时我们的程序就走通啦!
总结::其实吧,Holly个人觉的用OOP的思想很好理解,就是我们在JavaOOP写代码的时候有try-cathch ,容错类就是调用某个方法报错是抛出异常的自定义信息类而已!也许这个想法不对,但是个人觉的原理类似,脖子要断了,回家睡美容觉了,如有问题,请QQ/微信:964918306
此帖子为原创
作者:红酒人生
转载请注明出处:https://www.cnblogs.com/holly8/p/11024097.html
springcloud(九):熔断器Hystrix和Feign的应用案例的更多相关文章
- springcloud(九):熔断器Hystrix和Feign的全套应用案例(二)
一.. 创建Eureka-Server 服务中心项目 1. 创建Eureka-Server 服务中心项目架构如下 2. pom.xml <dependencies> <depende ...
- 二、springcloud之熔断器hystrix
一.背景 雪崩效应 在微服务架构中通常会有多个服务层调用,基础服务的故障可能会导致级联故障,进而造成整个系统不可用的情况,这种现象被称为服务雪崩效应.服务雪崩效应是一种因“服务提供者”的不可用导致“服 ...
- SpringCloud之熔断器Hystrix及服务监控Dashboard
目的: 服务雪崩效应 服务熔断服务降级 Hystrix默认超时时间设置 Hystrix服务监控Dashboard 服务雪崩效应 雪崩效应就是一种不稳定的平衡状态也是加密算法的一种特征,它指明文 ...
- springcloud(九)-Feign使用Hystrix
前言 上一篇我们使用注解@HystrixCommond的fallbackMethod属性实现回退.然而,Feign是以接口形式工作的,它没有方法体,上一篇讲解的方式显然不适用于Feign. 那么Fei ...
- 跟我学SpringCloud | 第四篇:熔断器Hystrix
跟我学SpringCloud | 第四篇:熔断器Hystrix 1. 熔断器 服务雪崩 在正常的微服务架构体系下,一个业务很少有只需要调用一个服务就可以返回数据的情况,这种比较常见的是出现在demo中 ...
- springcloud费话之断路器(hystrix in feign)
目录: springcloud费话之Eureka基础 springcloud费话之Eureka集群 springcloud费话之Eureka服务访问(restTemplate) springcloud ...
- SpringCloud系列十六:Feign使用Hystrix
1. 回顾 上文讲解了使用注解@HystrixCommand的fallbackMethod属性实现回退.然而,Feign是以接口形式工作的, 它没有方法体,前文讲解的方式显然不适用与Feign. 事实 ...
- SpringCloud学习笔记(14)----Spring Cloud Netflix之Hystrix对Feign的支持
1. Hystrix对Feign的支持 添加Feign中IUserBiz的实现类HystrixFallBack: package com.wangx.cloud.springcloud02consum ...
- 一起来学Spring Cloud | 第五章:熔断器 ( Hystrix)
在微服务项目中,一个系统可以分割成很多个不同的服务模块,不同模块之间我们通常需要进行相互调用.springcloud中可以使用RestTemplate+Ribbon和Feign来调用(工作中基本都是使 ...
随机推荐
- 21. Ext中表格自适应高度
转自:https://blog.csdn.net/happy492/article/details/6401099 1. 下面的代码中width和height的初始值为tab的开始大小,当浏览器窗口变 ...
- 洛谷P3400 仓鼠窝(单调栈)
P3400 仓鼠窝 题目描述 萌萌哒的Created equal是一只小仓鼠,小仓鼠自然有仓鼠窝啦. 仓鼠窝是一个由n*m个格子组成的行数为n.列数为m的矩阵.小仓鼠现在想要知道,这个矩阵中有多少个子 ...
- 树网的核 2007年NOIP全国联赛提高组(floyed)
树网的核 2007年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description [问题描述]设 T= ...
- cookie使用详解
cookie是用来保存客户资料的好方法,与同样可以用来保存客户资料的 session不同的是,session是把资料保存在服务器端,而cookie是把资料保存在客户端,我们平常接触的最多的cookie ...
- 题解报告:poj 1426 Find The Multiple(bfs、dfs)
Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose ...
- javascript学习之Date对象
前几天学习了一下date对象,于是写了一个简单的时间显示放到博客页面里(位于右上角),类似这样的效果,时:分:秒 xxxx年xx月xx日. 下面来说一下具体实现步骤. 首先,既然date是一个对象,那 ...
- php函数的声明与使用
function 函数名(){ 函数体 } 一个函数是由3部分组成:声明(function 关键字).函数名(用来找到函数体的).函数体(封装的代码) 2.函数的优越性 代码重用性强.维护方便.提高开 ...
- 【工具】Github
项目目录结构设计与git远程仓库的建立 git码云仓库建立:在码云网站上新建组织和项目. 配置sshkey认证和公钥:命令行ssh-keygen -t rsa -C "xxxxx@xxxxx ...
- vue2.0路由(跳转和传参)经典介绍
声明式 <router-link :to="...">编程式router.push(...) router.push('home') / ...
- Java 基础入门随笔(5) JavaSE版——函数重载
1.函数 函数就是定义在类中具有特定功能的一段独立小程序,也称为方法. 定义函数的格式: 修饰符 返回值类型 函数名(参数类型 形式参数1,参数类型 形式参数2,...) { ...