spring-cloud feign(二)熔断器

Feign Hystrix
​ Feign Hystrix熔断只是作用在服务调用这一端,因此我们代码只需要改动lwp-feign-consumer项目相关代码就可以。因为,Feign中已经依赖了Hystrix所以在maven配置上不用做任何改动。

https://www.jb51.net/article/134180.htm

Hystrix特性**

1.断路器机制

断路器很好理解, 当Hystrix Command请求后端服务失败数量超过一定比例(默认50%), 断路器会切换到开路状态(Open). 这时所有请求会直接失败而不会发送到后端服务. 断路器保持在开路状态一段时间后(默认5秒), 自动切换到半开路状态(HALF-OPEN). 这时会判断下一次请求的返回情况, 如果请求成功, 断路器切回闭路状态(CLOSED), 否则重新切换到开路状态(OPEN). Hystrix的断路器就像我们家庭电路中的保险丝, 一旦后端服务不可用, 断路器会直接切断请求链, 避免发送大量无效请求影响系统吞吐量, 并且断路器有自我检测并恢复的能力.

2.Fallback

Fallback相当于是降级操作. 对于查询操作, 我们可以实现一个fallback方法, 当请求后端服务出现异常的时候, 可以使用fallback方法返回的值. fallback方法的返回值一般是设置的默认值或者来自缓存.

3.资源隔离

在Hystrix中, 主要通过线程池来实现资源隔离. 通常在使用的时候我们会根据调用的远程服务划分出多个线程池. 例如调用产品服务的Command放入A线程池, 调用账户服务的Command放入B线程池. 这样做的主要优点是运行环境被隔离开了. 这样就算调用服务的代码存在bug或者由于其他原因导致自己所在线程池被耗尽时, 不会对系统的其他服务造成影响. 但是带来的代价就是维护多个线程池会对系统带来额外的性能开销. 如果是对性能有严格要求而且确信自己调用服务的客户端代码不会出问题的话, 可以使用Hystrix的信号模式(Semaphores)来隔离资源.

一、fallback使用熔断器类

1.配置文件

application.properties 或 application.yml添加配置:

feign.hystrix.enabled=true

2. feign接口代码

//熔断器
@FeignClient(name="PROVIDER", fallback=HelloClientServiceHystrix.class)
public interface HelloClientService extends HelloClient{
	@RequestMapping(method = RequestMethod.GET, value = "/hello/{userId}")
	String hello(@PathVariable String userId);
}

3.熔断代码

@Component必须使用

@Component
public class HelloClientServiceHystrix implements HelloClientService{

    @Override
    public String hello(String userId) {
        System.out.println("HelloClientServiceHystrix doing ...");
        return "HelloClientServiceHystrix result";
    }
}

4.contraller

@Autowired
private HelloClientService helloClientService;

@GetMapping("/hello")
public String hello() {
	return helloClientService.hello(String.valueOf(System.currentTimeMillis()));
}

5.测试

启动注册中心

启动feign-consumer(feign所在的服务调用端)

请求http://localhost:port/hello后服务端返回

HelloClientServiceHystrix result

二、使用熔断器工厂

1.配置文件

application.properties 或 application.yml添加配置:

feign.hystrix.enabled=true

2. feign接口代码

//熔断器
@FeignClient(name="PROVIDER", fallback=HelloClientServiceHystrixFactory.class)
public interface HelloClientService extends HelloClient{
	@RequestMapping(method = RequestMethod.GET, value = "/hello/{userId}")
	String hello(@PathVariable String userId);
}

3.熔断代码

  1. @Component必须使用
  2. 实现feign.hystrix.FallbackFactory接口
@Component
public class HelloClientServiceHystrixFactory implements FallbackFactory<HelloClientService> {

	@Override
	public HelloClientService create(Throwable cause) {
		System.out.println(String.format("fallback reason was: %s ", cause.getMessage()));
		// 实现接口或者继承此接口的子接口的实现类即可
		return new HelloClientService() {
			
			@Override
			public String hello(String userId) {
				System.out.println("HelloClientServiceHystrixFactory do something ...");
				return "HelloClientServiceHystrixFactory result";
			}
		};
	}

}

4.contraller

@Autowiredprivate HelloClientService helloClientService;

@GetMapping("/hello")
public String hello() {    return 	helloClientService.hello(String.valueOf(System.currentTimeMillis()));
}

5.测试

启动注册中心

启动feign-consumer(feign所在的服务调用端)

请求http://localhost:port/hello后服务端返回后服务端返回:

控制台输出

fallback reason was: com.netflix.client.ClientException: Load balancer does not have available server for client: PROVIDER 
HelloClientServiceHystrixFactory do something ...

浏览器返回

HelloClientServiceHystrixFactory result

猜你喜欢

转载自blog.csdn.net/lanwp5302/article/details/86000193
今日推荐