什么是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. 万物皆有始有终: Hawk5即日起停止升级迭代

    从即日起,Hawk将停止升级工作,其版本号将停留在5. https://github.com/ferventdesert/Hawk Hawk已经开发和维护6年时间了,它曾经承载了开发者很多的期待.背后 ...

  2. 微信小程序设置域名、不校验域名

    设置--项目设置 将不校验域名勾上就可以了,不再校验域名了通过 url: 'https://localhost:8443/spring4/user/list.do',就可以访问后台了. 若要配置域名则 ...

  3. SpringIOC和AOP简单概述

    Spring学习:主要学习两大块IOC   AOP 一.IOC   IOC:控制反转(也可以叫做依赖注入)的基本认识:   当某个Java对象(调用者)需要调用另一个Java对象(被依赖对象)的方法时 ...

  4. python之json模块

    #!/usr/bin/python # -*- coding: UTF- -*- ''' )序列化相关 json pickle (序列化是指将) 序列化是指将一个字符串转换成基础数据类型或者基础数据类 ...

  5. 如何查杀stopped进程

    在Linux系统下面,top命令可以查看查看stopped进程.但是不能查看stopped进程的详细信息.那么如何查看stopped 进程,并且杀掉这些stopped进程呢? ps -e j | gr ...

  6. ubuntu 18.04安装pytorch、cuda、cudnn等

    版权声明:本文为博主原创文章,欢迎转载,并请注明出处.联系方式:460356155@qq.com ubuntu 16.04用了1年多了,18.04版已经发布也半年了,与时俱进,重装Linux系统,这里 ...

  7. [转帖]Windows 10 部分早期版本已完全停止技术支持服务

    Windows 10 部分早期版本已完全停止技术支持服务 2019-4-12 01:27| 发布者: cjy__05| 查看: 10186| 评论: 47|来自: pcbeta 收藏分享 转帖来源:h ...

  8. 关于oracle sql语句查询时表名和字段名要加双引号的问题详解

    转自:http://www.2cto.com/database/201504/387184.html 作为oracle的初学者相信大家一定会遇到这个问题,如图: 明明就是navicat可视化创建了表, ...

  9. 解决SVN一直弹出登录问题,eclipse.tmatesoft.com

    Windows->preferences->Igonored Resources Add Pattern.. .project .classpath .settings 添加这三个: 把C ...

  10. HDU6278 Just h-index

    主席树+二分 每次对给定区间从1-区间长度len二分mid,查询区间内第mid大的数是不是大于等于mid.. #include <bits/stdc++.h> #define INF 0x ...