一、消费端引用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