Feign是什么?

Feign是一个http请求调用的轻量级框架,也可以说是声明式WebService客户端

Feign的作用

可以以Java接口注解的方式调用Http请求,它使java调用Http请求变的简单
Feign集成了Ribbon,实现了客户端的负载均衡

Feign的工作原理(简易版)

1、首先通过@EnableFeignCleints注解开启FeignCleint
2、根据Feign的规则实现接口,并加@FeignCleint注解
3、程序启动后,会进行包扫描,扫描所有的@FeignCleint的注解的类,并将这些信息注入到ioc容器中。
4、当接口的方法被调用,通过jdk的代理,来生成具体的RequesTemplate
5、RequesTemplate再生成Request
6、Request交给Client去处理,其中Client默认是HttpUrlConnection(也可以是HttpClient或Okhttp,需要配置)
7、最后Client被封装到LoadBalanceClient类,这个类结合类Ribbon做到了负载均衡。

启动类

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

import org.springframework.cloud.netflix.hystrix.EnableHystrix;

import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableHystrix

@EnableFeignClients

@EnableDiscoveryClient

@SpringBootApplication

public class JisServiceConsumerApplication {

public static void main(String[] args) {
SpringApplication.run(JisServiceConsumerApplication.class, args);
}

}

根据Feign的规则实现的接口

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

import org.springframework.cloud.netflix.hystrix.EnableHystrix;

import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableHystrix

@EnableFeignClients

@EnableDiscoveryClient

@SpringBootApplication

public class JisServiceConsumerApplication {

public static void main(String[] args) {
SpringApplication.run(JisServiceConsumerApplication.class, args);
}

}

熔断器的fallback(调用接口失败时会执行)

import org.springframework.stereotype.Component;

import org.springframework.web.bind.annotation.RequestParam;

import com.example.jisserviceconsumer.inter.HelloRemote;

@Component

public class HelloRemoteHystrix implements HelloRemote{

@Override
public String hello(@RequestParam(value = "name") String name) {
return "hello" +name+", this messge send failed biu biu biu ~ ";
}

}

Feign的理解的更多相关文章

  1. 深入理解Feign之源码解析

    转载请标明出处: 本文出自方志朋的博客 什么是Feign Feign是受到Retrofit,JAXRS-2.0和WebSocket的影响,它是一个jav的到http客户端绑定的开源项目. Feign的 ...

  2. SpringCloud的入门学习之概念理解、Feign负载均衡入门

    1.Feign是SpringCloud的一个负载均衡组件. Feign是一个声明式WebService客户端.使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口, ...

  3. 玩转Spring Cloud之服务注册发现(eureka)及负载均衡消费(ribbon、feign)

    如果说用Spring Boot+Spring MVC是开发单体应用(或单体服务)的利器,那么Spring Boot+Spring MVC+Spring Cloud将是开发分布式应用(快速构建微服务)的 ...

  4. SpringCloud---熔断降级理解、Hystrix实战(五)

    SpringCloud---熔断降级理解.Hystrix实战(五) https://www.cnblogs.com/qdhxhz/p/9581440.html https://blog.csdn.ne ...

  5. SpringCloud(6)---熔断降级理解、Hystrix实战

    SpringCloud(6)---熔断降级理解.Hystrix实战 一.概念 1.为什么需要熔断降级 (1)需求背景 它是系统负载过高,突发流量或者网络等各种异常情况介绍,常用的解决方案. 在一个分布 ...

  6. SpringCloud(5)---Feign服务调用

    SpringCloud(5)---Feign服务调用 上一篇写了通过Ribbon进行服务调用,这篇其它都一样,唯一不一样的就是通过Feign进行服务调用. 注册中心和商品微服务不变,和上篇博客一样,具 ...

  7. Spring Cloud Alibaba基础教程:支持的几种服务消费方式(RestTemplate、WebClient、Feign)

    通过<Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现>一文的学习,我们已经学会如何使用Nacos来实现服务的注册与发现,同时也介绍如何通过LoadBal ...

  8. SpringCloud(3)服务消费者(Feign)

    上一篇文章,讲述了如何通过 RestTemplate+Ribbon 去消费服务,这篇文章主要讲述如何通过Feign去消费服务. 1.Feign简介 Feign是一个声明式的伪Http客户端,它使得写H ...

  9. Feign源码解析系列-注册套路

    感谢不知名朋友的打赏,感谢你的支持! 开始 在追寻Feign源码的过程中发现了一些套路,既然是套路,就可以举一反三,所以值得关注. 这篇会详细解析Feign Client配置和初始化的方式,这些方式大 ...

随机推荐

  1. 修复gitlab服务器突然停电导致PostgreSQL损坏的数据库

    最开始是存储的卷组受损,使用的DRBD,使用了xfs分区格式: 挂载也报错: mount /dev/drbd0 /var/opt mount: wrong fs type, bad option, b ...

  2. List和List的区别?

    List<T>和List<?>的区别,这问的有点含糊.我想题主是想问类型参数“<T>”和无界通配符“<?>”的区别吧? 讨论“<T>&quo ...

  3. python读写csv文件的方法(还没试,先记录一下)

    该csv模块定义了以下功能: csv.reader(csvfile,dialect ='excel',** fmtparams ) 返回一个reader对象,它将迭代给定csvfile中的行. csv ...

  4. iOS popToViewController具体用法

    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIn ...

  5. maven command line specified settings.xml

    1. using argument parameter --settings  / or -s for shot mvn install --settings c:\user\settings.xml ...

  6. mysql quick query row count using sql

    1. command show table status like '{table-name}'; 2. sample mysql> use inventory; Database change ...

  7. 静态站点生成器-md-vue-vuepress

    推荐指数:

  8. LODOP中带caption的表格被关联并次页偏移测试

    ADD_PRINT_TABLE中的thead和tfoot可以每页输出,后面的打印项关联表格,可以紧跟着表格,实现在表格后面紧跟着输出内容的效果,表格可以自动分页,并总是跟在表格后面 ,在表格最后输出. ...

  9. 【warning】set the environment variable MXNET_CUDNN_AUTOTUNE_DEFAULT to 0 to disable

    前言 运行mxnet程序的时候出现这个warning信息,但是不影响整个程序的运行,不过宝宝喜欢将warning信息尽量也clear,强迫症嘛?!哈哈哈哈 问题描述 [::] src/operator ...

  10. 14、vue-pdf的使用

    安装 npm install --save vue-pdf vue-pdf默认只显示第一页,可以写按钮翻页,也可以v-for多页显示 项目结构 实例一 按钮分页 <template> &l ...