SpringCloud之Feign与Hystrix的整合
配置信息server:port: 8010spring:application:name: microservice-consumer-ordereureka:client:serviceUrl:defaultZone: http://localhost:8761/eureka/instance:prefer-ip-add...
·
配置信息
server:
port: 8010
spring:
application:
name: microservice-consumer-order
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
feign:
hystrix:
enabled: true
# 说明:请务必注意,从Spring Cloud Dalston开始,Feign默认是不开启Hystrix的。
# 因此,如使用Dalston请务必额外设置属性:feign.hystrix.enabled=true,否则
断路器不会生效。
# 而,Spring Cloud Angel/Brixton/Camden中,Feign默认都是开启Hystrix的。
无需设置该属性。
UserFeignClient
/**
* Feign的fallback测试
* 使用@FeignClient的fallback属性指定回退类
*/
@FeignClient(name = "microservice-provider-user",
fallback = FeignClientFallback.class/*,
configuration = FeignDisableHystrixConfiguration.class*/)
public interface UserFeignClient {
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public User findById(@PathVariable("id") Long id);
}
/**
* 回退类FeignClientFallback需实现Feign Client接口
* FeignClientFallback也可以是public class,没有区别
*/
@Component
class FeignClientFallback implements UserFeignClient {
@Override
public User findById(Long id) {
User user = new User();
user.setId(-1L);
user.setUsername("默认用户");
return user;
}
}
OrderController
@RestController
public class OrderController {
@Autowired
private UserFeignClient userFeignClient;
@GetMapping("/user/{id}")
public User findById(@PathVariable Long id) {
return userFeignClient.findById(id);
}
}
启动类不需要加Hystrix的注解,有配置文件配置
@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
public class ConsumerOrderApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerOrderApplication.class, args);
}
}
Feign禁用Hystrix
server:
port: 8010
spring:
application:
name: microservice-consumer-order
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
feign:
hystrix:
enabled: false
# 说明:请务必注意,从Spring Cloud Dalston开始,Feign默认是不开启Hystrix的。
否则断路器不会生效。
# 而,Spring Cloud Angel/Brixton/Camden中,Feign默认都是开启Hystrix的。
无需设置该属性。
package com.tuling.cloud.study.user.feign;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.tuling.cloud.study.user.entity.User;
/**
* Feign的fallback测试
* 使用@FeignClient的fallback属性指定回退类
*/
@FeignClient(name = "microservice-provider-user",
fallback = FeignClientFallback.class,
configuration = FeignDisableHystrixConfiguration.class)
public interface UserFeignClient {
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public User findById(@PathVariable("id") Long id);
}
/**
* 回退类FeignClientFallback需实现Feign Client接口
* FeignClientFallback也可以是public class,没有区别
*/
@Component
class FeignClientFallback implements UserFeignClient {
@Override
public User findById(Long id) {
User user = new User();
user.setId(-1L);
user.setUsername("默认用户");
return user;
}
}
/*
*
* 为Feign禁用Hystrix功能
*
*/
@Configuration
public class FeignDisableHystrixConfiguration {
@Bean
@Scope("prototype")
public Feign.Builder feignBuilder() {
return Feign.builder();
}
}
更多推荐
已为社区贡献26条内容
所有评论(0)