Feign整合sentinel

文章目录

  • 一、依赖
  • 二、注解
  • 三、配置
  • 四、使用
  • 五、配置sentinel规则


提示:以下是本篇文章正文内容,下面案例可供参考

一、依赖

<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

二、注解

@FeignClient(name = "user-center",fallbackFactory = UserCenterFeignFallbackFactoryHandler.class)
public interface UserCenterFeignClient {

    /**
     * http://user-center/users/{id}
     * @param id
     * @return
     */
    @GetMapping("/users/{id}")
    UserDTO findById(@PathVariable Integer id);
}

三、配置

feign:
  client:
    config:
      default: #全局
        loggerLevel: full
  sentinel:  #为Feign整合sentinel
    enabled: true
  httpclient:
    #让feign使用httpclient做请求;而不是urlConnection
    enabled: true
    #feign的最大链接数
    max-connections: 200
    #fegin的单个路径最大连接数
    max-connections-per-route: 50

四、使用

创建UserCenterFeignFallbackFactoryHandler


import com.itmuch.contentcenter.domain.dto.user.UserDTO;
import com.itmuch.contentcenter.feignclient.UserCenterFeignClient;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
//1.要加@Component
//2.要实现FallbackFactory泛型写你的FeignClient
//3.实现create方法
public class UserCenterFeignFallbackFactoryHandler implements FallbackFactory<UserCenterFeignClient> {


    @Override
    public UserCenterFeignClient create(Throwable throwable) {
        return new UserCenterFeignClient() {
            @Override
            public UserDTO findById(Integer id) {
                log.warn("发生了流控或者降级!"+throwable.getMessage());
                UserDTO userDTO = new UserDTO();
                return userDTO;
            }
        };
    }
}

五、配置sentinel规则

Feign整合sentinel_第1张图片

你可能感兴趣的:(sentinel,feign,JAVA,spring,cloud,微服务,java)