今天实践一下feign,但是fallback一直不生效
spingcloud 2021.0.3
消费者系统
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
@FeignClient(name = "sndhmg-config-client-producer", fallback = UserConsumerFeignFallback.class)
public interface UserConsumerFeign {
@GetMapping(path = "user/myName/{name}")
ResultVo<String> hello(@PathVariable String name);
}
@Component
public class UserConsumerFeignFallback implements UserConsumerFeign {
@Override
public ResultVo<String> hello(String name) {
return new ResultVo<>("feign 调用失败");
}
}
@Service
public class UserService {
@Autowired
RestTemplate restTemplate;
@Autowired
LoadBalancerClient loadBalancerClient;
@Autowired
UserConsumerFeign userConsumerFeign;
@Value("${feign.circuitbreaker.enabled}")
String circuitbreaker;
@Value("${feign.hystrix.enabled}")
String hystrix;
public ResultVo<String> hello(String name) {
ResponseEntity<ResultVo> resp = restTemplate
.getForEntity("http://sndhmg-config-client-producer/user/myName/" + name, ResultVo.class);
return resp.getBody();
}
public ResultVo<String> feignHello(String name) {
return userConsumerFeign.hello(name);
}
}
feign:
circuitbreaker:
enabled: true
hystrix:
enabled: true
感觉没有啥问题,但是始终不生效,求指教