Hystrix熔斷機制就像家里的保險絲一樣,若同時使用高功率的電器,就會燒壞電路,這時候保險絲自動斷開就有效的保護了電路。而我們程序中也同樣是這樣。例如若此時數據庫壓力太大速度很慢,此時還有不斷的請求訪問后台,就會造成數據庫崩潰。這時候hystrix容錯機制,可以為客戶端請求設置超時鏈接,添加回退的邏輯,減少集群壓力。
1.1 導依賴
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/>
</parent>
<!-- springCloud -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka客戶端依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
1.2 配置application.yml
# 指定端口
server:
port: 8085
context-path: /demo
# 服務名稱
spring:
application:
name: hystrix
# eureka服務器地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
1.3 配置Hystrix過濾器
import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext; /** * urlPatterns:攔截所有路徑(攔截規則) * filterName:過濾器名稱 */ @WebFilter(urlPatterns = "/*", filterName = "hystrixFilter") public class HystrixFilterConf implements Filter{ public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // 初始化Hystrix上下文
HystrixRequestContext ctx = HystrixRequestContext.initializeContext(); try { chain.doFilter(request, response); } catch (Exception e) { } finally { ctx.shutdown(); } } public void init(FilterConfig arg0) throws ServletException { } }
1.4 主函數入口
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient //開啟Eureka
@EnableCircuitBreaker//開啟斷路器
@ServletComponentScan//掃描servlet過濾器監聽器
public class ServerMain { public static void main(String[] args) { new SpringApplicationBuilder(ServerMain.class).web(true).run(args); } }
二: hystrix 的 熔斷,降級 機制。
2.1 回退機制案例
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.RequestMapping; /** * Hystrix回退機制(熔斷,降級) */ @Service public class RollbackService { /** * fallbackMethod: 指定回退方法 * * coreSize: 線程池最大數量 * maxQueueSize: 線程池最大隊列,默認-1通過SynchronousQueue來實現; 否則使用LinkedBlockingQueue實現 * queueSizeRejectionThreshold: 當maxQueueSize是LinkedBlockingQueue時,即使沒有達到最大列隊也會據絕請求。 * * timeoutInMilliseconds: 超時時間 * requestVolumeThreshold: 單位時間內超過這個多個請求失敗才執行熔斷 */ @RequestMapping(value = "/testRollback") @HystrixCommand(fallbackMethod = "myRollback", threadPoolProperties = { @HystrixProperty(name = "coreSize", value = "30"), @HystrixProperty(name = "maxQueueSize", value = "-1"), @HystrixProperty(name = "queueSizeRejectionThreshold", value = "-1") }, commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000"), @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "1") }) public String testRollback() { try {// 模擬請求阻塞
Thread.sleep(4000); } catch (Exception e) { } return "恭喜你訪問成功!"; } /** 回退方法 */
public String myRollback() { return "服務器壓力過大,明年再來吧!"; } }
2.2 編寫接口, 調用測試。
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class RollbackController { @Autowired private RollbackService rollbackService; @RequestMapping("/testRollback") public String testRollback() { return rollbackService.testRollback(); } }
三: hystrix 緩存機制:hystrix的緩存有點像mybatis默認開啟的一級緩存:在session關閉之前(一次會話期間),使用同樣的參數調用同一個方法,實際只查詢一次。
3.1 緩存邏輯
import org.springframework.stereotype.Service; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheRemove; import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult; @Service public class CacheService { @CacheResult @HystrixCommand public void cacheMember(Integer id) { System.out.println("調用 cacheMember 方法"); } /** * commandKey:緩存的key * 獲取和刪除必須用同一個key,並必須是同一次請求。 */ @CacheResult @HystrixCommand(commandKey = "myCacheKey") public void getCache(Integer id) { System.out.println("執行查詢方法"); } @CacheRemove(commandKey = "myCacheKey") @HystrixCommand public void removeCache(Integer id) { System.out.println("刪除緩存方法"); } }
3.2 緩存接口開發
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class CacheController { @Autowired private CacheService cacheService; @RequestMapping(value = "/cache", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public String cache() { for(int i = 0; i < 3; i++) { /** 在同一次請求里面調用了3次同一方法,會發現,控制台只 * 輸出了一次,說明后2次走的緩存沒調方法的邏輯 */ cacheService.cacheMember(1); } System.out.println("測試完畢"); return ""; } @RequestMapping(value = "/rc", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public String testRemoveCache() { cacheService.getCache(1); cacheService.getCache(1); cacheService.removeCache(1); System.out.println("######### 分隔線 ###########"); cacheService.getCache(1); System.out.println("測試完畢"); return ""; } }