码上敲享录 > SpringCloud实战教程 > Springcloud在Feign中整合断路器Hystrix实现发生异常时友好提示

Springcloud在Feign中整合断路器Hystrix实现发生异常时友好提示

上一章章节目录 2020-06-23已有1870人阅读 评论(0)

各个章节可能有关联关系,查找《SpringCloud实战教程》其他章节请参考:

http://www.yayihouse.com/yayishuwu/book/79



7. SpringcloudFeign中整合断路器Hystrix实现发生异常时友好提示

feign客户端代码:

(1) Pom.xml依赖,feign整合了Hystrix,不用另外添加依赖:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
     <groupId>com.wlg.springcloud</groupId>
     <artifactId>springcloud</artifactId>
     <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupId>com.wlg.springcloud</groupId>
  <artifactId>euraka-client-feifn</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>euraka-client-feifn</name>
  <description>Demo project for Spring Boot</description>
  <dependencies>
     <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>
  </dependencies>
</project>

(2) application.yml:

server:
 port: 9903
spring:
 application:
   name: service-two
eureka:
 client:
   serviceUrl:
     defaultZone: http://localhost:9901/eureka/

(3) Feign接口加上fallback = FeignClientServiceImp.class

当用户访问sayHello传参数txt0时抛出异常,那么就会执行fallback返回信息给用户

@FeignClient(value = "service-one",fallback = FeignClientServiceImp.class)
public interface FeignClientService {
   @RequestMapping("/hello")
   public String sayHello(@RequestParam("txt")String txt);
   }

(4) FeignClientServiceImp内容如下,方法要和接口的方法一致:

public class FeignClientServiceImp {
   public String sayHello(String txt) {
       return "系统维护中,请稍后再试。。。";
   }
}

(1) 通过feign接口调用其他服务

@Controller
public class FeignClientController {
   @Autowired
   private FeignClientService feignClientService;

    @RequestMapping("/sayHello")
   public  void sayHello(String txt, HttpServletResponse response) throws IOException {
        String result = feignClientService.sayHello(txt);
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.print(result);
        writer.flush();
        writer.close();
    }

}

(2) 被调用的工程eureka-client被调用的方法(在注册中心的服务名为service-one,前面章节创建了)

@RequestMapping("/hello")
@ResponseBody
public  String sayHello(@RequestParam("txt") String txt) throws Exception {
    if("0".equals(txt)){
       throw new Exception();
    }
    return  "收到你传过来的参数为:"+txt;
}

(3) 演示:

启动前面章节创建的服务发现及注册中心eurekaServer启动被调用的服务eureka-client,启动feign客户端(端口9903

浏览器访问:http://localhost:9903/sayHello?txt=1

没异常事的结果:收到你传过来的参数为:1

浏览器访问:http://localhost:9903/sayHello?txt=0

有异常结果:收到你传过来的参数为:0,但系统正在维护中。。。

这就说明了hystrix捕捉并处理了异常


0

有建议,请留言!

  • *您的姓名:

  • *所在城市:

  • *您的联系电话:

    *您的QQ:

  • 咨询问题:

  • 提 交