一 Feign简介
feign是声明式的web service客户端,它让微服务之间的调用变得更简单了,类似controller调用service。Spring Cloud集成了Ribbon和Eureka,可在使用Feign时提供负载均衡的http客户端。
二 使用
1)添加依赖:
<!-- feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
2)定义接口,添加注解
3)启动类加上@EnableFeignClients注解
4)使用
二 自定义feign配置
1)添加配置类(不能放在包含主应用程序上下文的@ComponentScan中)
@Configuration
public class FeignConfiguration {
@Bean
public Contract UserFeignClient(){
return new feign.Contract.Default();
}
}
2)修改接口的@FeignClient注解,加上
3)使用feign自带注解@RequestLine("GET /findAllPerson"):