验证中...
Languages: Java
Categories: Web开发技术
Latest update 2019-11-12 19:12
application.yml
Raw Copy
feign:
sentinel:
enabled: true
限流降级发生时,自定义处理逻辑
Raw Copy
package com.inedible.contentcenter.feignclient;
import com.inedible.contentcenter.domain.dto.user.UserDto;
import feign.Logger;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
@FeignClient(name = "user-center", fallbackFactory = UserCenterFallbackFactory.class)
public interface UserCenterFeignClient {
@GetMapping("/users/{id}")
UserDto findById(@PathVariable Integer id);
}
/**
* fallback = UserCenterFallback.class
* 自定义的限流降级处理逻辑
* fallback 和 fallbackFactory 不能同时使用
* fallbackFactory可以拿到异常,而fallback不行
*/
@Component
class UserCenterFallback implements UserCenterFeignClient {
@Override
public UserDto findById(Integer id) {
UserDto userDto = new UserDto();
userDto.setWxNickname("默认用户");
return userDto;
}
}
/**
* fallbackFactory = UserCenterFallbackFactory.class
*/
@Slf4j
@Component
class UserCenterFallbackFactory implements FallbackFactory<UserCenterFeignClient> {
@Override
public UserCenterFeignClient create(Throwable throwable) {
return id -> {
log.warn("被限流/降级", throwable);
UserDto userDto = new UserDto();
userDto.setWxNickname("默认用户");
return userDto;
};
}
}

Comment list( 0 )

Sign in for post a comment

Help Search

191139_cd20d5fd_5186603 191143_ebef6f8d_5186603