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. Beacon Designer 8.14 下载及破解

    先安装 Beacon Designer 8.14 下载地址 http://1.download.minixiazai.com/pc/Beacon.Designer.8_po.ziphttp://www ...

  2. netty5客户端监测服务端断连后重连

    服务端挂了或者主动拒绝客户端的连接后,客户端不死心,每15秒重连试试,3次都不行就算了.修改下之前的客户端引导类(NettyClient,参见netty5心跳与业务消息分发实例),新增两个成员变量,在 ...

  3. v关于使用Glide加载图片失败时显示自己特定的图片

    Glide是Android加载图片的一个框架. 常用加载图片到imageView:Glide.with(this).load(url).into(ImageView imageview). 当加载失败 ...

  4. Laya的调试,调试面板,断点调试

    参考: 性能统计面板介绍 版本2.1.1.1 调试面板 Laya有两个调试选项,编辑模式F9. 第一个调试模式,除了调试面板,还有一个查看当前舞台对象的面板.类似白鹭的Egret Inspector. ...

  5. 报错:(未解决)java.lang.VerifyError: Instruction type does not match stack map

    报错背景: CDH中集成kafka的服务,解决完kafka的jar包报错之后重启,发现这个报错. 报错现象: java.lang.VerifyError: Instruction type does ...

  6. WebService学习总结(三)——使用JDK开发WebService(转)

    一.WebService的开发手段 使用Java开发WebService时可以使用以下两种开发手段 1. 使用JDK开发(1.6及以上版本) 2.使用CXF框架开发(工作中) 二.使用JDK开发Web ...

  7. Python - Django - session 的基本使用

    urls.py: from django.conf.urls import url from app02 import views urlpatterns = [ # app02 url(r'^app ...

  8. Python - Django - ORM 常用的字段属性

    字段参数: null:用于表示某个字段可以为空 unique:如果设置为 unique=True,则该字段在此表中必须是唯一的 db_index:如果 db_index=True,则代表着为此字段设置 ...

  9. LeetCode_258. Add Digits

    258. Add Digits Easy Given a non-negative integer num, repeatedly add all its digits until the resul ...

  10. attribute__关键字举例之visibility

    /** @file visibilityT.c * @note * @brief * @author * @date 2019-6-20 * @note v1.0.0 Created * @histo ...