断路器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,会调用断路器设置的回调实现类中的方法

猜你喜欢

转载自www.cnblogs.com/cppdy/p/10056266.html