Sping 专题

FeignClient fallback 实现

2022-10-23  本文已影响0人  S拒绝拖延

FeignClient定义

@FeignClient(value = "userservice", configuration = FeignClientConfig.class, fallbackFactory = UserClientFallBackFactory.class)
public interface UserServiceClinet {

    @GetMapping("/user/get/{id}")
    Employees getUserById(@PathVariable("id") String id);
}

FallbackFactory 实现类

import com.liberty.entity.Employees;
import com.liberty.feignclients.UserServiceClinet;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class UserClientFallBackFactory implements FallbackFactory<UserServiceClinet> {

    @Override
    public UserServiceClinet create(Throwable throwable) {
        return id -> {
            log.error("用户查询异常!", throwable);
            return new Employees();
        };
    }
}

FeignClientConfig

import com.liberty.feignclients.fallback.UserClientFallBackFactory;
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FeignClientConfig {

    @Bean
    public Logger.Level fiegnLogLevel() {
        return Logger.Level.NONE;
    }


    @Bean
    public UserClientFallBackFactory userClientFallBackFactory() {
        return new UserClientFallBackFactory();
    }
}

yml 配置文件记得开启enable

feign:
  httpclient:
    enabled: true
    max-connections: 200 #最大连接数
    max-connections-per-route: 60 #单个路径的最大连接数
    connection-timeout: 1
  sentinel:
    enabled: true
# hystrix:
#   enabled: 
上一篇 下一篇

猜你喜欢

热点阅读