zoukankan      html  css  js  c++  java
  • Feign的理解

    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 ~ ";
    }
    

    }

  • 相关阅读:
    jQuery插件之jqzoom
    python数据分析画图体验
    python正则表达式,以及应用[下载图片]
    Python 程序如何高效地调试?
    从汉诺塔游戏理解python递归函数
    Python文本文件的输入输出操作学习
    Windows搭建python开发环境
    python循环综合运用
    Python参数传递,既不是传值也不是传引用
    使用python实现用微信远程控制电脑
  • 原文地址:https://www.cnblogs.com/jis121/p/11018327.html
Copyright © 2011-2022 走看看