因为 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的应用案例的更多相关文章

  1. springcloud(九):熔断器Hystrix和Feign的全套应用案例(二)

    一.. 创建Eureka-Server 服务中心项目 1. 创建Eureka-Server 服务中心项目架构如下 2. pom.xml <dependencies> <depende ...

  2. 二、springcloud之熔断器hystrix

    一.背景 雪崩效应 在微服务架构中通常会有多个服务层调用,基础服务的故障可能会导致级联故障,进而造成整个系统不可用的情况,这种现象被称为服务雪崩效应.服务雪崩效应是一种因“服务提供者”的不可用导致“服 ...

  3. SpringCloud之熔断器Hystrix及服务监控Dashboard

    目的:     服务雪崩效应 服务熔断服务降级 Hystrix默认超时时间设置 Hystrix服务监控Dashboard 服务雪崩效应 雪崩效应就是一种不稳定的平衡状态也是加密算法的一种特征,它指明文 ...

  4. springcloud(九)-Feign使用Hystrix

    前言 上一篇我们使用注解@HystrixCommond的fallbackMethod属性实现回退.然而,Feign是以接口形式工作的,它没有方法体,上一篇讲解的方式显然不适用于Feign. 那么Fei ...

  5. 跟我学SpringCloud | 第四篇:熔断器Hystrix

    跟我学SpringCloud | 第四篇:熔断器Hystrix 1. 熔断器 服务雪崩 在正常的微服务架构体系下,一个业务很少有只需要调用一个服务就可以返回数据的情况,这种比较常见的是出现在demo中 ...

  6. springcloud费话之断路器(hystrix in feign)

    目录: springcloud费话之Eureka基础 springcloud费话之Eureka集群 springcloud费话之Eureka服务访问(restTemplate) springcloud ...

  7. SpringCloud系列十六:Feign使用Hystrix

    1. 回顾 上文讲解了使用注解@HystrixCommand的fallbackMethod属性实现回退.然而,Feign是以接口形式工作的, 它没有方法体,前文讲解的方式显然不适用与Feign. 事实 ...

  8. SpringCloud学习笔记(14)----Spring Cloud Netflix之Hystrix对Feign的支持

    1. Hystrix对Feign的支持 添加Feign中IUserBiz的实现类HystrixFallBack: package com.wangx.cloud.springcloud02consum ...

  9. 一起来学Spring Cloud | 第五章:熔断器 ( Hystrix)

    在微服务项目中,一个系统可以分割成很多个不同的服务模块,不同模块之间我们通常需要进行相互调用.springcloud中可以使用RestTemplate+Ribbon和Feign来调用(工作中基本都是使 ...

随机推荐

  1. bzoj 1646: [Usaco2007 Open]Catch That Cow 抓住那只牛【bfs】

    满脑子dp简直魔性 模拟题意bfs转移即可 #include<iostream> #include<cstdio> #include<queue> using na ...

  2. bzoj 2015: [Usaco2010 Feb]Chocolate Giving【spfa】

    因为是双向边,所以相当于两条到1的最短路和,先跑spfa然后直接处理询问即可 #include<iostream> #include<cstdio> #include<q ...

  3. 修复mysql的表

    数据损坏原因 MySQL表损坏一般是数据损坏,引起损坏的原因可能是由于磁盘损坏.系统崩溃或者MySQL服务器被崩溃等外部原因.例如有人使用kill -9终止进程,导致MySQL进程未能正常关闭,那么就 ...

  4. LOJ#503. 「LibreOJ β Round」ZQC 的课堂(容斥+FHQTreap)

    题面 传送门 题解 首先\(x\)和\(y\)两维互相独立,可以分开考虑,我们以\(x\)为例 我们把\(x\)做个前缀和,那么就是问有多少\(i\)满足\(s_is_{i-1}<0\),其中\ ...

  5. centos 7 配置php

    对于我们的目的而言,安装 Apache 只需要在 CentOS 命令终端敲入这条命令就行了: $ sudo yum install httpd $ sudo systemctl enable http ...

  6. 记录sql操作

    需求:一个a表的A列等于b表的B列 但拥有的相同列C列值不相同 需要将其改成一样的 UPDATE vd_auth_switch vas,tb_student ts set vas.class_id = ...

  7. service: no such service mysqld 与MySQL的开启,关闭和重启

    1.问题原因与解决办法 因为修改了MySQL临时文件的目录后,使用service mysqld restart重启MySQL出现如下错误: service: no such service mysql ...

  8. 向listview控件中添加数据库数据

    //连接字符串 string str = "Data Source=.;Initial Catalog=mu;User ID=sa;Password=111"; //创建数据库连接 ...

  9. EasyUI系列学习(七)-Linkbutton(按钮)

    一.加载组件 1.使用class加载 <a href="#" class="easyui-linkbutton">按钮</a> 2.使用 ...

  10. MacOS 下安装 MySQL8.0 登陆 MySQL

    按照 官方教程 ,下载安装包,点击安装后,如需在命令行启动,还需设置命令路径: 在命令行中,打开配置文件 .bash_profile: vim ~/.bash_profile 在最后一行加上: PAT ...