java技术--SpringCloud:Hystrix代码实现(12)

1.Hystrix概念回顾:

(1)Hystrix是一个用于分布式系统的延迟和容错的开源库
(2)在分布式系统里,许多依赖不可避免的调用失败,比如超时、异常等
(3)Hystrix能够保证在一个依赖出问题的情况下,不会导致整个服务失败,避免级联故障
(4)提高分布式系统的弹性

2.准备工作

(1)启动EurekaServer工程,端口8000
(2)启动EurekaClient工程,端口8001,8002

3.Ribbon使用断路器Hystrix

(1)改造RibbonService工程的代码
(2)首先在pox.xml文件中加入spring-cloud-starter-netflix-hystrix的起步依赖
    <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>
(3)程序的启动类RibbonServiceApplication加@EnableHystrix注解开启Hystrix
    @SpringBootApplication
    @EnableEurekaClient
    //通过@EnableDiscoveryClient指向服务中心注册
    @EnableDiscoveryClient
    /**
     * 通过@EnableHystrix或@EnableCircuitBreaker注解开启Hystrix
     * 1.@EnableHystrix注解的作用和@EnableCircuitBreaker注解的作用一样
     * 2.@EnableHystrix注解对@EnableCircuitBreaker注解进行了封装
     */
    @EnableHystrix
    public class RibbonServiceApplication {
	  public static void main(String[] args) {
		SpringApplication.run(RibbonServiceApplication.class, args);}
	    // 向程序的IOC注入一个bean: restTemplate
	    @Bean
	   // 通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能
	   @LoadBalanced
	   RestTemplate restTemplate() {return new RestTemplate();}}  
(4)改造HelloRibbonService类,在hiService方法上加上@HystrixCommand注解
    @Service
    public class HelloRibbonService {
	   @Resource
	   private RestTemplate restTemplate;
	   /**
	    *该注解对该方法创建了熔断器的功能,并指定了fallbackMethod熔断方法
	    *熔断方法直接返回了一个字符串,字符串为”hi,”+name+”,sorry,error!”
	    */
	   @HystrixCommand(fallbackMethod = "hiError")
	   public String hiService(String name) {
         return restTemplate.getForObject("http://EURKACLIENT/hi?name="+name,String.class);}
       public String hiError(String name) {
         return "hi,"+name+",sorry,error!";}}
(5)启动:RibbonService工程
   <1>访问http://localhost:8003/hi?name=张三
   <2>浏览器显示:hi 张三,i am from port:8002或8001
(6)关闭:RibbonService工程
   <1>当再访问http://localhost:8003/hi?name=张三
   <2>浏览器会显示:hi ,张三,orry,error!  
     2.1.这就说明当RibbonService工程不可用的时候
     2.2.RibbonService调用EurekaClient的API接口时,会执行快速失败
     2.3.直接返回一组字符串,而不是等待响应超时,这很好的控制了容器的线程阻塞        

4.Feign中使用断路器Hystrix

(1)Feign是自带断路器的,在D版本的SpringCloud之后,它没有默认打开
   <1>Spring Cloud没有数字版本号,而是对应一个开发代号
   <2>开发代号是按首字母顺序的
      2.1.Dalston版本,可以简称 D 版本,SpringBoot对应1.5.x
      2.2.Edgware 版本,可以简称 E 版本,SpringBoot对应1.5.x
      2.3.Finchley版本,可以简称F版本,SpringBoot对应2.x
(2)在pom.xml配置文件中引入Hystrix依赖
   <1>虽然Feign是自带断路器的,但经本人测试不引入Hystrix依赖无法进入Feign工程
   <2>并且访问调用接口无法正常访问会直接进入熔断机制提示界面
   <3>引入pom.xml代码示例如下:   
   <!-- feign 已经依赖hystrix core 包 -->
   <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>  
(2)需要在配置文件中配置打开它,在配置文件加以下代码:
      feign.hystrix.enabled=true   
(3)基于FeignService工程进行改造 
(4)改造FeignService工程的IHelloFeignService接口类
   <1>@FeignClient注解中添加属性fallback
   <2>属性fallback加上指定类(接口实现类)就行了 
   <3>代码示例如下:
   /**
    * 通过@FeignClient(“服务名”),来指定调用哪个服务
    * 这里的服务名:就是EurekaClient配置文件application.properties中指定的服务名
    * 例如:spring.application.name:EurkaClient
    */
   @FeignClient(value = "EurkaClient",fallback = HelloFeignService.class)
   public interface IHelloFeignService {
	 @RequestMapping(value = "/hi",method = RequestMethod.GET)
	 String sayHiFromClientOne(@RequestParam(value = "name") String name);}
(5)编写IHelloFeignService接口类的实现类HelloFeignService
   <1>并将HelloFeignService注入到Ioc容器中
     1.1.接口实现类作用是:在里面编写当系统发生故障时返回给客户端友情提示的信息
   <2>实现类HelloFeignService代码示例如下:
   @Component
   public class HelloFeignService implements IHelloFeignService{
     @Override
     public String sayHiFromClientOne(String name) {
        return "sorry "+name;}} 
(6)启动FeignService工程
   <1>浏览器打开http://localhost:8004/hi?name=张三
   <2>EurekaClient工程没有启动,网页显示:sorry 张三
   <3>打开EurekaClient工程,再次访问,网页显示:hi 张三,i am from port:8001或8002  
   <4>这证明断路器起到作用了    

5.Ribbon使用Hystrix监视器

(1)hystrix-dashboard基本都是和turbine一起使用的
         <1>hystrix-dashboard只能监控单体应用,而turbine可以监控多个
         <2>HystrixDashboard是对微服务监控的可视化界面展示
(2)pom.xml引入依赖,代码如下:
     <!-- 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</artifactId>
     </dependency>
     <!--hytrix dashbord组件依赖:仪表盘 -->
     <dependency>
        <groupId>org.springframework.cloud</groupId
        <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
     </dependency>
(3)主程序启动类中加入@EnableHystrixDashboard注解,开启hystrixDashboard:
(4)启动工程,打开浏览器:访问http://localhost:8003/hystrix  
    <1>会进入监控界面仪表盘,仪表盘界面有一个小狗头像
    <2>在监控界面的http处输入访问网址,则会进入监控详情界面
(5)如果不想查看监控情况,则直接输入访问路径即可和Ribbon中使用的Hystrix访问形式
(6)参考文档:https://www.jianshu.com/p/f40acb0d01b2    

5.Ribbon和Feign中使用Hystrix区别

(1)在Ribbon中使用断路器Hystrix需要改造的地方比较多
    <1>在pox.xml文件中加入spring-cloud-starter-netflix-hystrix的起步依
    <2>启动类RibbonServiceApplication加@EnableHystrix注解开启Hystrix
    <3>在Service层中
      3.1.改造HelloRibbonService类
      3.2.在hiService方法上加上@HystrixCommand注解
       3.2.1.该注解对该方法创建了熔断器的功能,并指定了fallbackMethod熔断方法
	   3.2.2.熔断方法直接返回了一个字符串,字符串可自定义用于提示
	<4>改造完毕,启动:RibbonService工程   
(2)在Feign中使用断路器Hystrix需要改造的地方
    <1>Feign是自带断路器的,在D版本的SpringCloud之后,它没有默认打开
    <2>需在配置文件加以下代码打开:feign.hystrix.enabled=true 
    <3>在FeignService工程的接口类@FeignClient注解中添加属性fallback
     3.1.属性fallback加上指定类(指定类就是接口实现类)
    <4>添加编写接口类的实现类
     4.1.并将接口实现类通过注解@Component注入到Ioc容器中
     4.2.接口实现类作用是:在里面编写当系统发生故障时返回给客户端友情提示的信息
(3)Ribbon和Feign中使用Hystrix相同点
   <1>在启动Ribbon和Feign工程前,都要先启动EurekaServer和EurekaClient工程     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值