资讯专栏INFORMATION COLUMN

SpringCloud(第 017 篇)电影微服务接入Feign,添加fallbackFactory

singerye / 3246人阅读

摘要:添加访问远端用户微服务类客户端回退机制类。添加访问层添加电影微服务启动类电影微服务接入,添加属性来触发请求进行容灾降级。注解表示该电影微服务已经接入模块。

SpringCloud(第 017 篇)电影微服务接入Feign,添加 fallbackFactory 属性来触发请求进行容灾降级

-

一、大致介绍
1、在一些场景中,简单的触发在 FeignClient 加入 Fallback 属性即可,而另外有一些场景需要访问导致回退触发的原因,那么这个时候可以在 FeignClient 中加入 FallbackFactory 属性即可;
2、而在使用 Fallback 和 FallbackFactory 时候,我仅仅表述个人观点,发现二者混合一起用的话,会发生冲突情况,所以大家用的时候注重考虑一下场景,二者属性用其一即可。
二、实现步骤 2.1 添加 maven 引用包


    4.0.0

    springms-consumer-movie-feign-with-hystrix-factory
    1.0-SNAPSHOT
    jar

    
        com.springms.cloud
        springms-spring-cloud
        1.0-SNAPSHOT
    

    
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            org.springframework.cloud
            spring-cloud-starter-eureka
        

        
        
            org.springframework.cloud
            spring-cloud-starter-feign
        

        
        
            org.springframework.cloud
            spring-cloud-starter-hystrix
        
    

2.2 添加应用配置文件(springms-consumer-movie-feign-with-hystrix-factorysrcmainresourcesapplication.yml)
spring:
  application:
    name: springms-consumer-movie-feign-with-hystrix-factory
server:
  port: 8115
eureka:
  client:
#    healthcheck:
#      enabled: true
    serviceUrl:
      defaultZone: http://admin:admin@localhost:8761/eureka
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}


# 解决第一次请求报超时异常的方案,因为 hystrix 的默认超时时间是 1 秒,因此请求超过该时间后,就会出现页面超时显示 :
#
# 这里就介绍大概三种方式来解决超时的问题,解决方案如下:
#
# 第一种方式:将 hystrix 的超时时间设置成 5000 毫秒
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
#
# 或者:
# 第二种方式:将 hystrix 的超时时间直接禁用掉,这样就没有超时的一说了,因为永远也不会超时了
# hystrix.command.default.execution.timeout.enabled: false
#
# 或者:
# 第三种方式:索性禁用feign的hystrix支持
# feign.hystrix.enabled: false ## 索性禁用feign的hystrix支持

# 超时的issue:https://github.com/spring-cloud/spring-cloud-netflix/issues/768
# 超时的解决方案: http://stackoverflow.com/questions/27375557/hystrix-command-fails-with-timed-out-and-no-fallback-available
# hystrix配置: https://github.com/Netflix/Hystrix/wiki/Configuration#execution.isolation.thread.timeoutInMilliseconds
2.3 添加实体用户类User(springms-consumer-movie-feign-with-hystrix-factorysrcmainjavacomspringmscloudentityUser.java)
package com.springms.cloud.entity;

import java.math.BigDecimal;

public class User {

    private Long id;

    private String username;

    private String name;

    private Short age;

    private BigDecimal balance;

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Short getAge() {
        return this.age;
    }

    public void setAge(Short age) {
        this.age = age;
    }

    public BigDecimal getBalance() {
        return this.balance;
    }

    public void setBalance(BigDecimal balance) {
        this.balance = balance;
    }

}
2.4 添加访问远端用户微服务 Feign 客户端(springms-consumer-movie-feign-with-hystrix-factorysrcmainjavacomspringmscloudfeignUserFeignHystrixFactoryClient.java)
package com.springms.cloud.feign;

import com.springms.cloud.entity.User;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.*;

/**
 * 用户Http请求的客户端。
 *
 * 注解FeignClient的传参:表示的是注册到 Eureka 服务上的模块名称。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 2017/9/24
 *
 */
@FeignClient(name = "springms-provider-user", /*fallback = HystrixClientFallback.class,*/ fallbackFactory = HystrixClientFallbackFactory.class)
public interface UserFeignHystrixFactoryClient {

    /**
     * 这里有两个坑需要注意:
* *
  • 这里需要设置请求的方式为 RequestMapping 注解,用 GetMapping 注解是运行不成功的,即 GetMapping 不支持。
  • *
  • 注解 PathVariable 里面需要填充变量的名字,不然也是运行不成功的。
  • * * @param id * @return */ @RequestMapping(value = "/simple/{id}", method = RequestMethod.GET) public User findById(@PathVariable("id") Long id); }
    2.5 添加访问远端用户微服务 Fallback 类(springms-consumer-movie-feign-with-hystrix-factorysrcmainjavacomspringmscloudfeignHystrixClientFallback.java)
    package com.springms.cloud.feign;
    
    import com.springms.cloud.entity.User;
    import org.springframework.stereotype.Component;
    
    /**
     * Hystrix 客户端回退机制类。
     *
     * 这里加上注解 Component 的目的:就是因为没有这个注解,运行时候会报错,报错会说没有该类的这个实例,所以我们就想到要实例化这个类,因此加了这个注解。
     *
     * @author hmilyylimh
     *
     * @version 0.0.1
     *
     * @date 2017/9/24
     *
     */
    @Component
    public class HystrixClientFallback implements UserFeignHystrixFactoryClient {
    
        @Override
        public User findById(Long id) {
    
            System.out.println("======== findById Fallback " + Thread.currentThread().getThreadGroup() + " - " + Thread.currentThread().getId() + " - " + Thread.currentThread().getName());
    
            User tmpUser = new User();
            tmpUser.setId(0L);
            return tmpUser;
        }
    }
    2.6 添加访问远端用户微服务 FallbackFactory 类(springms-consumer-movie-feign-with-hystrix-factorysrcmainjavacomspringmscloudfeignHystrixClientFallbackFactory.java)
    package com.springms.cloud.feign;
    
    import com.springms.cloud.entity.User;
    import feign.hystrix.FallbackFactory;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component;
    
    /**
     * Hystrix 客户端回退机制类。
     *
     * 这里加上注解 Component 的目的:就是因为没有这个注解,运行时候会报错,报错会说没有该类的这个实例,所以我们就想到要实例化这个类,因此加了这个注解。
     *
     * @author hmilyylimh
     *
     * @version 0.0.1
     *
     * @date 2017/9/24
     *
     */
    @Component
    public class HystrixClientFallbackFactory implements FallbackFactory {
    
        private static final Logger Logger = LoggerFactory.getLogger(HystrixClientFallbackFactory.class);
    
        @Override
        public UserFeignHystrixFactoryClient create(Throwable e) {
    
            Logger.info("fallback; reason was: {}", e.getMessage());
            System.out.println("======== UserFeignHystrixFactoryClient.create " + Thread.currentThread().getThreadGroup() + " - " + Thread.currentThread().getId() + " - " + Thread.currentThread().getName());
    
            return new UserFeignWithFallBackFactoryClient(){
    
                @Override
                public User findById(Long id) {
                    System.out.println("======== findById FallBackFactory " + Thread.currentThread().getThreadGroup() + " - " + Thread.currentThread().getId() + " - " + Thread.currentThread().getName());
    
                    User tmpUser = new User();
                    tmpUser.setId(-1L);
                    return tmpUser;
                }
            };
        }
    }
    
    /****************************************************************************************
     @FeignClient(name = "hello", fallbackFactory = HystrixClientFallbackFactory.class)
     protected interface HystrixClient {
         @RequestMapping(method = RequestMethod.GET, value = "/hello")
         Hello iFailSometimes();
     }
    
     @Component
     static class HystrixClientFallbackFactory implements FallbackFactory {
         @Override
         public HystrixClient create(Throwable cause) {
            return new HystrixClientWithFallBackFactory() {
                 @Override
                 public Hello iFailSometimes() {
                    return new Hello("fallback; reason was: " + cause.getMessage());
                }
            };
         }
     }
     ****************************************************************************************/
    2.7 添加回退处理客户端类(springms-consumer-movie-feign-with-hystrix-factorysrcmainjavacomspringmscloudfeignUserFeignWithFallBackFactoryClient.java)
    package com.springms.cloud.feign;
    
    /**
     * 回退处理客户端。
     *
     * @author hmilyylimh
     *
     * @version 0.0.1
     *
     * @date 2017/9/24
     *
     */
    public interface UserFeignWithFallBackFactoryClient extends UserFeignHystrixFactoryClient{
    }
    2.8 添加Web访问层Controller(springms-consumer-movie-feign-with-hystrix-factorysrcmainjavacomspringmscloudcontrollerMovieFeignHystrixFactoryController.java)
    package com.springms.cloud.controller;
    
    import com.springms.cloud.entity.User;
    import com.springms.cloud.feign.UserFeignHystrixFactoryClient;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class MovieFeignHystrixFactoryController {
    
        @Autowired
        private UserFeignHystrixFactoryClient userFeignHystrixFactoryClient;
    
        @GetMapping("/movie/{id}")
        public User findById(@PathVariable Long id) {
            System.out.println("======== findById Controller " + Thread.currentThread().getThreadGroup() + " - " + Thread.currentThread().getId() + " - " + Thread.currentThread().getName());
            return userFeignHystrixFactoryClient.findById(id);
        }
    }
    2.9 添加电影微服务启动类(springms-consumer-movie-feign-custom-without-hystrixsrcmainjavacomspringmscloudMsConsumerMovieFeignCustomWithoutHystrixApplication.java)
    package com.springms.cloud;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.cloud.netflix.feign.EnableFeignClients;
    
    /**
     * 电影微服务接入Feign,添加 fallbackFactory 属性来触发请求进行容灾降级。
     *
     * Feign: Java HTTP 客户端开发的工具。
     *
     * 注解 EnableFeignClients 表示该电影微服务已经接入 Feign 模块。
     *
     * @author hmilyylimh
     *
     * @version 0.0.1
     *
     * @date 2017/9/24
     *
     */
    @SpringBootApplication
    @EnableEurekaClient
    @EnableFeignClients
    public class MsConsumerMovieFeignHystrixFactoryApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(MsConsumerMovieFeignHystrixFactoryApplication.class, args);
            System.out.println("【【【【【【 电影Feign-HystrixFactory微服务 】】】】】】已启动.");
        }
    }
    三、测试
    /****************************************************************************************
     一、电影微服务接入Feign,添加 fallbackFactory 属性来触发请求进行容灾降级(测试正常接入功能):
    
     1、注解:EnableFeignClients;
     2、编写类 HystrixClientFallbackFactory 回退处理机制类,并给该类加上注解 Component ;加入 FeignClient 注解
         // @FeignClient(name = "springms-provider-user", fallback = HystrixClientFallback.class  )
     3、启动 springms-discovery-eureka 模块服务,启动1个端口;
     4、启动 springms-provider-user 模块服务,启动1个端口;
     5、启动 springms-consumer-movie-feign-with-hystrix-factory 模块服务;
     6、在浏览器输入地址 http://localhost:8115/movie/1 可以看到具体的用户信息(即用户ID != 0 的用户)成功的被打印出来;
     ****************************************************************************************/
    
    /****************************************************************************************
     二、电影FeignHystrix-HystrixFactory微服务接入 HystrixFactory 功能模块(测试断路器功能):
    
     1、注解:EnableFeignClients;
     2、编写类 HystrixClientFallbackFactory 回退处理机制类,并给该类加上注解 Component,UserFeignHystrixFactoryClient 加上 fallbackFactory 属性;
         // @FeignClient(name = "springms-provider-user", fallback = HystrixClientFallback.class, fallbackFactory = HystrixClientFallbackFactory.class )
     3、启动 springms-discovery-eureka 模块服务,启动1个端口;
     4、启动 springms-provider-user 模块服务,启动1个端口;
     5、启动 springms-consumer-movie-feign-with-hystrix-factory 模块服务;
     6、在浏览器输入地址 http://localhost:8115/movie/1 可以看到具体的用户信息(即用户ID != 0 的用户)成功的被打印出来;
    
     7、停止 springms-provider-user 模块服务;
     8、在浏览器输入地址http://localhost:8115/movie/1 可以看到用户信息ID = 0 的用户成功的被打印出来,但随着问题也来了;
     9、HystrixClientFallbackFactory 截获的异常却没有被打印出来,本来用户微服务停止的话,请求链接就已经链接超时了,但是为啥异常没有打印出来呢?请看下面第三中测试方法。
     ****************************************************************************************/
    
    /****************************************************************************************
     三、电影FeignHystrix-HystrixFactory微服务接入 HystrixFactory 功能模块(测试断路器功能):
    
     1、注解:EnableFeignClients;
     2、编写类 HystrixClientFallbackFactory 回退处理机制类,并给该类加上注解 Component,UserFeignHystrixFactoryClient 去掉 fallback 属性,然后加上 fallbackfactory 属性;
         // @FeignClient(name = "springms-provider-user", fallbackFactory = HystrixClientFallbackFactory.class )
     3、启动 springms-discovery-eureka 模块服务,启动1个端口;
     4、启动 springms-provider-user 模块服务,启动1个端口;
     5、启动 springms-consumer-movie-feign-with-hystrix-factory 模块服务;
     6、在浏览器输入地址 http://localhost:8115/movie/1 可以看到具体的用户信息(即用户ID != 0 的用户)成功的被打印出来;
    
     7、停止 springms-provider-user 模块服务;
     8、在浏览器输入地址http://localhost:8115/movie/1 可以看到用户信息ID = -1 的用户成功的被打印出来,而且异常信息日志也被打印出来了,这就正常了;
    
     注意:第2步骤:UserFeignHystrixFactoryClient 去掉 fallback 属性,然后加上 fallbackfactory 属性;
           所以这里目前暂时谨记,fallback 和 fallbackfactory 属性会有冲突,所以只要其一就行了;
     ****************************************************************************************/
    四、下载地址

    https://gitee.com/ylimhhmily/SpringCloudTutorial.git

    SpringCloudTutorial交流QQ群: 235322432

    SpringCloudTutorial交流微信群: 微信沟通群二维码图片链接

    欢迎关注,您的肯定是对我最大的支持!!!

    文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

    转载请注明本文地址:https://www.ucloud.cn/yun/70556.html

    相关文章

    • SpringCloud 012 服务接入 Feign 负载均衡通过 FeignClient

      摘要:添加电影微服务启动类电影微服务接入进行客户端负载均衡,通过调用远程微服务。注解表示该电影微服务已经接入模块。 SpringCloud(第 012 篇)电影微服务接入 Feign 进行客户端负载均衡,通过 FeignClient 调用远程 Http 微服务 - 一、大致介绍 1、本章节主要介绍在 SpringCloud 生态圈中,使用一个类似于 Java HTTP 客户端的工具 Feig...

      Cobub 评论0 收藏0
    • SpringCloud 013 电影服务使用定制化Feign负载均衡调度并为Feign配置

      摘要:不和在同级目录,因为文档有说明,该配置文件不需要被扫描到。添加访问层自定义控制器。添加电影微服务启动类电影微服务使用定制化在客户端进行负载均衡调度并为配置帐号密码登录认证。注解表示该电影微服务已经接入模块。 SpringCloud(第 013 篇)电影微服务使用定制化 Feign 在客户端进行负载均衡调度并为 Feign 配置帐号密码登录认证 Eureka - 一、大致介绍 1、定制 ...

      cloud 评论0 收藏0
    • SpringCloud 016 电影服务, 定制Feign,一个Feign功能禁用而另一个

      摘要:在该配置中,加入这个方法的话,表明使用了该配置的地方,就会禁用该模块使用容灾降级的功能添加访问层添加电影微服务启动类电影微服务,定制,一个功能禁用,另一个功能启用。 SpringCloud(第 016 篇)电影微服务,定制Feign,一个Feign功能禁用Hystrix,另一个Feign功能启用Hystrix - 一、大致介绍 1、在一些场景中,部分功能需要使用断路器功能,部分功能不需...

      张宪坤 评论0 收藏0
    • springcloud-feign实现服务发现

      摘要:本文重点介绍一下基于实现服务发现。使用方式下面我们开始的使用添加和的依赖添加注解开启服务发现,注解支持客户端。同样子,他可以使用默认的也可以使用或者修改配置文件服务名字服务无端口会随机选择一个服务集群名字注册中心地址,完成。 springcloud-feign实现服务发现 上一篇介绍了nacos实现配置和注册中心,在微服务中只有配置和注册中心远远不够,还需要有服务发现。本文重点介绍一...

      _DangJin 评论0 收藏0
    • SpringCloud学习(3)

      摘要:服务雪崩效应是一种因服务提供者的不可用导致服务消费者的不可用并将不可用逐渐放大的过程。这种代理能够记录最近调用发生错误的次数,然后决定使用允许操作继续,或者立即返回错误。这个自己持有的上下文默认实现类也是。      本篇集成Hystrix,继续搭建demo。 雪崩效应:在微服务架构中通常会有多个服务层调用,基础服务的故障可能会导致级联故障,进而造成整个系统不可用的情况,这种现象被称为...

      monw3c 评论0 收藏0

    发表评论

    0条评论

    singerye

    |高级讲师

    TA的文章

    阅读更多
    最新活动
    阅读需要支付1元查看
    <