接着上面的基礎來寫hystrix熔斷器
至于為什麼用熔斷器?
比如A服務需要同時調用B、C、D的服務,
如果其中D服務出現問題,那麼線程就會阻塞在這裡,如果有大量的請求進來,就會導緻A服務也會癱瘓。(俗稱雪崩)
而hystrix就是為了讓出現異常的 服務做自動降級處理,快速傳回。
一、hystrix和ribbon配合使用
1.1 在之前代碼的基礎上,在ribbon-client服務的pom檔案中添加hystrix的jar
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
1.2 在啟動類上添加注解@EnableHystrix
package com.example.ribbonclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
public class RibbonClientApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonClientApplication.class, args);
}
}
1.3 修改ribbon的服務調用方法
在調用方法上加上@HystrixCommand注解,fallbackMethod 定義異常回調方法,
如果有需要排除不需要熔斷的異常可以添加 ignoreExceptions 屬性
package com.example.ribbonclient.controller;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class RibbonController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/index")
@HystrixCommand(fallbackMethod = "indexHistrxErrorMethod"
// ,ignoreExceptions = {ArithmeticException.class}
)
public String index(){
return restTemplate.getForObject("http://eureka-client/index",String.class);
}
private String indexHistrxErrorMethod(){
return "服務異常,自動降級處理";
}
}
1.4 為測試熔斷機制,修改eureka-client 8763服務的代碼
在serviceimpl中添加異常代碼後重新開機
package com.example.eurekaclient.service.impl;
import com.example.eurekaclient.service.IndexService;
import org.springframework.stereotype.Service;
@Service
public class IndexServiceImpl implements IndexService {
@Override
public String getMesasge() {
int number = 1/0;
return "hello springcloud 8763";
}
}
1.5 啟動ribbon-client服務進行測試
二、hystrix和feign配合使用
之前說過feign中已經整合了hystrix,是以不需要再添加hystrix的jar依賴,隻需要在配置檔案中将熔斷機制打開就可以了
1.1 在feign-client服務的application.yml檔案中添加
#打開服務熔斷機制
feign.hystrix.enabled=true
1.2 在服務調用的方法的注解裡加上熔斷回調的類
package com.example.feignclient.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient(value = "eureka-client",fallback = EurekaClientServiceHystrixClientFallback.class)
public interface EurekaClientService {
@GetMapping("/index")
String indexMessage();
}
1.3 建立這個類,并實作目前接口
package com.example.feignclient.service;
import org.springframework.stereotype.Component;
@Component
public class EurekaClientServiceHystrixClientFallback implements EurekaClientService {
@Override
public String indexMessage() {
return "服務異常,自動降級處理";
}
}
1.4 如果需要通路引起服務降級的原因,則可以使用
@FeignClient
中的
fallbackFactory
屬性。(這裡略過,貼上使用說明)
@FeignClient(name = "hello", fallbackFactory = HystrixClientFallbackFactory.class)
protected interface HystrixClient {
@RequestMapping(method = RequestMethod.GET, value = "/hello")
Hello iFailSometimes();
}
@Component
static class HystrixClientFallbackFactory implements FallbackFactory<HystrixClient> {
@Override
public HystrixClient create(Throwable cause) {
return new HystrixClient() {
@Override
public Hello iFailSometimes() {
return new Hello("fallback; reason was: " + cause.getMessage());
}
};
}
}
1.5 運作測試
源碼位址: https://github.com/houfanGitHub/springcloud.git