1.问题描述
再给feign客户端配置熔断器时报错。
2.代码展示
- application.properties文件
spring.application.name=feign-consumer
server.port=4444
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}
- 启动类
···
@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
public class SpringCloudFeignCusApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudFeignCusApplication.class, args);
}
}
- 控制器
···
@RestController
public class ConsumerController {
@Autowired
ComputeClient computeClient;
@RequestMapping("/add")
public Integer add(){
return computeClient.add(10,20);
}
}
- feign客户端
···
@FeignClient(value = "compute-service",fallback = ComputeClientHystrix.class)
public interface ComputeClient {
@RequestMapping(value = "/add",method = RequestMethod.GET)
Integer add(@RequestParam("a") Integer a,@RequestParam("b") Integer b);
}
- ComputeClientHystrix.class
···
@Component
public class ComputeClientHystrix implements ComputeClient {
@Override
public Integer add(@RequestParam("a") Integer a,@RequestParam("b") Integer b) {
return -99999;
}
}
3.解决办法
在application.properties文件中添加feign.hystrix.enabled=true
,开启熔断器,默认为false。
4.页面访问
在关闭依赖服务(compute-service)后,访问http://localhost:4444/add,页面正常显示