SpringCloud笔记五:Feign
什么是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的更多相关文章
- springcloud系列五 feign远程调用服务
一:Feign简介 Feign 是一种声明式.模板化的 HTTP 客户端,在 Spring Cloud 中使用 Feign,可以做到使用 HTTP请求远程服务时能与调用本地方法一样的编码体验,开发者完 ...
- C#可扩展编程之MEF学习笔记(五):MEF高级进阶
好久没有写博客了,今天抽空继续写MEF系列的文章.有园友提出这种系列的文章要做个目录,看起来方便,所以就抽空做了一个,放到每篇文章的最后. 前面四篇讲了MEF的基础知识,学完了前四篇,MEF中比较常用 ...
- 《MFC游戏开发》笔记五 定时器和简单动画
本系列文章由七十一雾央编写,转载请注明出处. http://blog.csdn.net/u011371356/article/details/9332377 作者:七十一雾央 新浪微博:http:// ...
- (转)Qt Model/View 学习笔记 (五)——View 类
Qt Model/View 学习笔记 (五) View 类 概念 在model/view架构中,view从model中获得数据项然后显示给用户.数据显示的方式不必与model提供的表示方式相同,可以与 ...
- java之jvm学习笔记五(实践写自己的类装载器)
java之jvm学习笔记五(实践写自己的类装载器) 课程源码:http://download.csdn.net/detail/yfqnihao/4866501 前面第三和第四节我们一直在强调一句话,类 ...
- Crazyflie笔记五: CRTP 实时通信协议(一)(转)
源:Crazyflie笔记五: CRTP 实时通信协议(一) 这里详细介绍了 Crazyflie 的 CRTP实时通信协议的相关内容,由于内容很长,分几篇博文来讲述.这里是第一节内容.欢迎交流:301 ...
- SpringCloud学习之feign
一.关于feigin feigin是一种模板化,声明式的http客户端,feign可以通过注解绑定到接口上来简化Http请求访问.当然我们也可以在创建Feign对象时定制自定义解码器(xml或者jso ...
- Learning ROS for Robotics Programming Second Edition学习笔记(五) indigo computer vision
中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS for Robotics Pr ...
- java框架之SpringCloud(4)-Ribbon&Feign负载均衡
在上一章节已经学习了 Eureka 的使用,SpringCloud 也提供了基于 Eureka 负载均衡的两种方案:Ribbon 和 Feign. Ribbon负载均衡 介绍 SpringCloud ...
随机推荐
- Python爬虫之正则表达式(2)
# 最常规的匹配 import re content = 'Hello 123 4567 World_This is a Regex Demo' print(len(content)) result ...
- python 3.7 安装mysqlclient 错误解决
安装时出现的问题 >pip3.7 install mysqlclientCollecting mysqlclient Using cached https://files.pythonhost ...
- 英语口语练习系列-C22-吃东西
基础词汇 1. bill [bɪl] n. 账单:钞票:法案:鸟嘴 Bill (人名)比尔 pay the bill 付账单 telephone bill 话费单 electricity bill 电 ...
- C#基础知识之Dictionary
最近使用了Dictionary,出现了意想不到的错误,先记录一下自己遇到的问题以及目前我的解决方法,然后温习一下Dictionary的基础用法. 一.自己遇到的问题 1.代码如下: namespace ...
- laravel学习笔记一
指定端口 数据迁移 php artisan migrate:install 任何路由 match get,post只选择其一 没有表名对应默认的posts表,如果表为post就不行 时区不对时 分页 ...
- Autoware(2)—加载地图数据
选择Point cloud.Ref选择.autoware/.data/map/pointcloud_map/里面的全选 点Point cloud加载 vector Map和TF同理
- 问题记录2019-03-06(todo)
RuntimeError: maximum recursion depth exceeded while calling a Python object
- 排序学习实践---ranknet方法
要: 1 背景 随着移动互联网的崛起,越来越多的用户开始习惯于从手机完成吃.喝.玩.乐.衣.食.住.行等各个方面的需求.打开手机,点开手淘.美团等APP,商品玲玲满目,而让用户将所有商品一页 ...
- OpenStack-Neutron(5)
一. Neutron 概述 SDN(software-defined networking)软件定义网络,其所具有的灵活性和自动化优势使其成为云时代网络管理的主流. Neutron的设计目标是实现“网 ...
- Linux(Ubuntu 16) 下Java开发环境的配置(二)------Tomcat的配置及常见问题
前言 相比于java JDK的配置,Tomcat的配置简单的多,简直就相当于直接运行了,本文以Tomcat8.0为例进行配置 1.Tomcat的下载 地址:https://tomcat.apach ...