[Feign] Spring Cloud中的OpenFeign(一)

本文是基于官方文档以及自己的个人理解所写,不足之处还请指出。

文章目录

          • 1、什么是OpenFeign
          • 2、Feign的引入
          • 3、Feign中的Configuration
          • 4、Feign中Hystrix的支持

1、什么是OpenFeign

Feign是一个声明式的web服务客户端,用来进行微服务之间的调用。

2、Feign的引入

首先导入如下依赖:

       <dependency>
			<groupId>org.springframework.cloudgroupId>
			<artifactId>spring-cloud-starter-openfeignartifactId>
            <version>2.1.2.RELEASEversion>
        dependency>

在项目启动类中添加如下注解:

@EnableFeignClients  // 启动FeignClient客户端
3、Feign中的Configuration

在Feign中可以通过配置类或者配置文件,覆盖掉其默认配置,接下来通过一个例子来说明:

@FeignClient(name="serverid",configuration=FeignConfiguration.class)
public interface FeignMgmt{
	....
}

(1)通过自定义配置类(FeignConfiguration类):

@Configuration
public class FeignConfiguration{
	// 添加日志
	@Bean
	public Logger.Level feignLoggerLevel() {
		return Logger.Level.FULL;
	}

	// Contract个人理解为对于注解的解释,如果没有自定义的注解使用默认的即可。
	@Bean
	public Contract feignContract() {
		return new Contract.Default();
	}

	@Bean
	public Retryer feignRetryer() {
		return new Retryer.Default(100, SECONDS.toMillis(1), 5);
	}
	//...
	//...
}

注意:FeignConfiguration类(自定义类)要放到ComponentScan扫描不到的地方,否则容易引起冲突。【具体操作:可以通过将它放在与任何@ComponentScan或@SpringBootApplication分开的、不重叠的包中,或者显式地排除在@ComponentScan中,来避免这种情况。】

(2)通过配置文件配置
可以在application.yml文件中进行如下配置:

feign:
  client:
    config:
      default:  # key 为default时表示的是全局配置
        connect-timeout: 1000
        read-timeout: 1000
        request-interceptors: // Feign自定义请求拦截器
        - com.delta.cradle.matlmgmt.feign.MyInterceptor   // 自定义拦截器路径
        .....

@@@补充Feign的请求拦截器:

public class MyInterceptor implements RequestInterceptor {

	@Override
	public void apply(RequestTemplate template) {
		template.header("Content-Type", "application/json");
		System.out.println("这是自定义请求拦截器");
	}

}

拦截器要实现RequestInterceptor 接口,在apply中进行相关的拦截处理

4、Feign中Hystrix的支持

要实现Hystrix,首先需要在application.yml文件中进行如下配置:

  # 启用断路器,不设置fallback无效,如果想要关闭设置为false即可。
  hystrix:
    enabled: true

在FeignClient接口方法中,进行如下配置:

@FeignClient(name = "serviceid", fallback = MatlFeignFallBack.class)
public interface MatlFeignClient {
	//....
}

或者:

@FeignClient(name = "serviceid", fallbackFactory = MatlFeignFallBackFactory.class)
public interface MatlFeignClient {
	//....
}

这两种方式的区别在于,使用fallbackFactory时,可以返回回退触发的原因。

以下为两种方式的回退类的写法:

@Component
public class MatlFeignFallBack implements MatlFeignClient {

	@Override
	public JSONObject getOne(@NotNull(message = "id不能为空") Long id) throws Exception {
		ApiResult<String> apiResult = new ApiResult<String>();
		apiResult.setFailed("fallback,reason is", null);
		return new JSONObject(apiResult);
	}

}
@Component
public class MatlFeignFallBackFactory implements FallbackFactory<MatlFeignClient> {

	@Override
	public MatlFeignClient create(Throwable cause) {
		return new MatlFeignClient() {
			@Override
			public JSONObject getOne(@NotNull(message = "id不能为空") Long id) throws Exception {
				ApiResult<String> apiResult = new ApiResult<String>();
				apiResult.setFailed("fallback,reason is" + cause.getMessage(), null);
				return new JSONObject(apiResult);
			}
		};
	}

}

你可能感兴趣的:(SpringCloud)