Eclipse(STS) 初次搭建Spring Cloud项目之断路器Hystrix(五)

  1. 在微服务架构中,根据业务来拆分成一个个的服务,在SpringCloud中用RestTemplate+Ribbon和Feign来实现互相调用。为了保证其高可用,单个服务通常会集群部署。
  2. 由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,Servlet容器的线程资源会被消耗完毕,导致服务瘫痪。
  3. 服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这就是服务故障的“雪崩”效应。
  4. 为了解决这个问题,业界提出了断路器模型。

一、什么是断路器

Netflix has created a library called Hystrix that implements the circuit breaker pattern. In a microservice architecture it is common to have multiple layers of service calls。 -------来自官网

当底层服务出现故障时会导致连锁故障。当对特定的服务的调用的不可用达到一个阀值(Hystric 是5秒20次) 断路器将会被打开。断路打开后,可用避免连锁故障,fallback方法可以直接返回一个固定值。

二、这里我们直接说Feign集成Hystrix

Feign本身就集成了Hystrix,只是默认为关闭状态,我们需要在配置文件中将其打开

feign:
  hystrix:
    enabled: true
复制代码

下面我们来看看代码如何写,其实很简单,使用上一篇文章中的cloud-demo-feign项目,application.yml内容如下:

server:
 port: 8811 # 你的端口
spring:
  application:
    name: feign
eureka:
  client:
    service-url:
      defaultZone: http://admin:admin@server1:8761/eureka/,http://admin:admin@server2:8762/eureka/
feign:
  hystrix:
    enabled: true
service-eureka-client:
  ribbon:
     NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
#com.netflix.loadbalancer.RandomRule #配置规则 随机
#com.netflix.loadbalancer.RoundRobinRule #配置规则 轮询
#com.netflix.loadbalancer.RetryRule #配置规则 重试
#com.netflix.loadbalancer.WeightedResponseTimeRule #配置规则 响应时间权重
#com.netflix.loadbalancer.BestAvailableRule #配置规则 最空闲连接策略
复制代码

项目结构如下:

新建feign断路器接口SchedualClientNameWithFallbackFactory和断路器的实现类HystrixClientFallbackFactory

package com.hewl.hystrix;

import com.hewl.feign.SchedualCilentName;

public interface SchedualClientNameWithFallbackFactory extends SchedualCilentName {

}

复制代码
package com.hewl.hystrix;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.hewl.feign.SchedualCilentName;

import feign.hystrix.FallbackFactory;

@Component
public class HystrixClientFallbackFactory implements FallbackFactory<SchedualCilentName> {
	private static final Logger log = LoggerFactory.getLogger(HystrixClientFallbackFactory.class);

	@Override
	public SchedualCilentName create(Throwable cause) {
		// TODO Auto-generated method stub
		return new SchedualClientNameWithFallbackFactory() {
			
			@Override
			public String testFromClientOne(String name) {
				log.error("fallback ,the result is : " + cause);
				// TODO Auto-generated method stub
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("ID", -1L);
				map.put("NAME","");
				return map.toString();
			}
		};
	}

}

复制代码

SchedualCilentName类做如下修改,feignclient注解添加fallbackfactory键值对

package com.hewl.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.hewl.hystrix.HystrixClientFallbackFactory;

@FeignClient(value="service-eureka-client",fallbackFactory = HystrixClientFallbackFactory.class)
public interface SchedualCilentName {
	
	@GetMapping(value = "/test")
	public String testFromClientOne(@RequestParam("name") String name);

}
复制代码

下面启动项目测试

  1. 启动eureka-server服务
  2. 启动eureka-client服务
  3. 启动feign服务

访问http://localhost:8811/test

熔断成功!!!

转载于:https://juejin.im/post/5d01f057f265da1bd04edac8

猜你喜欢

转载自blog.csdn.net/weixin_34244102/article/details/93181989