OpenFeign入门

2024-10-09 13:58:37 浏览数 (1)

引入启动

在配置类中加入@EnableFeignClients注解,该注解引入相关类FeignClientsRegistrar,通过此类进行客户端@FeignClient注册;@FeignClient注解放在接口类中。

配置

配置

说明

feign.circuitbreaker.enabled=true

启用熔断器

feign.compression.request或者response.enabled

请求压缩

feign.client.config.default.logger-level = full

配置日志,其中default可以修改为具体的client name

日志配置

feign的日志级别有:

NONE, 默认没有

BASIC, 记录方法、路径、响应状态、执行时间

HEADERS, 比BASIC多HEADER信息

FULL, header信息, 请求body, requests 和 responses元信息

可以在配置文件中配置,也可以通过代码配置

代码语言:java复制
@Configuration
public class FooConfiguration {
    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }
}

熔断器

1、配置@FeignClient中的fallback参数,fallback需要注册为bean

代码语言:java复制
@FeignClient(name="provider",fallback=UserServiceCallback.class)
public interface UserService {
	
	@RequestMapping(method = RequestMethod.GET, value = "/appone/home/")
	public String addUser(@RequestParam String userid,@RequestParam String pwd,@RequestParam String say);
	
	@RequestMapping(method = RequestMethod.GET, value = "/appone/home/")
	public String delUser(@RequestParam String userid,@RequestParam String say);
	
	@RequestMapping(method = RequestMethod.GET, value = "/appone/home/")
	public String getUer(@RequestParam String userid,@RequestParam String say);
	
	@RequestMapping(method = RequestMethod.GET, value = "/appone/home/allLogs/")
	public Object allLogs(@RequestParam PageInfo pg,@RequestParam String title);
	
	@RequestMapping(method = RequestMethod.POST, value = "/appone/home/delLog")
	public ResponseMessage<Object> delLog(String id);
	
}

@Component
public class UserServiceCallback implements UserService {
	
	Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	public String addUser(String userid, String pwd, String say) {
		return null;
	}

	@Override
	public String delUser(String userid, String say) {
		return null;
	}

	@Override
	public String getUer(String userid, String say) {
		return null;
	}

	@Override
	public Object allLogs(PageInfo pg, String title) {
		throw new NoFallbackAvailableException("Boom!", new RuntimeException());
	}

	@Override
	public ResponseMessage<Object> delLog(String id) {
		throw new NoFallbackAvailableException("Boom!", new RuntimeException());
	}
	
}

2、若要知道具体断路的原因,则需要fallbackFactory参数,也需要配置为bean

拦截器

拦截器用于拦截请求,可以增加认证参数

1、继承RequestInterceptor

代码语言:java复制
public interface RequestInterceptor {

  /**
   * Called for every request. Add data using methods on the supplied {@link RequestTemplate}.
   */
  void apply(RequestTemplate template);
}

public class MyInterceptor implements RequestInterceptor
{
    @Override
    public void apply(RequestTemplate template)
    {

    }
}

2、注册

2.1 直接注册为bean

增加 @Component 或者 @Bean 注解

2.2 通过配置

feign.client.config.服务名.requestInterceptors0: com.xxx.xx

2.3 通过configuration

指定配置

原理

FeignClientsRegistrar继承了ImportBeanDefinitionRegistrar

代码语言:java复制
class FeignClientsRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware, EnvironmentAware {

    @Override
	public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
        /// 注册默认的配置
		registerDefaultConfiguration(metadata, registry);
        /// 注册 @FeignClient 注解的类,并使用JDK动态代理(所以要求client必须是接口)
		registerFeignClients(metadata, registry);
	}
}

0 人点赞