SpringCloud(第 016 篇)电影微服务,定制Feign,一个Feign功能禁用Hystrix,另一个Feign功能启用Hystrix
一、大致介绍
11、在一些场景中,部分功能需要使用断路器功能,部分功能不需要断路器功能,所以才有了本章节的介绍; 22、定制 Feign 的时候,可以为 Feign 设置 Configuration 配置,在该配置中可以设置是否需要断路器功能; 33、该定制的 Configuration 在官方文件介绍中特意提到不需要被启动类所在的包下,因为该配置类不不要被扫描到; 44、当然,此章节定制后的 Feign 同样支持客户端负载均衡功能,只要开启多个用户微服务即可; 55、这里提到为什么开启多个微服务就能做到客户端负载均衡呢? 6 - 简答的说就是 Feign 可以看作仅仅只是一个请求网络已被封装好的客户端HTTP请求工具类,将发出去的请求封装好; 7 - 至于要请求到哪台远端微服务的话,剩下的就交给 Ribbon 来处理了,而且默认集成使用了负载均衡的Ribbon客户端; 8 - 自然而然添加多个用户微服务,Feign自然就支持负载均衡的功能。
二、实现步骤
2.1 添加 maven 引用包
1<?xml version="1.0" encoding="UTF-8"?> 2<project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <artifactId>springms-consumer-movie-feign-custom-without-hystrix</artifactId> 8 <version>1.0-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <parent> 12 <groupId>com.springms.cloud</groupId> 13 <artifactId>springms-spring-cloud</artifactId> 14 <version>1.0-SNAPSHOT</version> 15 </parent> 16 17 <dependencies> 18 <!-- web模块 --> 19 <dependency> 20 <groupId>org.springframework.boot</groupId> 21 <artifactId>spring-boot-starter-web</artifactId> 22 </dependency> 23 24 <!-- 客户端发现模块 --> 25 <dependency> 26 <groupId>org.springframework.cloud</groupId> 27 <artifactId>spring-cloud-starter-eureka</artifactId> 28 </dependency> 29 30 <!-- Java HTTP 客户端开发的工具的模块 --> 31 <dependency> 32 <groupId>org.springframework.cloud</groupId> 33 <artifactId>spring-cloud-starter-feign</artifactId> 34 </dependency> 35 36 <!-- Hystrix 断路器模块 --> 37 <dependency> 38 <groupId>org.springframework.cloud</groupId> 39 <artifactId>spring-cloud-starter-hystrix</artifactId> 40 </dependency> 41 </dependencies> 42 43</project> 44
2.2 添加应用配置文件(springms-consumer-movie-feign-custom-without-hystrix\src\main\resources\application.yml)
1spring: 2 application: 3 name: springms-consumer-movie-feign-custom-without-hystrix 4server: 5 port: 8110 6eureka: 7 client: 8# healthcheck: 9# enabled: true 10 serviceUrl: 11 defaultZone: http://admin:admin@localhost:8761/eureka 12 instance: 13 prefer-ip-address: true 14 instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}} 15 16logging: 17 level: 18 com.springms.cloud.feign.UserFeignCustomClient: DEBUG 19 20# 解决第一次请求报超时异常的方案,因为 hystrix 的默认超时时间是 1 秒,因此请求超过该时间后,就会出现页面超时显示 : 21# 22# 这里就介绍大概三种方式来解决超时的问题,解决方案如下: 23# 24# 第一种方式:将 hystrix 的超时时间设置成 5000 毫秒 25hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000 26# 27# 或者: 28# 第二种方式:将 hystrix 的超时时间直接禁用掉,这样就没有超时的一说了,因为永远也不会超时了 29# hystrix.command.default.execution.timeout.enabled: false 30# 31# 或者: 32# 第三种方式:索性禁用feign的hystrix支持 33# feign.hystrix.enabled: false ## 索性禁用feign的hystrix支持 34 35# 超时的issue:https://github.com/spring-cloud/spring-cloud-netflix/issues/768 36# 超时的解决方案: http://stackoverflow.com/questions/27375557/hystrix-command-fails-with-timed-out-and-no-fallback-available 37# hystrix配置: https://github.com/Netflix/Hystrix/wiki/Configuration#execution.isolation.thread.timeoutInMilliseconds
2.3 添加实体用户类User(springms-consumer-movie-feign-custom-without-hystrix\src\main\java\com\springms\cloud\entity\User.java)
1package com.springms.cloud.entity; 2 3import java.math.BigDecimal; 4 5public class User { 6 7 private Long id; 8 9 private String username; 10 11 private String name; 12 13 private Short age; 14 15 private BigDecimal balance; 16 17 public Long getId() { 18 return this.id; 19 } 20 21 public void setId(Long id) { 22 this.id = id; 23 } 24 25 public String getUsername() { 26 return this.username; 27 } 28 29 public void setUsername(String username) { 30 this.username = username; 31 } 32 33 public String getName() { 34 return this.name; 35 } 36 37 public void setName(String name) { 38 this.name = name; 39 } 40 41 public Short getAge() { 42 return this.age; 43 } 44 45 public void setAge(Short age) { 46 this.age = age; 47 } 48 49 public BigDecimal getBalance() { 50 return this.balance; 51 } 52 53 public void setBalance(BigDecimal balance) { 54 this.balance = balance; 55 } 56 57}
2.4 添加访问远端用户微服务 Feign 客户端(springms-consumer-movie-feign-custom-without-hystrix\src\main\java\com\springms\cloud\feign\UserFeignCustomClient.java)
1package com.springms.cloud.feign; 2 3import com.springms.cloud.entity.User; 4import com.springms.config.TestFeignCustomConfiguration; 5import feign.Param; 6import feign.RequestLine; 7import org.springframework.cloud.netflix.feign.FeignClient; 8 9/** 10 * 用户Http请求的客户端,FeignClient 注解地方采用了自定义的配置。 11 * 12 * 注解FeignClient的传参:表示的是注册到 Eureka 服务上的模块名称。 13 * 14 * @author hmilyylimh 15 * 16 * @version 0.0.1 17 * 18 * @date 2017/9/24 19 * 20 */ 21@FeignClient(name = "springms-provider-user", configuration = TestFeignCustomConfiguration.class, fallback = UserFeignCustomClientFallback.class) 22public interface UserFeignCustomClient { 23 24 /** 25 * 这里的注解 RequestLine、Param 是 Feign 的配置新的注解,详细请参考链接:https://github.com/OpenFeign/feign 26 * 27 * @param id 28 * @return 29 */ 30 @RequestLine("GET /simple/{id}") 31 public User findById(@Param("id") Long id); 32} 33 34 35/**************************************************************************************** 36 参考代码如下: 37 38 interface GitHub { 39 @RequestLine("GET /repos/{owner}/{repo}/contributors") 40 List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo); 41 } 42 43 static class Contributor { 44 String login; 45 int contributions; 46 } 47 48 public static void main(String... args) { 49 GitHub github = Feign.builder().decoder(new GsonDecoder()).target(GitHub.class, "https://api.github.com"); 50 51 // Fetch and print a list of the contributors to this library. 52 List<Contributor> contributors = github.contributors("OpenFeign", "feign"); 53 for (Contributor contributor : contributors) { 54 System.out.println(contributor.login + " (" + contributor.contributions + ")"); 55 } 56 } 57 ****************************************************************************************/
2.5 添加登录认证Eureka服务 Feign 客户端(springms-consumer-movie-feign-custom-without-hystrix\src\main\java\com\springms\cloud\feign\UserFeignCustomSecondClient.java)
1package com.springms.cloud.feign; 2 3import com.springms.config.TestEurekaAuthConfiguration; 4import org.springframework.cloud.netflix.feign.FeignClient; 5import org.springframework.web.bind.annotation.PathVariable; 6import org.springframework.web.bind.annotation.RequestMapping; 7 8/** 9 * 用户Http请求的客户端,FeignClient 注解地方采用了自定义的配置。 10 * 11 * 注解FeignClient的传参:表示的是注册到 Eureka 服务上的模块名称。 12 * 13 * @author hmilyylimh 14 * 15 * @version 0.0.1 16 * 17 * @date 2017/9/24 18 * 19 */ 20@FeignClient(name = "xxx", url = "http://localhost:8761/", configuration = TestEurekaAuthConfiguration.class, fallback = UserFeignCustomSecondClientFallback.class) 21public interface UserFeignCustomSecondClient { 22 23 @RequestMapping(value = "/eureka/apps/{serviceName}") 24 public String findEurekaInfo(@PathVariable("serviceName") String serviceName); 25}
2.6 添加访问远端用户微服务 Fallback 类(springms-consumer-movie-feign-custom\src\main\java\com\springms\config\TestFeignCustomConfiguration.java)
1package com.springms.cloud.feign; 2 3import com.springms.cloud.entity.User; 4import org.springframework.stereotype.Component; 5 6/** 7 * Hystrix 客户端回退机制类。 8 * 9 * 这里加上注解 Component 的目的:就是因为没有这个注解,运行时候会报错,报错会说没有该类的这个实例,所以我们就想到要实例化这个类,因此加了这个注解。 10 * 11 * @author hmilyylimh 12 * 13 * @version 0.0.1 14 * 15 * @date 2017/9/24 16 * 17 */ 18@Component 19public class UserFeignCustomClientFallback implements UserFeignCustomClient{ 20 21 /** 22 * Fallback 回退降级的方法,返回一个默认的用户信息。 23 * 24 * @param id 25 * @return 26 */ 27 @Override 28 public User findById(Long id) { 29 System.out.println("========== findById Fallback " + Thread.currentThread().getThreadGroup() + " - " + Thread.currentThread().getId() + " - " + Thread.currentThread().getName()); 30 31 User tmpUser = new User(); 32 tmpUser.setId(0L); 33 return tmpUser; 34 } 35}
2.7 添加登录认证Eureka服务 Fallback 类(springms-consumer-movie-feign-custom\src\main\java\com\springms\config\TestEurekaAuthConfiguration.java)
1package com.springms.cloud.feign; 2 3import org.springframework.stereotype.Component; 4 5/** 6 * Hystrix 客户端回退机制类。 7 * 8 * 这里加上注解 Component 的目的:就是因为没有这个注解,运行时候会报错,报错会说没有该类的这个实例,所以我们就想到要实例化这个类,因此加了这个注解。 9 * 10 * @author hmilyylimh 11 * 12 * @version 0.0.1 13 * 14 * @date 2017/9/24 15 * 16 */ 17@Component 18public class UserFeignCustomSecondClientFallback implements UserFeignCustomSecondClient{ 19 20 @Override 21 public String findEurekaInfo(String serviceName) { 22 System.out.println("========== findEurekaInfo Fallback " + Thread.currentThread().getThreadGroup() + " - " + Thread.currentThread().getId() + " - " + Thread.currentThread().getName()); 23 return "springms-provider-user"; 24 } 25}
2.8 添加访问远端用户微服务配置类(springms-consumer-movie-feign-custom-without-hystrix\src\main\java\com\springms\config\TestFeignCustomConfiguration.java)
1package com.springms.config; 2 3import feign.Contract; 4import feign.Logger; 5import org.springframework.context.annotation.Bean; 6import org.springframework.context.annotation.Configuration; 7 8/** 9 * 自定义配置。 10 * 11 * 不和 com.springms.cloud 在同级目录,因为文档有说明,不要被扫描到即可。 12 * 13 * @author hmilyylimh 14 * 15 * @version 0.0.1 16 * 17 * @date 2017/9/24 18 * 19 */ 20@Configuration 21public class TestFeignCustomConfiguration { 22 23 @Bean 24 public Contract feignContract(){ 25 return new feign.Contract.Default(); 26 } 27 28 /** 29 * 日志级别配置 30 * 31 * @return 32 */ 33 @Bean 34 Logger.Level feignLoggerLevel(){ 35 return Logger.Level.FULL; 36 } 37}
2.9 添加访问远端Eureka微服务配置类(springms-consumer-movie-feign-custom-without-hystrix\src\main\java\com\springms\config\TestEurekaAuthConfiguration.java)
1package com.springms.config; 2 3import feign.Feign; 4import feign.auth.BasicAuthRequestInterceptor; 5import org.springframework.context.annotation.Bean; 6import org.springframework.context.annotation.Configuration; 7import org.springframework.context.annotation.Scope; 8 9/** 10 * 认证配置,由于 UserFeignCustomSecondClient 访问 http://localhost:8761/ 需要密码登录,所以才有了此配置的出现。 11 * 12 * 不和 com.springms.cloud 在同级目录,因为文档有说明,不要被扫描到即可。 13 * 14 * @author hmilyylimh 15 * 16 * @version 0.0.1 17 * 18 * @date 2017/9/24 19 * 20 */ 21@Configuration 22public class TestEurekaAuthConfiguration { 23 24 /** 25 * 此方法主要配置登录 Eureka 服务器的帐号与密码。 26 * 27 * @return 28 */ 29 @Bean 30 public BasicAuthRequestInterceptor basicAuthRequestInterceptor(){ 31 return new BasicAuthRequestInterceptor("admin", "admin"); 32 } 33 34 /** 35 * 在该配置中,加入这个方法的话,表明使用了该配置的地方,就会禁用该模块使用 Hystrix 容灾降级的功能; 36 * 37 * @return 38 */ 39 @Bean 40 @Scope("prototype") 41 public Feign.Builder feignBuilder(){ 42 return Feign.builder(); 43 } 44}
2.10 添加Web访问层Controller(springms-consumer-movie-feign-custom-without-hystrix\src\main\java\com\springms\cloud\controller\MovieFeignCustomWithoutHystrixController.java)
1package com.springms.cloud.controller; 2 3import com.springms.cloud.entity.User; 4import com.springms.cloud.feign.UserFeignCustomClient; 5import com.springms.cloud.feign.UserFeignCustomSecondClient; 6import org.springframework.beans.factory.annotation.Autowired; 7import org.springframework.web.bind.annotation.GetMapping; 8import org.springframework.web.bind.annotation.PathVariable; 9import org.springframework.web.bind.annotation.RestController; 10 11@RestController 12public class MovieFeignCustomWithoutHystrixController { 13 14 @Autowired 15 private UserFeignCustomClient userFeignCustomClient; 16 17 @Autowired 18 private UserFeignCustomSecondClient userFeignCustomSecondClient; 19 20 @GetMapping("/movie/{id}") 21 public User findById(@PathVariable Long id) { 22 System.out.println("======== findById Controller " + Thread.currentThread().getThreadGroup() + " - " + Thread.currentThread().getId() + " - " + Thread.currentThread().getName()); 23 return userFeignCustomClient.findById(id); 24 } 25 26 @GetMapping("/{serviceName}") 27 public String findEurekaInfo(@PathVariable String serviceName){ 28 System.out.println("======== findEurekaInfo Controller " + Thread.currentThread().getThreadGroup() + " - " + Thread.currentThread().getId() + " - " + Thread.currentThread().getName()); 29 return userFeignCustomSecondClient.findEurekaInfo(serviceName); 30 } 31}
2.11 添加电影微服务启动类(springms-consumer-movie-feign-custom-without-hystrix\src\main\java\com\springms\cloud\MsConsumerMovieFeignCustomWithoutHystrixApplication.java)
1package com.springms.cloud; 2 3import org.springframework.boot.SpringApplication; 4import org.springframework.boot.autoconfigure.SpringBootApplication; 5import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 6import org.springframework.cloud.netflix.feign.EnableFeignClients; 7 8/** 9 * 10 * 电影微服务,定制Feign,一个Feign功能禁用Hystrix,另一个Feign功能启用Hystrix。 11 * 12 * Feign: Java HTTP 客户端开发的工具。 13 * 14 * 注解 EnableFeignClients 表示该电影微服务已经接入 Feign 模块。 15 * 16 * @author hmilyylimh 17 * 18 * @version 0.0.1 19 * 20 * @date 2017/9/24 21 * 22 */ 23@SpringBootApplication 24@EnableEurekaClient 25@EnableFeignClients 26public class MsConsumerMovieFeignCustomWithoutHystrixApplication { 27 28 public static void main(String[] args) { 29 SpringApplication.run(MsConsumerMovieFeignCustomWithoutHystrixApplication.class, args); 30 System.out.println("【【【【【【 电影自定义FeignWithoutHystrix微服务 】】】】】】已启动."); 31 } 32}
三、测试
1/**************************************************************************************** 2 一、电影微服务,定制Feign,一个Feign功能禁用Hystrix,另一个Feign功能启用Hystrix(正常功能测试): 3 4 1、编写:UserFeignCustomClientFallback、UserFeignCustomSecondClientFallback 断路器回退客户端类; 5 2、编写 TestEurekaAuthConfiguration 配置,加入禁用 Hystrix 模块功能的代码,表示禁用该配置的客户端模块; 6 3、启动 springms-discovery-eureka 模块服务,启动1个端口; 7 4、启动 springms-provider-user 模块服务,启动1个端口; 8 5、启动 springms-consumer-movie-feign-custom-without-hystrix 模块服务; 9 6、在浏览器输入地址 http://localhost:8110/movie/1 可以看到信息成功的被打印出来,表明正常情况下一切正常; 10 7、在浏览器输入地址 http://localhost:8110/springms-provider-user 可以看到信息成功的被打印出来,表明正常情况下一切正常; 11 12 注意:如果第6步如果一开始就输入ID = 0 的用户信息,不要灰心,耐心等等,说不定服务之间还没通信链接成功呢。。。 13 如果第6步的地址输入错误,请回头看看 springms-provider-user 该微服务 appname 的名字,再次输入http://localhost:8110/ + appname 即可试试; 14 15 ****************************************************************************************/ 16 17/**************************************************************************************** 18 二、电影微服务,定制Feign,一个Feign功能禁用Hystrix,另一个Feign功能启用Hystrix(禁用其中一个 Feign 的断路器功能): 19 20 1、编写:UserFeignCustomClientFallback、UserFeignCustomSecondClientFallback 断路器回退客户端类; 21 2、编写 TestEurekaAuthConfiguration 配置,加入禁用 Hystrix 模块功能的代码,表示禁用该配置的客户端模块; 22 3、启动 springms-discovery-eureka 模块服务,启动1个端口; 23 4、启动 springms-provider-user 模块服务,启动1个端口; 24 5、启动 springms-consumer-movie-feign-custom-without-hystrix 模块服务; 25 6、在浏览器输入地址 http://localhost:8110/movie/1 可以看到信息成功的被打印出来,表明正常情况下一切正常; 26 7、在浏览器输入地址 http://localhost:8110/springms-provider-user 可以看到信息成功的被打印出来,表明正常情况下一切正常; 27 28 8、关闭 springms-provider-user 模块服务; 29 9、在浏览器输入地址 http://localhost:8110/movie/1 可以看到用户ID = 0 的用户信息被打印出来,表明该模块的Hystrix断路器模块起作用了; 30 31 10、再关闭 springms-discovery-eureka 模块服务; 32 11、再在浏览器输入地址 http://localhost:8110/appname-springms-provider-user 可以看到网页已经打印出链接不上服务的错误页面了,表明该模块禁用Hystrix断路器模块也起作用了; 33 ****************************************************************************************/
四、下载地址
https://gitee.com/ylimhhmily/SpringCloudTutorial.git
SpringCloudTutorial交流QQ群: 235322432
SpringCloudTutorial交流微信群: 微信沟通群二维码图片链接
欢迎关注,您的肯定是对我最大的支持!!!