• ## feign远程调用开启熔断


    消费方

    server.port=80
    spring.application.name=consume-eureka
    eureka.client.service-url.defaultZone= http://localhost:10086/eureka/
    feign.hystrix.enabled=true  ##feign开启熔断
    
    
    //启用feign远程调用不需要引入RestTemplate调用方式
    
    @SpringBootApplication
    @EnableDiscoveryClient //开启加入eureka注册中心
    @EnableCircuitBreaker//开启熔断
    @EnableFeignClients//开起Feign远程调用
    public class ConsumeEurekaApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ConsumeEurekaApplication.class, args);
        }
    
      //  @Bean
      //  @LoadBalanced //开启ribbon负载均衡
      //  public RestTemplate restTemplate(){
      //      return new RestTemplate();
      //  }
    }
    
    
    //feign启用熔断之后原来的熔断就不需要了 直接 定义 indexClient调用接口
    
    @RestController
    //@DefaultProperties (defaultFallback = "fallback") //全局容错
    public class LoginController {
        //@Autowired
        // private RestTemplate restTemplate;
        @Autowired
        private IndexClient indexClient;
    
        @GetMapping("login")
        //@HystrixCommand
        public String login(@RequestParam String message){
          //  System.out.println("restTemplate请求服务时直接使用服务名进行请求");
          //  return restTemplate.getForObject("http://serve-eureka/index/"+message,String.class);
    
            System.out.println("通过feign远程调用服务端");
            return indexClient.index(message);
        }
    
      //  public String fallback(){
      //    return "请求超时";
      //  }
    }
    
    
    //"serve-eureka为远程调用的服务名 
    //IndexClientFallBack为熔断方法 此方法时是实现IndexClient接口的
    // String index(@PathVariable("message") String message);此方法为serve-eureka服务中要调用的方法
    
    @FeignClient(value = "serve-eureka",fallback = IndexClientFallBack.class)//声明一个接口时feign接口,接口中的方法为要《《远程调用的方法》》
    public interface IndexClient {
        @GetMapping("index/{message}")
         String index(@PathVariable("message") String message);
    
    
    }
    
    //此为熔断方法
    //@Component此注解是将实现交由spring管理
    @Component
    public class IndexClientFallBack implements IndexClient {
        @Override
        public String index(String message) {
            return "请求超时";
        }
    }
    feign 默认开启负载均衡
    
    代码使世界更精彩
  • 相关阅读:
    软件过程的守护神
    C++一个简单的手柄类模板
    矿Spring入门Demo
    [Docker] Prune Old Unused Docker Containers and Images
    [Ramda] Compose lenses
    [Docker] Build Your Own Custom Docker Image
    [SCSS] Reuse Styles with the SCSS @mixin Directive
    [Angular] Router outlet events
    [Angular] Subscribing to router events
    [Angular] Enable router tracing
  • 原文地址:https://www.cnblogs.com/lgx123/p/14863494.html
Copyright © 2020-2023  润新知