spring cloud feign客户端熔断器

spring cloud feign客户端熔断器

1. pom部分

父pom dependencyManagement

 <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>
                    org.springframework.cloud
                </groupId>
                <artifactId>
                    spring-cloud-dependencies
                </artifactId>
                <version>
                    Greenwich.RELEASE
                </version>
                <type>
                    pom
                </type>
                <scope>
                    import
                </scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

feign客户端pom


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
    </dependencies>

2.application.properties

server.port=8091
spring.application.name=cloud4
#注册服务的地址
eureka.client.service-url.defaultZone=http://localhost:8080/eureka/
#开启熔断
feign.hystrix.enabled=true
#设置熔断器超时时间
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=60000
#设置ribbon超时时间
ribbon.ReadTimeout=2000
ribbon.ConnectTimeout=2000

3.代码部分

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class SpringCloud4Application {

    public static void main(String[] args) {

        SpringApplication.run(SpringCloud4Application.class,args);
    }
}

Retryer 为重试机制配置

@Configuration
public class FeignConfig {

    /**
     * 参数一:为下次发起重试请求 生成间隔时间算法的参数(时间单位:毫秒)
     * 参数二:距下次发起重试请求最大的间隔时间(时间单位:毫秒)
     * 参数三:重试次数
     * @return
     */
    @Bean
    public Retryer feignRetryer() {
        return new Retryer.Default(100, SECONDS.toMillis(1), 5);
    }
}

@FeignClient(value = "cloud1", configuration = FeignConfig.class, fallback = FeignServiceHystrixComponent.class)
public interface FeignService {

    @GetMapping("test/a")
    public String a();
}

@Component
public class FeignServiceHystrixComponent implements FeignService {

    @Override
    public String a() {
        return "error";
    }
}
@Service
public class TestService {

    @Autowired
    FeignService feignService;

    public String a(){

        return feignService.a();
    }
}
@RestController
@RequestMapping("test")
public class TestController {

    @Autowired
    TestService testService;

    @GetMapping("a")
    public ResponseEntity a(){

        return ResponseEntity.ok(testService.a());
    }

}
发布了43 篇原创文章 · 获赞 6 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43866295/article/details/87994758