zoukankan      html  css  js  c++  java
  • feign hystrix

    1.添加依赖

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <parent>
        <groupId>com.guo</groupId>
        <artifactId>guo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
      </parent>
      <artifactId>guo-feign</artifactId>
      
       <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
        </dependencies>
        
    </project>
    

      

    2.添加配置 application.yml

    eureka:
      client:
        serviceUrl:
          defaultZone: http://localhost:8761/eureka/
    server:
      port: 8765
    spring:
      application:
        name: guo-feign
    
    

      

    3.编写feign接口

    package com.guo.feign.service;
    
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    
    @FeignClient(value = "service-hi")
    public interface SchedualServiceHi {
        @RequestMapping(value = "/hi",method = RequestMethod.GET)
        String sayHiFromClientOne(@RequestParam(value = "name") String name);
    }
    

      

    4.编写controller

    package com.guo.feign.web;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.guo.feign.service.SchedualServiceHi;
    
    @RestController
    public class HiController {
    
    
        //编译器报错,无视。 因为这个Bean是在程序启动的时候注入的,编译器感知不到,所以报错。
        @Autowired
        SchedualServiceHi schedualServiceHi;
    
        @GetMapping(value = "/hi")
        public String sayHi(@RequestParam String name) {
            return schedualServiceHi.sayHiFromClientOne( name );
        }
    }
    

      

    5.编写启动类

    package com.guo.feign;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    
    @SpringBootApplication
    @EnableEurekaClient
    @EnableFeignClients
    public class ServiceFeignApplication {
    
        public static void main(String[] args) {
            SpringApplication.run( ServiceFeignApplication.class, args );
        }
    }
    

    6.在application.yml添加熔断器 hystrix 配置

    feign.hystrix.enabled: true

    eureka:
      client:
        serviceUrl:
          defaultZone: http://localhost:8761/eureka/
    server:
      port: 8765
    spring:
      application:
        name: guo-feign
    
    feign.hystrix.enabled: true
    

    7.编写熔断回调接口,必须实现同一接口 SchedualServiceHi

    package com.guo.feign.service.fallback;
    
    import org.springframework.stereotype.Component;
    
    import com.guo.feign.service.SchedualServiceHi;
    
    @Component
    public class SchedualServiceHiHystric implements SchedualServiceHi {
        @Override
        public String sayHiFromClientOne(String name) {
            return "sorry, you are fail,"+name;
        }
    }
    

    8.在接口上指定熔断回调处理类

    package com.guo.feign.service;
    
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    
    import com.guo.feign.service.fallback.SchedualServiceHiHystric;
    
    @FeignClient(value = "service-hi",fallback = SchedualServiceHiHystric.class)
    public interface SchedualServiceHi {
        @RequestMapping(value = "/hi",method = RequestMethod.GET)
        String sayHiFromClientOne(@RequestParam(value = "name") String name);
    }

      

    9.访问feign接口

    http://localhost:8765/hi?name=forezp

    当关闭服务提供方 service-hi 在此访问feign接口时会返回

    sorry, you are fail,china

  • 相关阅读:
    解决win7和win8的64位系统安装NetAdvantage时总是提示%SystemDriver%inetpub\wwwroot错误的方法
    如何使VS2008 调试网站的根目录和IIS调试的一致?
    设置网站默认目录
    asp.net网站未能加载文件或程序集或它的某一个依赖项。拒绝访问
    HTML5 :Canvas之基本用法
    HTML5 Canvas之图片引用
    python虚拟环境的介绍和创建
    基于jTemplates、ascx协同工作的酷UI模板技术
    我的.Subtext二次开发之路系列:无限层次分类
    我的.Subtext二次开发之路系列:引言
  • 原文地址:https://www.cnblogs.com/yun965861480/p/10839924.html
Copyright © 2011-2022 走看看