原文链接:
https://blog.csdn.net/catoop/article/details/101011782
https://www.jianshu.com/p/e92f3600a09f
一,导入依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
·
·
二, 启用类上添加注解@EnableFeignClients:
启用类上添加注解@EnableFeignClients
客户端允许开启使用Feign调用,扫描@FeignClient
标注的FeignClient接口
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrix
public class RibbonConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonConsumerApplication.class, args);
}
}
·
·
三,进行Feign相关参数配置:
配置yml文件feign.hystrix.enabled=true:
server:
port: 8808
feign:
hystrix:
#开启feign的hystrix支持,默认是false
enabled: true
或者:添加配置项application.properties
# 是否启用httpclient
feign.httpclient.enabled=false
# 是否启用httpok(性能比httpclient高)
feign.okhttp.enabled=true
# 是否启用hystrix
feign.hystrix.enabled=true
# 请求连接超时时间(毫秒)
feign.httpclient.connection-timeout=3000
·
·
四,编写配置类:
添加Feign配置文件(如果不添加此配置文件会报如下错误),错误信息: No qualifying bean of type ‘org.springframework.boot.autoconfigure.http.HttpMessag。
/**
* feign的http客户端配置
*/
@Configuration
public class FeignConfig {
@Bean
@ConditionalOnMissingBean
public HttpMessageConverters messageConverters(ObjectProvider<HttpMessageConverter<?>> converters) {
return new HttpMessageConverters(converters.orderedStream().collect(Collectors.toList()));
}
}
使用配置类的注意事项:
上面的配置类被@Configuration
注解标注,此时该配置类会作为Feign的全局配置起作用,
与在启动类的@EnableFeignClients
注解中配置defaultConfiguration
属性效果一样。如果需要对每个Feign进行差异化配置则需要去掉@Configuration
注解(defaultConfiguration
同理),
然后在每个@FeignClient
注解配置configuration
属性来指定配置类。
·
·
五,编写FeignClient接口和回退类:
标识@FeignClient的接口:
使用Feign 调用 Restful 接口,feign 内部基于 ribbon 实现负载均衡,无需做特殊配置处理。
这块的代码多说两句:
1、接口调用方,也就是 FeignClient 端的工程,也直接引入服务提供方抽象的 API 接口依赖。
这样服务调用方 FeignClient 创建接口类继承依赖进来的 API 接口即可(不用重复的定义 GetMapping PostMapping RequestMapping 那一堆方法)
2、@FeignClient 建议使用 fallbackFactory 属性,而不是 fallback 属性。
因为前者可以操作异常信息,可操作性相对更强一些。变成层面也没有增加什么复杂性,只不过匿名类的方式比 fallback 略显不够直观(这个根据自己实际情况选择)。
@FeignClient常用参数:
-
name 就是被调用方的服务名称 (
这里如果你没有配置服务注册中心的化,其实可以随便写
) -
url 就是被调用方的地址(
如果配置了服务注册中心, 可以不写!, 不过两个服务必须都注册!,这样才能找到!
) -
fallbackFactory :就是调用失败时指定的处理类
@FeignClient(name = "data-base/base",
configuration=FeignClientsConfiguration.class,
fallbackFactory = RestFeignImpl.class)
public interface RestFeign {
@PostMapping(path = "/api/action")
Rs action(RequestJSON requestJSON);
}
注解 @FeignClient 的主要属性说明:
回退类:
fallback和fallbackFactory 属性:指定一个就是调用失败时指定的处理类,这个类必须实现@FeignClient声明的接口,并且在spring context中。
建议使用 fallbackFactory 属性,而不是 fallback 属性。因为前者可以操作异常信息,可操作性相对更强一些。变成层面也没有增加什么复杂性,只不过匿名类的方式比 fallback 略显不够直观(这个根据自己实际情况选择)。
@Component
public class RestFeignImpl implements FallbackFactory<RestFeign> {
@Override
public RestFeign create(Throwable throwable) {
throwable.printStackTrace();
return requestJSON -> RsUtil.error("远程调用发生错误:"+throwable.getMessage());
}
}
·
·
六,使用:
1、封装一个包含请求信息的实体类:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class RequestJSON {
//服务类型
private String fromType;
//服务类
private String serviceClass;
//服务方法
private String method;
//数据
private Object data;
}
2、在业务类中使用Feign进行远程调用:
@Service
public class EmapAxisSectionRelationFunction implements RelationFunctionService {
@Resource
RestFeign restFeign;
public static List<FieldContent> test(String sql) {
RequestJSON requestJSON = new RequestJSON(
"datawork-service",
"CONTENT",
"executeSql",
//查询逻辑
sql);
//获取返回结果:
Rs dataResult = restFeign.action(requestJSON);
}
}