• SpringCloud之Feign[五]


    SpringCloud之Feign

    Feign是什么?

      Feign是简化Java HTTP客户端开发的工具它是一个jav的到http客户端绑定的开源项目。 Feign的主要目标是将Java Http 客户端变得简单。Feign的源码地址:https://github.com/OpenFeign/feign,在此我们用Feign调用下SpringCloud的服务步骤:

      1.引入Gradle依赖

     //fegin的依赖
        compile('org.springframework.cloud:spring-cloud-starter-feign')
        // https://mvnrepository.com/artifact/com.netflix.feign/feign-gson
        compile group: 'com.netflix.feign', name: 'feign-gson', version: '8.16.2'
        //注册中心调用者
        compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
        //熔断器
        compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-hystrix'
        compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-hystrix-dashboard'
        //依赖api
        compile project(":api")

      2.在接口上继承需要调用的Api接口

    /**
     * 定义fegin调用服务 以及熔断
     * fallback表示熔断器 降级机制
     * value 表示依赖注册中心哪个服务
     */
    @Primary
    @FeignClient(value = "SERVICE",fallback = ClassesHystrix.class)
    public interface FeignInterfice extends ClassesApi {
    }
    --------------------------------------------------华丽的分割线,以下是继承的接口方法--------------------------------------------
    /**
    * 用于给service提供接口
    */
    public interface ClassesApi {
    /**
    * 查询全部
    * @return
    */
    @GetMapping("selectAll")
    List<DtoClasses> selectAll();

    /**
    * 报错降级处理
    * @return
    */
    @GetMapping("error")
    List<DtoClasses> error();
    }

      @FeignClient(value = "SERVICE",fallback = ClassesHystrix.class)       value 表示调用哪个服务在注册中心的名称 fallback 可以不写 表示熔断降级

    处理的类

    3.在启动类上加入注解

      @SpringBootApplication   此类为Springboot启动类
      @EnableDiscoveryClient(autoRegister = false)  加入此注解它会去注册中心找服务
      @EnableFeignClients  开启Feign
      @EnableCircuitBreaker  将熔断器继承进来

    @SpringBootApplication
    @EnableDiscoveryClient(autoRegister = false)
    @EnableFeignClients
    @EnableCircuitBreaker
    public class FeignApplication {
        public static void main(String[] args) {
            SpringApplication.run(FeignApplication.class,args);
        }
    }

    全部代码地址:https://github.com/zgc456/SpringCloud-Summary

    里面包含ribbon zuul feign hystrix 等等只看feign即可

    使用时应先启动Eureka(注册中心) 在启动Service(服务生产者) 然后启动Feign调用即可  

     

    
    
    
  • 相关阅读:
    关于云计算:IaaS的四个误解和四个猜想 浪峰小园子
    国外10个优秀的免费轻量级CMS系统 浪峰小园子
    Win8下80端口被System占用,造成Apache不能启动的解决办法 浪峰小园子
    [转载]基于内存数据库的分布式数据库架构何坤 浪峰小园子
    php短域名转换为实际域名函数 浪峰小园子
    [转载]苹果公司与分工原理 浪峰小园子
    PHPer的等级划分
    简单的无限分类树
    转换字符串编码
    php开启虚拟域名功能
  • 原文地址:https://www.cnblogs.com/zheng1/p/8573500.html
Copyright © 2020-2023  润新知