Feign英语单词,主要用作为动词,用作动词译为“装作;假装,伪装;捏造(借口、理由等);创造或虚构,假装;装作;作假;佯作”
Feign是一个声明式的http客户端,起作用就是帮助我们优雅的实现http请求的发送,解决restTemplate的不足之处。
代码可读性差,编程体验不统一
参数复杂的url难以维护
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
@SpringBootApplication @EnableFeignClients public class DemoOrderApplication { ... }
新建接口,FeignClient指定服务名
package priv.sinoam.demoorder.demo.clients; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import java.util.Map; @FeignClient("demo-user") public interface UserClient { @GetMapping("/demouser/user/test") Map<String, Object> test1(); }
调用接口
@Autowired private UserClient userClient; @Override public Map<String, Object> test1() { Map<String, Object> map2 = userClient.test1(); return map; }
此时应该可以启动了,但是
报错中可以找到这句话:No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalancer?
由于SpringCloud Feign在Hoxton.M2 RELEASED版本之后不再使用Ribbon而是使用spring-cloud-loadbalancer,所以不引入spring-cloud-loadbalancer会报错
引入依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-loadbalancer</artifactId> </dependency>
完美解决,正常启动
报错:
java.lang.AbstractMethodError: org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.choose(Ljava/lang/String;Lorg/springframework/cloud/client/loadbalancer/Request;)Lorg/springframework/cloud/client/ServiceInstance;
在nacos依赖中添加扩展:
<!-- nacos客户端依赖包 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <exclusions> <exclusion> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </exclusion> </exclusions> </dependency> </dependencies>
重启测试成功
版本太新可能会遇到问题,但是网上很多是选择退回版本的方式解决,但其实可以与时俱进的,不必要的时候,何必退后呢。