Spring Cloud配置(四)熔断器

在Spring Cloud中使用了Hystrix 来实现断路器的功能。Hystrix是Netflix开源的微服务框架套件之一,该框架目标在于通过控制那些访问远程系统、服务和第三方库的节点,从而对延迟和故障提供更强大的容错能力。Hystrix具备拥有回退机制和断路器功能的线程和信号隔离,请求缓存和请求打包,以及监控和配置等功能。

以下分别在ribbon以及feign中配置断路器Hystrix

一、feign中配置Hystrix

feign是自带断路器功能的,并且默认打开,如果你要关闭的话,需要加上这个配置:

feign:
    hystrix:
        enabled: false

以之前创建的消费者 service-consumer-feign为例子配置:

1) 这里只需要在@FeignClient注解上加上fallback就可以了,如下:

package com.sam.service.consumer.feign;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @ClassName: ServiceAFeignClient 
 * @Description: 服务客户端实现 
 * @author sam 
 * @date 2018年8月10日 下午4:00:42
 */
@Component
@FeignClient(value = "service-producer", fallback = ServiceAFeignClientFallback.class) // 这里的name对应调用服务的spring.applicatoin.name
public interface ServiceAFeignClient {

	@RequestMapping(value = "/hi")
	String hi(@RequestParam("id") String id);

}

2) 同时创建熔断处理类:ServiceAFeignClientFallback

package com.sam.service.consumer.feign;

import org.springframework.stereotype.Component;

/**
 * @ClassName: ServiceAFeignClientFallback 
 * @Description: 熔断回调类 
 * @author sam 
 * @date 2018年8月9日 下午5:24:52
 */
@Component
public class ServiceAFeignClientFallback implements ServiceAFeignClient {

	@Override
	public String hi(String id) {
		return "hi, " + id + ", error!";
	}

}

配置之后,访问消费者:service-consumer-feign  http://localhost:8911/hi?id=123  可以查看信息:

把生产者 service-producer-A等都停止,再刷新访问,看到熔断提示:

 

二、ribbon中配置Hystrix

以之前创建的消费者 service-consumer-ribbon为例子配置:

1)pom.xml中添加 

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

2)在启动类ClientApplicationRibbon中添加@EnableHystrix开启hystrix功能,如下:

package com.sam.service.consumer.ribbon;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * @ClassName: ClientApplicationRibbon 
 * @Description: 消费者服务(使用Ribbon实现)
 * @author sam 
 * @date 2018年8月10日 下午4:01:21
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class ClientApplicationRibbon {

	public static void main(String[] args) {
		SpringApplication.run(ClientApplicationRibbon.class, args);
	}

	@Bean
	@LoadBalanced
	RestTemplate restTemplate() {
		return new RestTemplate();
	}
}

3)Controller中, 使用@HystrixCommand指定回调方法,如下:

package com.sam.service.consumer.ribbon;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

/**
 * @ClassName: TestController
 * @Description: Ribbon+RestTemplate方式
 * @author sam
 * @date 2018年8月9日 下午4:17:10
 */
@RestController
public class TestController {

	@Autowired
	RestTemplate restTemplate;

	@RequestMapping("/hi")
	@HystrixCommand(fallbackMethod = "hiFallback")
	public String hi(@RequestParam String id) {
		return restTemplate.getForObject("http://service-producer/hi?id=" + id, String.class);
	}

	// 断路器回调方法
	public String hiFallback(String id) {
		return "hi, " + id + ", error!";
	}
}

以上配置完成后,停止生产者,亦可以实现和fergn一样的熔断器效果!

 

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud配置熔断器可以使用断路器来实现容错机制。首先,您需要在pom.xml文件中添加以下依赖项: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> ``` 接下来,在您的应用程序主类上添加@EnableCircuitBreaker注解,以启用断路器功能。然后,在您希望应用断路器的方法上添加@HystrixCommand注解,以定义断路器的行为。您可以在@HystrixCommand注解中指定fallbackMethod,以在触发熔断时调用备用方法。 例如,以下是一个使用Spring Cloud熔断器配置的示例: ```java import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.netflix.hystrix.EnableHystrix; @SpringBootApplication @EnableCircuitBreaker public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } } ``` ```java import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; @Service public class YourService { private RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "fallbackMethod") public String yourMethod() { // 调用其他服务的代码 } public String fallbackMethod() { // 备用方法的实现 } } ``` 通过这种方式,您可以配置Spring Cloud熔断器来处理服务故障,并提供备用方法来处理熔断情况。这样可以确保系统在发生故障时仍然能够提供可靠的服务。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Spring Cloud Gateway熔断限流配置](https://blog.csdn.net/exception_class/article/details/130237653)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [Spring Cloud配置()熔断器](https://blog.csdn.net/vtopqx/article/details/81746084)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值