重写Feign+单个禁用Feign的Hystrix支持
如果Hystrix在类路径和feign上hystrix。
启用= true,Feign将用一个断路器来包装所有的方法。
返回一个com.netflix.hystrix.HystrixCommand也是可用的。
这允许您使用反应模式(调用. toobservable()或. observe()或异步使用(调用. queue())。
要在每个客户机基础上禁用Hystrix支持,可以创建一个香草假。使用“原型”范围的构建器,例如:
ConfigurationAuth类
package com.example.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import feign.Feign;
import feign.auth.BasicAuthRequestInterceptor;
/**
* 此类不要放在启动类能扫描到的包下
* @author 重写Feign的默认配置
*
*/
@Configuration
public class ConfigurationAuth {
//配置注册到Eureka所需要的账户和密码
@Bean
public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
return new BasicAuthRequestInterceptor("user", "123456");
}
/**
* 禁用单个feign的hystrix支持
* @return
*/
@Bean
@Scope("prototype")
public Feign.Builder feignBuilder() {
return Feign.builder();
}
}
ConfigurationContract类
package com.example.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import feign.Contract;
import feign.Logger;
/**
* 此类不要放在启动类能扫描到的包下
* @author 重写Feign的默认配置
*
*/
@Configuration
public class ConfigurationContract {
//Contract默认是Spring MVC的契约,就可以用Spring MVC的注解
@Bean
public Contract feignContract() {
return new feign.Contract.Default();
}
//配置日志级别
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
OrderController类
package com.example.demo.controller;
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;
import com.example.demo.entity.User;
import com.example.demo.feign.FeignClientServiceName;
import com.example.demo.feign.UserFeignClient;
//声明式服务
@RestController
public class OrderController {
@Autowired
private UserFeignClient userFeignClient;
@Autowired
private FeignClientServiceName feignClientServiceName;
@GetMapping("/order/{id}")
public User findById(@PathVariable Long id) {
return this.userFeignClient.findById(id);
}
@GetMapping("/{serviceName}")
public String findServiceInfoFromEurekaByServiceName(@PathVariable String serviceName) {
return this.feignClientServiceName.findServiceInfoFromEurekaByServiceName(serviceName);
}
}
User类
package com.example.demo.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;
}
}
FeignClientServiceHystrixFeignFallBack类
package com.example.demo.feign;
import org.springframework.stereotype.Component;
@Component
public class FeignClientServiceHystrixFeignFallBack implements FeignClientServiceName {
@Override
public String findServiceInfoFromEurekaByServiceName(String serviceName) {
return "hello";
}
}
FeignClientServiceName接口
package com.example.demo.feign;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.example.config.ConfigurationAuth;
@FeignClient(name = "aaaa", url = "http://localhost:8761/", configuration = ConfigurationAuth.class,fallback=FeignClientServiceHystrixFeignFallBack.class)
public interface FeignClientServiceName {
@RequestMapping(value = "/eureka/apps/{serviceName}")
public String findServiceInfoFromEurekaByServiceName(@PathVariable("serviceName") String serviceName);
}
UserFeignClient接口
package com.example.demo.feign;
import org.springframework.cloud.netflix.feign.FeignClient;
import com.example.config.ConfigurationContract;
import com.example.demo.entity.User;
import feign.Param;
import feign.RequestLine;
//重写Feign的默认配置
@FeignClient(name = "spring-cloud-user", configuration = ConfigurationContract.class,fallback=UserHystrixFeignFallBack.class) // 调用哪个服务的名称
public interface UserFeignClient {
@RequestLine("GET /user/{id}") //注意:GET后面有一个空格
public User findById(@Param("id") Long id);
UserHystrixFeignFallBack类
package com.example.demo.feign;
import org.springframework.stereotype.Component;
import com.example.demo.entity.User;
@Component
public class UserHystrixFeignFallBack implements UserFeignClient {
@Override
public User findById(Long id) {
return new User();
}
}
SpringCloudOrderApplication类
package com.example.demo;
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;
/**
* 声明式服务
*
*/
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients//表示为Fegin客户端
public class SpringCloudOrderApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudOrderApplication.class, args);
}
}
application.yml配置
spring:
application:
name: spring-cloud-order_overriding_feign_defaults-hystrix #微服务的名称
server:
port: 7907 #微服务端口号
eureka:
client:
healthcheck:
enabled: true # 开启健康检查
serviceUrl:
defaultZone: http://user:123456@localhost:8761/eureka #服务eureka的URL地址
instance:
prefer-ip-address: true
logging:
level:
com.example.demo.feign.UserFeignClient: DEBUG
# 解决第一次请求报超时异常的方案:
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 5000
# 或者:
# hystrix.command.default.execution.timeout.enabled: false
# 或者:
# 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
pom.xml配置
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.demo</groupId>
<artifactId>spring-cloud-order_overriding_feign_defaults</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>spring-cloud-order_overriding_feign_defaults</name>
<description>spring-cloud-order_overriding_feign_defaults</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<properties>
<!-- 文件拷贝时的编码 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- 编译时的编码 -->
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<!-- jdk版本 -->
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 注册中心 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- 这个库让我们可以访问应用的很多信息,包括:/env、/info、/metrics、/health等 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 添加声明式feign依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<!-- 用于热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!-- 版本依赖管理,故之后添加依赖无需指定version -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<!-- 用以为integration-test提供支持。 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
把用户服务停掉之后,效果如下:
查看健康指标:hystrix起作用了