1、配置文件
feign: sentinel: enabled: true
2、 编写一个工厂类
import com.cart.cartservice.client.ItemClient;
import com.cart.cartservice.entity.Item;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.http.ResponseEntity;
@Slf4j
public class ItemClientFallbackFactory implements FallbackFactory<ItemClient> {
@Override
public ItemClient create(Throwable cause) {
return new ItemClient() {
@Override
public ResponseEntity<Item> queryById(Integer id) {
log.error("查询对象失败", cause);
//返回一个空对象
return ResponseEntity.ok(new Item());
}
};
}
}
需要实现FallbackFactory接口,其中ItemClient为客户端接口。
3、声明这个类
import com.cart.cartservice.factory.ItemCli