• Spring Cloud 之 Feign集成Hystrix基本使用(十)


    上一篇讲了Ribbon集成Hystrix基本使用,本篇再说一下Feign是如何集成Hystrix,完成回退的。

    先把Eureka集群和服务提供者(x-demo-service)集群启动。

    x-demo-service依然不用改任何东西,下面开始对x-demo-service-feign进行改造。

    1、build.gradle配置文件加入Hystrix依赖

    1 dependencies {
    2     compile project(":x-demo-service-api")
    3 
    4     compile("org.springframework.cloud:spring-cloud-starter-netflix-eureka-client")
    5     compile("org.springframework.cloud:spring-cloud-starter-openfeign")
    6     compile("org.springframework.cloud:spring-cloud-starter-netflix-hystrix")
    7 }

    2、新建回退类,该类必须集成我们之前写的FeignClientDemoService接口

     1 package com.x.demo.feign.fallback;
     2 
     3 import com.x.demo.feign.service.FeignClientDemoService;
     4 import org.springframework.stereotype.Component;
     5 
     6 /**
     7  * @author Leo
     8  */
     9 @Component
    10 public class FeignClientDemoServiceFallback implements FeignClientDemoService {
    11     @Override
    12     public String service() {
    13         return "我是Feign,服务端大概率挂了!";
    14     }
    15 }

    3、FeignClientDemoService接口修改

    修改FeignClient注解配置,将fallback指向我们新建的回退类FeignClientDemoServiceFallback
     1 package com.x.demo.feign.service;
     2 
     3 import com.x.demo.feign.fallback.FeignClientDemoServiceFallback;
     4 import org.springframework.cloud.openfeign.FeignClient;
     5 import org.springframework.web.bind.annotation.RequestMapping;
     6 import org.springframework.web.bind.annotation.RequestMethod;
     7 
     8 /**
     9  * @author Leo
    10  */
    11 @FeignClient(value = "x-demo-service", fallback = FeignClientDemoServiceFallback.class)
    12 public interface FeignClientDemoService {
    13 
    14     @RequestMapping(value = "demo/service", method = RequestMethod.GET)
    15     String service();
    16 }

    4、启动服务

    启动类不用改任何东西

     1 /**
     2  * @author Leo
     3  */
     4 @SpringBootApplication
     5 @EnableEurekaClient
     6 @EnableFeignClients
     7 public class FeignServerApplication {
     8 
     9     public static void main(String[] args) {
    10         SpringApplication.run(FeignServerApplication.class, args);
    11     }
    12 }

    5、测试

    浏览器输入:http://localhost:8092/feign/service,多次刷新接口,返回:

    x-demo-service hello.8081

    x-demo-service hello.8082

    x-demo-service hello.8083

    将服务提供者x-demo-service Kill之后,刷新http://localhost:8092/feign/service,返回:

    我们看到接口返回了一个Whitelabel Error Page页面,并没有返回我们在回退类中输出的“我是Feign,服务端大概率挂了!”,这肯定不是我们想要的结果

    原因是我们还需要改一下配置文件,需要开启hystrix功能,将feign.hystrix.enabled设置为true。

     1 spring:
     2   application:
     3     name: x-demo-service-feign
     4 
     5 server:
     6   port: 8092
     7 
     8 eureka:
     9   client:
    10     serviceUrl:
    11       defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/
    12 
    13 feign:
    14   hystrix:
    15     enabled: true

    重启服务,刷新http://localhost:8092/feign/service,返回:

     至此,Feign基础hystrix完成。

  • 相关阅读:
    OpenStack IceHouse版cinder模块新添加功能
    最小代价生成树
    HDU 3065 病毒侵袭持续中(AC自己主动机)
    POJ--2284--That Nice Euler Circuit【平面图欧拉公式】
    java工具类(四)之实现日期随意跳转
    Metasploit学习笔记之——情报搜集
    POJ 2378 Tree Cutting 子树统计
    cocos2d-x 3.0 touch事件官方解释
    html_entity_decode() 将 HTML 实体转成字符原型
    微信公众平台开发(81) 2014新年微信贺卡
  • 原文地址:https://www.cnblogs.com/shileibrave/p/14440241.html
Copyright © 2020-2023  润新知