Feign使用fallbackFactory属性打印fallback异常;
fallbackFactory 是fallback的一个升级版;
microservice-consumer-movie-feign-with-hystrix-hystrix-factory/pom.xml
4.0.0
microservice-consumer-movie-feign-with-hystrix-hystrix-factory
jar
com.itmuch.cloud
microservice-spring-cloud
0.0.1-SNAPSHOT
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-feign
spring:
application:
name: microservice-consumer-movie-feign-with-hystrix-hystrix-factory
server:
port: 7901
eureka:
client:
healthcheck:
enabled: true
serviceUrl:
defaultZone: http://user:password123@localhost:8761/eureka
instance:
prefer-ip-address: true
com.itmuch.cloud.controller.MovieController
package com.itmuch.cloud.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.itmuch.cloud.entity.User;
import com.itmuch.cloud.feign.UserFeignClient;
@RestController
public class MovieController {
@Autowired
private UserFeignClient userFeignClient;
@GetMapping("/movie/{id}")
public User findById(@PathVariable Long id) {
return this.userFeignClient.findById(id);
}
}
com.itmuch.cloud.feign.UserFeignClientWithFactory
package com.itmuch.cloud.feign;
public interface UserFeignClientWithFactory extends UserFeignClient {
}
com.itmuch.cloud.feign.HystrixClientFactory
package com.itmuch.cloud.feign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.itmuch.cloud.entity.User;
import feign.hystrix.FallbackFactory;
@Component
public class HystrixClientFactory implements FallbackFactory {
private static final Logger LOGGER = LoggerFactory.getLogger(HystrixClientFactory.class);
@Override
public UserFeignClient create(Throwable cause) {
HystrixClientFactory.LOGGER.info("fallback; reason was: {}", cause.getMessage());
return new UserFeignClientWithFactory() {
@Override
public User findById(Long id) {
User user = new User();
user.setId(-1L);
return user;
}
};
}
}
说明:
如果需要访问作为回退触发器的原因,则可以使用@FeignClient中的fallbackFactory属性。
为指定的客户端接口定义一个回退工厂。回退工厂必须产生回退类的实例,这些实例实现由FeignClient注释的接口。
如果同时设置fallback和fallbackfactory不可以的,会有有冲突,fallback生效,fallbackfactory不能使用,fallbackFactory 是fallback的一个升级版,注释fallback设置即可;
com.itmuch.cloud.feign.UserFeignClient
package com.itmuch.cloud.feign;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.itmuch.cloud.entity.User;
@FeignClient(name = "microservice-provider-user", /*fallback = HystrixClientFallback.class, */fallbackFactory = HystrixClientFactory.class)
public interface UserFeignClient {
@RequestMapping(value = "/simple/{id}", method = RequestMethod.GET)
public User findById(@PathVariable("id") Long id);
}
说明:
1)将fallback注释掉;
2)采用fallbackFactory的方式;
com.itmuch.cloud.ConsumerMovieFeignApplication
package com.itmuch.cloud;
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
public class ConsumerMovieFeignApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerMovieFeignApplication.class, args);
}
}
http://localhost:7901/movie/1
==============================
QQ群:143522604
群里有相关资源
欢迎和大家一起学习、交流、提升!
==============================