什么是Feign?

Feign的作用也是负载均衡,不过Feign是只需要创建一个接口,然后加上注解的声明式Web服务客户端

而且,Feign集成了Ribbon,默认的负载均衡方式也是轮询。

有了Ribbon我还要Feign干嘛?

上一篇文章说了,Ribbon很强大,甚至可以自定义负载均衡的算法。那为什么还会有Feign这个负载均衡的东西呢?

原因是:Ribbon对微服务的调用是这样的

 private static final String REST_URL_PREFIX="http://PROVIDER-DEPT";

    @Autowired
private RestTemplate restTemplate;

Ribbon通过微服务的服务名和RestTemplate来调用,但是实际开发中会用到接口式编程,例如WebService接口,这个时候Ribbon没办法提供接口式的访问,而Feign可以。所以什么是Feign?接口加注解的Web服务端调用的负载均衡技术。

新建consumer-feign

我们原有的consumer80项目,使用的是Ribbon+RestTemplate的模式,现在我们新建一个consumer项目,起名为consumer-feign-80,新建完成之后,Maven的pom文件里,把consumer-80复制过来之外还需要添加Feign的引用

        <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>

yml文件也复制过来就可以

新建一个Controller,为Feign而生处理请求,如下

package com.vae.springcloud.controller;

import com.vae.springcloud.entity.DeptEntity;
import com.vae.springcloud.service.DeptClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import java.util.List; public class DeptControllerFeign { @Autowired
private DeptClientService service; @PostMapping(value = "/dept/add")
public boolean add(@RequestBody DeptEntity deptEntity){
return service.add(deptEntity);
} @GetMapping(value = "/dept/get/{id}")
public DeptEntity get(@PathVariable("id") Integer id){
return service.get(id);
}
@GetMapping(value = "/dept/list")
public List<DeptEntity> get() throws Exception{
return service.get();
}
@GetMapping(value = "/dept/delete/{id}")
public boolean delete(@PathVariable("id") Integer id){
return service.delete(id);
}
@GetMapping(value = "/dept/update")
public void update(@RequestBody DeptEntity deptEntity){
service.update(deptEntity);
} }

可以看到,用到了DeptClientService这个接口,这个等下介绍,接口式编程嘛

主方法我们要添加两个和Feign有关的注解

package com.vae.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan; @SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = {"com.vae.springcloud"})
@ComponentScan("com.vae.springcloud")
public class ConsumerFeign80Application { public static void main(String[] args) {
SpringApplication.run(ConsumerFeign80Application.class, args);
} }

@EnableFeignClients(basePackages = {"com.vae.springcloud"})

@ComponentScan("com.vae.springcloud")

就是这两个注解

修改api项目

我们的Feign是接口式加注解的负载均衡,现在上面我们新建的子项目consumer-feign-80都加了注解了,现在开始写接口了,在我们的api项目里面写

引入Maven文件

首先,要在api项目里引入feign的引用

        <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>

新建feign的接口

package com.vae.springcloud.service;

import com.vae.springcloud.entity.DeptEntity;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.List; @FeignClient(value = "provider-dept")
public interface DeptClientService { @PostMapping(value = "/dept/add")
public boolean add(@RequestBody DeptEntity deptEntity); @GetMapping(value = "/dept/get/{id}")
public DeptEntity get(@PathVariable("id") Integer id); @GetMapping(value = "/dept/list")
public List<DeptEntity> get(); @GetMapping(value = "/dept/delete/{id}")
public boolean delete(@PathVariable("id") Integer id); @GetMapping(value = "/dept/update")
public void update(@RequestBody DeptEntity deptEntity); }

启动项目

启动eureka集群,再启动provider集群,再启动consumer-feign-80客户端,你可以发现consumer是可以访问的,默认的还是轮询的方式。

但是我的项目报了一个错,如下:

报错

我加入了Feign的Maven引用之后就是报错,我暂时无法解决,导致我无法启动provider项目

报错如下:

2019-04-16 12:37:18.492 ERROR 12260 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.vae.springcloud.api.ApiApplication]; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfiguration.class] cannot be opened because it does not exist

Caused by: java.io.FileNotFoundException: class path resource [org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfiguration.class] cannot be opened because it does not exist

我怀疑是我的Maven配置,或者其他某个地方配置的问题。反正我是无法解决,网上目前查不出来原因。

Feign啊,暂且搁置吧

如果有谁会Feign的配置这一块,希望不吝赐教教教我......

发泄发泄心情,一个段落

我真的是无语了,下面学Hystrix的时候,又遇到了这个问题,网上根本搜不出来,全中国只有我遇到这个问题???

暂时不写SpringCloud了,不学了,心累,网上根本搜不到,身边的朋友除了我几乎没有学Java的,学的几个只会SSM........

还有,我Google的时候发现了一家傻逼网站,叫什么码农教程,你教你妈呢?我刚发的文章就抄袭了,还他妈原文,恬不知耻,傻逼

SpringCloud笔记五:Feign的更多相关文章

  1. springcloud系列五 feign远程调用服务

    一:Feign简介 Feign 是一种声明式.模板化的 HTTP 客户端,在 Spring Cloud 中使用 Feign,可以做到使用 HTTP请求远程服务时能与调用本地方法一样的编码体验,开发者完 ...

  2. C#可扩展编程之MEF学习笔记(五):MEF高级进阶

    好久没有写博客了,今天抽空继续写MEF系列的文章.有园友提出这种系列的文章要做个目录,看起来方便,所以就抽空做了一个,放到每篇文章的最后. 前面四篇讲了MEF的基础知识,学完了前四篇,MEF中比较常用 ...

  3. 《MFC游戏开发》笔记五 定时器和简单动画

    本系列文章由七十一雾央编写,转载请注明出处. http://blog.csdn.net/u011371356/article/details/9332377 作者:七十一雾央 新浪微博:http:// ...

  4. (转)Qt Model/View 学习笔记 (五)——View 类

    Qt Model/View 学习笔记 (五) View 类 概念 在model/view架构中,view从model中获得数据项然后显示给用户.数据显示的方式不必与model提供的表示方式相同,可以与 ...

  5. java之jvm学习笔记五(实践写自己的类装载器)

    java之jvm学习笔记五(实践写自己的类装载器) 课程源码:http://download.csdn.net/detail/yfqnihao/4866501 前面第三和第四节我们一直在强调一句话,类 ...

  6. Crazyflie笔记五: CRTP 实时通信协议(一)(转)

    源:Crazyflie笔记五: CRTP 实时通信协议(一) 这里详细介绍了 Crazyflie 的 CRTP实时通信协议的相关内容,由于内容很长,分几篇博文来讲述.这里是第一节内容.欢迎交流:301 ...

  7. SpringCloud学习之feign

    一.关于feigin feigin是一种模板化,声明式的http客户端,feign可以通过注解绑定到接口上来简化Http请求访问.当然我们也可以在创建Feign对象时定制自定义解码器(xml或者jso ...

  8. Learning ROS for Robotics Programming Second Edition学习笔记(五) indigo computer vision

    中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS for Robotics Pr ...

  9. java框架之SpringCloud(4)-Ribbon&Feign负载均衡

    在上一章节已经学习了 Eureka 的使用,SpringCloud 也提供了基于 Eureka 负载均衡的两种方案:Ribbon 和 Feign. Ribbon负载均衡 介绍 SpringCloud ...

随机推荐

  1. 系统功能调用Windows操作系统原理实验

    一.实验目的 1.熟悉操作系统的系统功能调用. 2.掌握用C语言实现系统功能调用的方法和步骤. 3.掌握利用10H号功能调用(BIOS的显示I/O功能调用)来实现对屏幕的操作与控制. 二.实验内容 1 ...

  2. 谈谈当代大学生学习IT技术的必要性。

    21世纪,人类社会已经从工业时代全面进入信息化时代,IT技术的发展正在影响人类的日常生活.比如,外卖平台给人们的用餐提供了更多的选择,移动支付颠覆了传统的支付方式.网购使得人们的购物更加方便,真正做到 ...

  3. html+css 制作简易导航栏

    二话不说直接上代码(萌新:实在也没什么好说的) <!DOCTYPE html> <html lang="en" xmlns="http://www.w3 ...

  4. 回去试idea

    https://blog.csdn.net/s_eal/article/details/81486472?utm_source=blogxgwz0

  5. PostgreSQL:Java使用CopyManager实现客户端文件COPY导入

    在MySQL中,可以使用LOAD DATA INFILE和LOAD DATA LOCAL INFILE两种方式导入文本文件中的数据到数据库表中,速度非常快.其中LOAD DATA INFILE使用的文 ...

  6. 慢日志查询python flask sqlalchemy慢日志记录

    engine = create_engine(ProdConfig.SQLALCHEMY_DATABASE_URI, echo=True) app = Flask(__name__) app.conf ...

  7. SQL语句检索数据排序及过滤

    阅读目录 一:排序检索数据 二:过滤数据 三:高级数据过滤 四:用通配符进行过滤 回到顶部 一:排序检索数据 1.1 排序数据 比如查询数据库中表数据的时候,我们使用如下语句: select * fr ...

  8. Guava Cache探索及spring项目整合GuavaCache实例

    背景 对于高频访问但是低频更新的数据我们一般会做缓存,尤其是在并发量比较高的业务里,原始的手段我们可以使用HashMap或者ConcurrentHashMap来存储. 这样没什么毛病,但是会面临一个问 ...

  9. 《Linux/UNIX系统编程手册》第63章 IO多路复用、信号驱动IO以及epoll

    关键词:fasync_helper.kill_async.sigsuspend.sigaction.fcntl.F_SETOWN_EX.F_SETSIG.select().poll().poll_wa ...

  10. spring4配置文件详解

    转自: spring4配置文件详解 一.配置数据源 基本的加载properties配置文件 <context:property-placeholder location="classp ...