因为 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