微服务中关于feign整合hystrix的案例使用以及注意事项

微服务中关于feign整合hystrix的案例使用以及注意事项

2018年12月19日 22:23:06 uniquewdl 阅读数:134

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.****.net/uniqueweimeijun/article/details/85108794

What Is Hystrix?

在一个分布式系统里,许多依赖不可避免的会调用失败,比如超时、异常等,如何能够保证在一个依赖出问题的情况下,不会导致整体服务失败,这个就是Hystrix需要做的事情。Hystrix提供了熔断、隔离、Fallback、cache、监控等功能,能够在一个、或多个依赖同时出现问题时保证系统依然可用。知道它的,作用优点和好处了。那么下边就跟着我一起来操作一下feign整合Hystrix。

 

1.创建feign项目

1.pom文件

 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  4. <modelVersion>4.0.0</modelVersion>

  5.  
  6. <artifactId>feign-customizing</artifactId>

  7. <packaging>jar</packaging>

  8.  
  9. <parent>

  10. <groupId>com.itmuch.cloud</groupId>

  11. <artifactId>microservice-spring-cloud</artifactId>

  12. <version>0.0.1-SNAPSHOT</version>

  13. </parent>

  14.  
  15. <properties>

  16. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  17. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

  18. <java.version>1.8</java.version>

  19. </properties>

  20.  
  21. <dependencies>

  22. <dependency>

  23. <groupId>org.springframework.boot</groupId>

  24. <artifactId>spring-boot-starter-web</artifactId>

  25. </dependency>

  26.  
  27. <dependency>

  28. <groupId>org.springframework.cloud</groupId>

  29. <artifactId>spring-cloud-starter-eureka</artifactId>

  30. </dependency>

  31.  
  32. <dependency>

  33. <groupId>org.springframework.boot</groupId>

  34. <artifactId>spring-boot-starter-actuator</artifactId>

  35. </dependency>

  36.  
  37. <dependency>

  38. <groupId>org.springframework.cloud</groupId>

  39. <artifactId>spring-cloud-starter-feign</artifactId>

  40. </dependency>

  41. <dependency>

  42. <groupId>org.springframework.cloud</groupId>

  43. <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>

  44. </dependency>

  45. <dependency>

  46. <groupId>org.springframework.cloud</groupId>

  47. <artifactId>spring-cloud-starter-hystrix</artifactId>

  48. </dependency>

  49. </dependencies>

  50. </project>

---------------pom文件的注意点就是一定要注意springboot的版本,版本不能太高。否则会报错找不到方法的异常。具体分析在后续博客中会详细分析。

 2.配置文件:

application.yml文件

 
  1. spring:

  2. application:

  3. name: microservice-consumer-movie

  4. server:

  5. port: 2000

  6. eureka:

  7. client:

  8. healthcheck:

  9. enabled: true

  10. serviceUrl:

  11. defaultZone: http://47.xxx.6.xxx:1000/eureka

  12. instance:

  13. prefer-ip-address: true

  14. logging:

  15. level:

  16. com.itmuch.cloud.feign.UserFeignClient: DEBUG

  17.  
  18.  

注意点:

#可以通过下边的配置设置超时时间,默认为一秒即为1000:

也可以通过下边的设置来更好的观察项目的效果
# hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
# 或者:
# hystrix.command.default.execution.timeout.enabled: false
# 或者:
feign.hystrix.enabled: true ## 索性禁用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

 3,启动类

Applicatoin

 
  1.  
  2.  
  3. import org.springframework.boot.SpringApplication;

  4. import org.springframework.boot.autoconfigure.SpringBootApplication;

  5. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

  6. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

  7. import org.springframework.cloud.netflix.feign.EnableFeignClients;

  8. import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

  9.  
  10. @SpringBootApplication

  11. @EnableDiscoveryClient

  12. @EnableFeignClients

  13. @EnableHystrixDashboard

  14. public class MyFeignApplication {

  15. public static void main(String[] args) {

  16. SpringApplication.run(MyFeignApplication.class, args);

  17. }

  18. }

注意点:四个注解缺一不可:

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrixDashboard

4.建立controller

 
  1. package com.itmuch.cloud.controller;

  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;

  4. import org.springframework.web.bind.annotation.GetMapping;

  5. import org.springframework.web.bind.annotation.PathVariable;

  6. import org.springframework.web.bind.annotation.RequestMapping;

  7.  
  8.  
  9. import com.itmuch.cloud.entity.User;

  10. import com.itmuch.cloud.feign.FeignClient2;

  11. import com.itmuch.cloud.feign.UserFeignClient;

  12.  
  13. @RestController

  14. public class TestController {

  15.  
  16. @Autowired

  17. private UserFeignClient userFeignClient;

  18.  
  19.  
  20. @RequestMapping("/index")

  21. public String index() {

  22. String str = userFeignClient.getContext();

  23. return str;

  24. }

  25. }

这里的controller是一个外部入口,通过请求index方法进而调用下边的feign方法,

5.feign的接口配置(重点)

 
  1.  
  2.  
  3. import org.springframework.cloud.netflix.feign.FeignClient;

  4. import org.springframework.web.bind.annotation.RequestMapping;

  5. import org.springframework.web.bind.annotation.RequestMethod;

  6.  
  7. @FeignClient(name = "feign-user", fallback = MyHystrix.class)

  8. public interface UserFeignClient {

  9. @RequestMapping(value="/getContext",method=RequestMethod.GET)

  10. public String getContext();

  11. }

注意点:@FeignClient(name = "feign-user", fallback = MyHystrix.class)   这里就是feign的核心配置,name属性是feign请求外部接口的服务名称(就是一同注册到注册中心的服务名称) fallback就是产生异常错误后的回退路径

要求fallback指向的类必须实现feign接口。然后再对应的接口的实现具体实现自定义 的异常处理(抛错,提示异常,回退等操作)。注意点2feign中 的方法注解不支持GetMapping  以及如果是@PathVariable一定要有值。

附加fallback的类

 
  1.  
  2.  
  3. import org.springframework.stereotype.Component;

  4.  
  5.  
  6. @Component

  7. public class MyHystrix implements UserFeignClient {

  8.  
  9. @Override

  10. public String getContext() {

  11. System.err.println("=======报错");

  12. return "报错了";

  13. }

  14.  
  15. }

说明,如果是UserFeignClient的某个方法由于超时,异常等非正常操作都会进入对应Hystrix方法中,这样更好地捕获异常和进行处理。

6.新建一个项目作为feign的请求项目,要求该项目也能够注册到注册中心上(eureka等)

下边不做详细说明核心代码如下:

7.pom文件

 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  4. <modelVersion>4.0.0</modelVersion>

  5.  
  6. <artifactId>movie-feign</artifactId>

  7. <packaging>jar</packaging>

  8.  
  9. <parent>

  10. <groupId>com.itmuch.cloud</groupId>

  11. <artifactId>microservice-spring-cloud</artifactId>

  12. <version>0.0.1-SNAPSHOT</version>

  13. </parent>

  14.  
  15. <properties>

  16. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  17. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

  18. <java.version>1.8</java.version>

  19. </properties>

  20.  
  21. <dependencies>

  22. <dependency>

  23. <groupId>org.springframework.boot</groupId>

  24. <artifactId>spring-boot-starter-web</artifactId>

  25. </dependency>

  26.  
  27. <dependency>

  28. <groupId>org.springframework.cloud</groupId>

  29. <artifactId>spring-cloud-starter-eureka</artifactId>

  30. </dependency>

  31.  
  32. <dependency>

  33. <groupId>org.springframework.boot</groupId>

  34. <artifactId>spring-boot-starter-actuator</artifactId>

  35. </dependency>

  36.  
  37. <dependency>

  38. <groupId>org.springframework.cloud</groupId>

  39. <artifactId>spring-cloud-starter-feign</artifactId>

  40. </dependency>

  41. </dependencies>

  42. </project>

 8.application.yml

 
  1. spring:

  2. application:

  3. name: provider-user

  4. server:

  5. port: 2001

  6. eureka:

  7. client:

  8. healthcheck:

  9. enabled: true

  10. serviceUrl:

  11. defaultZone: http://47.xxx.6.xxx:1000/eureka

  12. instance:

  13. prefer-ip-address: true

9.请求端controller

 
  1. package com.itmuch.cloud.controller;

  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;

  4. import org.springframework.web.bind.annotation.GetMapping;

  5. import org.springframework.web.bind.annotation.PathVariable;

  6. import org.springframework.web.bind.annotation.RequestMapping;

  7. import org.springframework.web.bind.annotation.RestController;

  8.  
  9. import com.itmuch.cloud.entity.User;

  10. import com.itmuch.cloud.feign.UserFeignClient;

  11.  
  12. @RestController

  13. public class MovieController {

  14.  
  15.  
  16.  
  17.  
  18.  
  19. /*

  20. * @GetMapping("/test") public User testPost(User user) { return

  21. * this.userFeignClient.postUser(); }

  22. *

  23. * @GetMapping("/test-get") public User testGet(User user) { return

  24. * this.userFeignClient.getUser(user); }

  25. */

  26. @RequestMapping("/getContext")

  27. public String getContext() {

  28. return "我是服务端的getContext方法";

  29. }

  30. }

10.最后来波效果图:

10-1eureka注册中心

微服务中关于feign整合hystrix的案例使用以及注意事项

1

10-2 失败结果

微服务中关于feign整合hystrix的案例使用以及注意事项

10-3成功

微服务中关于feign整合hystrix的案例使用以及注意事项