在跑Demo案例时,没有遇见这个问题,在我们业务消费服务代码中创建
熔断器时产生了下面的问题,项目都跑不起来了。
Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map ‘xxx.xxxService’ method
public abstract java.util.Map xxx.xxxService.listFAQ(java.lang.Integer,java.lang.String)
to {GET /api/web/v2/faq}: There is already ‘faqFallBack’ bean method
public java.util.Map xxx.xxxFallBack.listFAQ(java.lang.Integer,java.lang.String) mapped.
网上的答案: 报这个错的原因是因为你controller里的@RequestMapping中的路径有重复,检查了下,并没有重复的,BUG并不是这个原因
jdk8 + maven + idea + Spring全家桶
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
打开熔断器,默认是false
feign:
sentinel:
enabled: true
记得在入口类添加注解 @EnableFeignClients
@FeignClient(value = "provider-member",fallback = FaqFallBack.class)
@RequestMapping("/api/web/v2")
public interface FaqService {
@RequestMapping(value = "/faq",method = RequestMethod.GET)
Map listFAQ(@RequestParam("pageNo")Integer pageNo, @RequestParam("distributor")String distributor);
}
@Component
public class FaqFallBack implements FaqService {
@Override
public Map listFAQ(Integer pageNo, String distributor) {
return Response.BAD("无法获取FAQ列表~");
}
}
通过工厂创建熔断器,解决了我的问题。。
import com.ezblock.consumermember.service.fallback.FaqFallBack;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;
@Component
public class FaqServiceFallBackFactory implements FallbackFactory<FaqFallBack> {
@Override
public FaqFallBack create(Throwable throwable) {
return new FaqFallBack(throwable);
}
}
不需要@Component注解,对每一个函数进行了熔断处理
public class FaqFallBack implements FaqService {
private Throwable throwable;
public FaqFallBack(Throwable throwable) {
this.throwable = throwable;
}
@Override
public Map listFAQ(Integer pageNo, String distributor) {
return Response.BAD("无法获取FAQ列表~");
}
}
@FeignClient(value = "provider-member",fallbackFactory = FaqServiceFallBackFactory.class)
@RequestMapping("/api/web/v2")
public interface FaqService {
@RequestMapping(value = "/faq",method = RequestMethod.GET)
Map listFAQ(@RequestParam("pageNo")Integer pageNo, @RequestParam("distributor")String distributor);
}
github:spring-cloud-alibaba sentinel结合fegin使用案例