前言

Feign英语单词,主要用作为动词,用作动词译为“装作;假装,伪装;捏造(借口、理由等);创造或虚构,假装;装作;作假;佯作”

Feign是一个声明式的http客户端,起作用就是帮助我们优雅的实现http请求的发送,解决restTemplate的不足之处。

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>

重启测试成功

后记

版本太新可能会遇到问题,但是网上很多是选择退回版本的方式解决,但其实可以与时俱进的,不必要的时候,何必退后呢。

学习spring cloud记录11-Feign初体验的更多相关文章

  1. Spring Cloud(Dalston.SR5)--Feign 声明式REST客户端

    Spring Cloud 对 Feign 进行了封装,集成了 Ribbon 并结合 Eureka 可以实现客户端的负载均衡,Spring Cloud 实现的 Feign 客户端类名为 LoadBala ...

  2. Spring Cloud 入门 之 Feign 篇(三)

    原文地址:Spring Cloud 入门 之 Feign 篇(三) 博客地址:http://www.extlight.com 一.前言 在上一篇文章<Spring Cloud 入门 之 Ribb ...

  3. 0.9.0.RELEASE版本的spring cloud alibaba sentinel+feign降级处理实例

    既然用到了feign,那么主要是针对服务消费方的降级处理.我们基于0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例添油加醋,把sentinel功能加 ...

  4. Spring Cloud中关于Feign的常见问题总结

    一.FeignClient接口,不能使用@GettingMapping 之类的组合注解 代码示例: @FeignClient("microservice-provider-user" ...

  5. Spring Cloud系列之Feign的常见问题总结

    一.FeignClient接口,不能使用@GettingMapping 之类的组合注解 代码示例: @FeignClient("microservice-provider-user" ...

  6. Spring Cloud下使用Feign Form实现微服务之间的文件上传

    背景 ​ Spring Cloud现在已经被越来越多的公司采用了,微服务架构比传统意义上的单服务架构从复杂度上多了很多,出现了很多复杂的场景.比如,我们的产品是个app,支持第三方登录功能,在手机端调 ...

  7. Spring Cloud ZooKeeper集成Feign的坑2,服务调用了一次后第二次调用就变成了500,错误:Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is com.n

    错误如下: 2017-09-19 15:05:24.659 INFO 9986 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refre ...

  8. 深入学习spring cloud gateway 限流熔断

    前言 Spring Cloud Gateway 目前,Spring Cloud Gateway是仅次于Spring Cloud Netflix的第二个最受欢迎的Spring Cloud项目(就GitH ...

  9. Spring Cloud Alibaba(8)---Feign服务调用

    Feign服务调用 有关Spring Cloud Alibaba之前写过五篇文章,这篇也是在上面项目的基础上进行开发. Spring Cloud Alibaba(1)---入门篇 Spring Clo ...

  10. Android开发学习之路--百度地图之初体验

    手机都有gps和网络,通过gps或者网络可以定位到自己,然后通过百度,腾讯啊之类的地图可以显示我们的地理位置.这里学习下百度地图的使用.首先就是要申请开发者了,这个详细就不多讲了.http://dev ...

随机推荐

  1. 学习Kotlin语法(三)

    简介 在上一节,我们对Kotlin中面向对象编程(OOP)的相关知识有了大致的了解,本章节我们将去进一步了解函数.lambada表达式.内联函数.操作符重载.作用域函数. 目录 函数 函数的使用 参数 ...

  2. 【开源推荐】AI Interviewer:基于Spring-Alibaba-AI的智能面试官系统(附GitHub实战教程)

    项目背景 作为开发者,你是否经历过: 技术面试时因紧张发挥失常? 刷了1000+LeetCode却不会表达解题思路? 花费上千元购买模拟面试服务? 今天推荐的AI Interviewer开源项目,正是 ...

  3. JMeter提取响应结果保存到本地总结

    1.说明 本次实验以登录接口为例提取响应结果中的uid和ticket参数并保存到csv文件 2.脚本结构 说明: 1)本次实验的在于BeanShell后置处理程序的编写,登录接口参数传递之前有些总结, ...

  4. Bean注入几种方式 (放入Spring容器)

    目录 1.XML方式注入 set方式注入 构造方法注入 2.注解方式注入 @Component + @ComponentScan @Configuration + @Bean + @Component ...

  5. 定义一个正则表达式,使用finditer从表达式内取值后存储到列表中

    演示代码: responce = requests.get(url = url_web,headers=head) url_obj = re.compile(r'<a class="m ...

  6. MySql随笔记基础

    XAMPP使用 shell 命令 每个数据库对应 一个子文件夹 mysql 进入mySQL的命令 -uroot userroot 登录用户 -uroot -p password 登录密码 -p123 ...

  7. 如何在 MySQL 中避免单点故障?

    如何在 MySQL 中避免单点故障? 在 MySQL 中避免单点故障(SPOF, Single Point of Failure)是确保数据库高可用性和系统稳定性的关键.通过采取以下几种策略,可以最大 ...

  8. FreeSWITCH中SIP网关(Gateway)操作

    freeswitch是一款简单好用的VOIP开源软交换平台. 以下是一篇关于FreeSWITCH中SIP网关(Gateway)操作的技术指南,基于提供的官方文档内容整理: 一.网关生命周期管理 1. ...

  9. 工具 | ysoSimple

    0x00 简介 ysoSimple是一款简易的Java漏洞利用工具,集成Java反序列化,Hessian反序列化,XStream反序列化,SnakeYaml反序列化,Shiro550,JSF反序列化, ...

  10. 【BUG】C语言|左移之后,最高位的数字还在吗?(整型提升)

    文章目录 问题概述 应用 怀旧 问题概述 这个错是刚学c语言的时候碰到的,突然好想我的c语言老师,所以在此记录一下. #include<stdio.h> void main(){ unsi ...