什么是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. Redis可视化工具 Redis Desktop Manager

    1.前言 从接触Redis也有两年,平时就使用它来做缓存层,它给我的印象就是很强大,内置的数据结构很齐全,加上Redis5.0的到来,新增了很多特色功能.而Redis5.0最大的新特性就是多出了一个数 ...

  2. Markdown语法指南

    1.背景 个人比较喜欢用Markdonw写东西,比如写博客随笔,写有道云笔记等,但有的时候会突然忘记某个具体语法怎么写了,如插入图片.插入链接.表格等,那干脆把这个语法简单地总结一下,也方便日后快速查 ...

  3. SQLServer之修改UNIQUE约束

    使用SSMS数据库管理工具修改UNIQUE约束 1.连接数据库,选择数据表->右键点击->选择设计(或者展开键,选择要修改的键,右键点击,选择修改,后面步骤相同). 2.选择要修改的数据列 ...

  4. git冲突解决办法合集

    一 换行符CRLF错误解决办法 1 错误产生原因 不同的操作系统使用的换行符是不一样的. unix/linux使用的是LF,max后期也采用了LF,但在windows一直采用的CRLF(回车)换行符. ...

  5. Java中a+=b和a=a+b的区别

    在Java语言中a+=b和a=a+b是有区别的,主要的区别是在运算时精度的问题,当然了-=.*=./=,%=也都是一个道理.这里以a+=b和a=a+b为例做说明. (1)下面以一段Java程序为例,试 ...

  6. js 获取输入框中光标的索引位置

    <html> <head></head> <body> <script> function getTxt1CursorPosition(){ ...

  7. postgresql事务

    查看更多教程:http://www.gitbook.net/postgresql/2013080567.html pgsql事务与并发控制 事务与并发控制 数据库几大特性: ACID: Atomici ...

  8. HTML之超链接

    图像标签 图像标签为 <img> ,它是行内元素,其主要功能是在网页里面插入图像,所插入图片由属性 scr 属性决定.主要格式为 <img scr="URL"&g ...

  9. redis从入门到高可用 Redis复制的原理与优化

    需要的联系我,QQ:1844912514

  10. map遍历性能记录

    map遍历可以通过keySet或者entrySet方式. 性能上:entrySet略胜一筹,原因是keySet获取到key后再根据key去获取value,在查一遍,所以慢一些. keySet: //先 ...