zoukankan      html  css  js  c++  java
  • 断路器Feign

    Feign是自带断路器,需要在配置文件中开启断路器

    改造消费者项目(FeignDemo)

    1、在application.yml配置文件中开启断路器

    eureka:
      client:
        service-url:
          defaultZone: http://127.0.0.1:9000/eureka/
    server:
      port: 9004
    spring:
      application:
        name: cppdy-feign
    feign:
      hystrix:
        enabled: true

    2、创建HelloServiceImpl实现类

    package com.cppdy.service.impl;
    
    import org.springframework.stereotype.Component;
    
    import com.cppdy.service.HelloService;
    
    @Component
    public class HelloServiceImpl implements HelloService {
    
        @Override
        public String hello(String name) {
    
            return "Sorry,Server Error:" + name;
        }
    
    }

    3、在HelloService接口中设置断路器回调实现类

    package com.cppdy.service;
    
    import org.springframework.cloud.netflix.feign.FeignClient;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    
    import com.cppdy.service.impl.HelloServiceImpl;
    
    @FeignClient(value = "cppdy-hello", fallback = HelloServiceImpl.class)
    public interface HelloService {
    
        @RequestMapping("hello")
        String hello(@RequestParam("name") String name);
    }

     4、先启动EurekaDemo(注册中心项目),再启动FeignDemo(消费者项目)端口号设置为9004,访问http://localhost:9004/hello,会调用断路器设置的回调实现类中的方法

  • 相关阅读:
    noip2011 总结
    noip2010 总结
    noip2009 总结
    noip2008 总结
    noip2006总结
    noip2007 总结
    noip2006 总结
    【模板】线段树模板
    【数学】8.30题解-count数页码
    【数论】8.30题解-prime素数密度 洛谷p1835
  • 原文地址:https://www.cnblogs.com/jiefu/p/10056266.html
Copyright © 2011-2022 走看看