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. imageButton和zoomButton

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  2. PHP过狗webshell编写过程

    0x1 先上图: 0x2 编写过程 这里必须强调一下我内心的感觉,小阔爱前两天也研究了过狗的一句话了,然后我突然觉得大家都在进步,我研究点啥呢?不如也试试以前因为觉得自己不懂代码,而不会研究的免杀sh ...

  3. Dart中排除空的情况:

    但是dart的string类型还有另一个方法isNotEmpty,此时这样写: if (str?.isNotEmpty()) { // str is not empty, do something } ...

  4. 【Mybatis】MyBatis之插件开发(十)

    MyBatis插件开发原理 MyBatis采用责任链模式,通过动态代理组织多个插件(拦截器),通过这些插件可以改变MyBatis的默认行为(诸如SQL重写之类的),由于插件会深入到MyBatis的核心 ...

  5. PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)

    1070 Mooncake (25 分)   Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...

  6. 安装kubernet(k8s)

    简介: 需要学习的都明白,这里就不赘述了. 本文采用CentOS-7-x86_64-DVD-1810.iso 一:安装操作系统 本来是喜欢用fedora的,但是fedora貌似包维护的不好,就又开始用 ...

  7. pipline中使用ansible

    实验架构: 192.168.0.96 gitlab 192.168.0.97 jenkins.ansible 192.168.0.98 harbor.docker集群 说明:下面代码编译镜像那一步的代 ...

  8. ubuntu 18.04 安装搜狗输入法

    安装 fcitxsudo apt install fcitx 配置输入法打开fcitx配置界面,点击桌面左下角的菜单,弹出如下桌面: 点击: “Input Method”,进入设置界面 点击 “OK” ...

  9. bam文件格式说明

    bam文件说明 bam文件和sam文件内容其实是一样的,只是bam是二进制的压缩文件,需要通过特定的软件来进行查看,bam文件通常可以理解为12个字段组成 BAM格式分为header section( ...

  10. conda创建、删除、重命名环境

    链接:https://www.jianshu.com/p/7265011ba3f2 创建新环境 conda create -n rcnn python=3.6 删除环境 conda remove -n ...