hystrix 配置 不生效_12、Feign整合断路器Hystrix

本文介绍了如何在Spring Cloud项目中使用Feign整合断路器Hystrix。Feign默认内置了Hystrix支持,但需要额外配置才能启用。通过创建新项目,设置pom.xml依赖,编写启动类,配置bootstrap.yml和application.yml,并定义服务消费者类,实现了Feign客户端的Hystrix断路器功能。
摘要由CSDN通过智能技术生成

上编说了《RestTemplate+Ribbon整合断路器Hystrix》,这篇来看看如何Feign整合断路器Hystrix,Feign整合断路器Hystrix也是相对比较简单的。Feign默认已经自带断路器Hystrix,所以不需要像RestTemplate+Ribbon整合断路器Hystrix那样需要在SpringBoot的启动类添加注解。但是Feign自带断路器并没有打开,需要做些额外的配置。

feign:

hystrix:

enabled: true

1、 新建项目sc-eureka-client-consumer-feign-hystrix,对应的pom.xml文件如下

"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">

4.0.0

spring-cloud

sc-eureka-client-consumer-feign

0.0.1-SNAPSHOT

jar

sc-eureka-client-consumer-feign

http://maven.apache.org

org.springframework.boot

spring-boot-starter-parent

2.0.4.RELEASE

org.springframework.cloud

spring-cloud-dependencies

Finchley.RELEASE

pom

import

UTF-8

1.8

1.8

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

org.springframework.boot

spring-boot-starter-web

org.springframework.cloud

spring-cloud-starter-openfeign

备注:从继续关系可以看出spring-cloud-starter-openfeign已经集成断路器Hystrix

d3375e5e9e78a2631929b54e6e73e88f.png

2、新建springboot启动类

package sc.consumer;

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 ConsumerFeignApplication {

public static void main(String[] args) {

SpringApplication.run(ConsumerFeignApplication.class, args);

}

}

3、 新建配置文件bootstrap.yml和application.yml

bootstrap.yml

server:

port: 5800

application.yml

spring:

application:

name: sc-eureka-client-consumer-feign-hystrix

eureka:

client:

registerWithEureka: true #是否将自己注册到Eureka服务中,默认为true

fetchRegistry: true #是否从Eureka中获取注册信息,默认为true

serviceUrl:

defaultZone: http://localhost:5001/eureka/

feign:

hystrix:

enabled: true

说明:在application.yml配置文件添加了开启断路器Hystrix的配置项

750ea5a48557f7f7e79c777be7e11b40.png

4、新建服务消费者类UserService.java

package sc.consumer.service;

import java.util.Map;

import org.springframework.cloud.openfeign.FeignClient;

import org.springframework.web.bind.annotation.DeleteMapping;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.PutMapping;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestMapping;

import sc.consumer.model.User;

import sc.consumer.service.hystrix.UserServiceHystrix;

@FeignClient(value="sc-eureka-client-provider

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值