OpenFeign的使用

一、消费端引用pom坐标

        <!--feign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <!--            <scope>provided</scope>-->
        </dependency>
        <!-- okhttp 扩展 -->
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-okhttp</artifactId>
        </dependency>

二、消费端启动类上增加@EnableFeignClients注解

@EnableAsync
@SpringBootApplication(scanBasePackages = "com.test.*", exclude = DataSourceAutoConfiguration.class)
@EnableFeignClients
public class TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }

三、消费端编写feign接口类

@FeignClient(contextId = "remoteFileFunction", value = "file-service")
public interface RemoteFileService {

    @ApiOperation(value = "查询文件" , notes="查询文件")
    @RequestMapping(value="/file/query",method = RequestMethod.POST)
    Result query(@RequestBody FileInfo fileInfo);
}

四、注意

 遇到上图这种情况时解决方法:

① 排查@FeignClient注解参数contextId是否重复

② 如果contextId没有重复,排查启动类注解@EnableFeignClients是否重复使用(此注解是用来扫描带@FeignClient的接口类),造成了重复扫描而报错。(重复使用的可能有:启动类加了注解的情况下,在pom引入的子模块中有使用该注解或者配置了FeignConfig,如下面的代码)

@EnableFeignClients("com.test.**.feign")
@Configuration
@ConditionalOnClass(Feign.class)
@AutoConfigureBefore(FeignAutoConfiguration.class)
public class FeignConfig {

③ 配置文件中配置(不推荐)

spring:
  main:
    allow-bean-definition-overriding: true

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值