Spring基礎 快速入門spring cloud(5) 斷路器之Hystrix
阿新 • • 發佈:2018-12-14
Spring Cloud是Spring總多的Project中的一個,它提供了一整套的工具幫助系統架構師們在進行分散式設計的時候可以拿來即用, 在建立和釋出微服務時極為便捷和有效。
本系列文章將會使用最簡單的例子和最為容易的方式來學習Spring Cloud。本文將會介紹如何引入Netflix Hystrix在微服務的架構中如何實現斷路器。
構成
專案 | 詳細 |
---|---|
Config Service | Spring Cloud Config:統一配置管理服務 |
Dashboard Service | Hystrix Dashboard |
Api Route Service | Zuul:Api Gateway |
Discovery Service | Eureka:服務發現 |
User Service | RESTFUL的使用者相關的服務 |
Org Service | RESTFUL的組織相關的服務 |
斷路器
>斷路器本身是電路上的一種過載保護裝置,當線路中有電器發生短路時,它能夠及時的切斷故障電路以防止嚴重後果發生。
而在系統架構之中同樣的熔斷機制也是需要的。
MatinFowler作過詳細闡述,有興趣的可以檢視下面這篇文章。
URL | http://martinfowler.com/bliki/CircuitBreaker.html |
---|
Pom詳細
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.liumiaocn.demo.springcloud</groupId>
<artifactId>dashboardservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>dashboardservice</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-turbine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.BUILD-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
dashboardservice
需要加入EnableTurbine和EnableHystrixDashboard註解。
package com.liumiaocn.demo.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
import org.springframework.web.bind.annotation.RequestMapping;
@SpringBootApplication
@EnableEurekaClient
@EnableTurbine
@EnableHystrixDashboard
public class DashboardserviceApplication {
@RequestMapping("/")
public String home() {
return "forward:/hystrix";
}
public static void main(String[] args) {
SpringApplication.run(DashboardserviceApplication.class, args);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
設定檔案
server.port=8101
spring.application.name=dashboardservice
eureka.client.serviceUrl.defaultZone=http://localhost:8801/eureka/
- 1
- 2
- 3
專案 | 詳細 |
---|---|
server.port | dashboardService提供服務所用Port |
spring.application.name | 向Eureka Server進行註冊時使用的服務名 |
eureka.client.serviceUrl.defaultZone | http://localhost:8801/eureka/ 注意此處的8801埠號需要跟Server端一致。 |
生成Package
專案 | 詳細 |
---|---|
命令 | mvn clean package |
執行場所 | pom所在目錄 |
目標檔案所在目錄 | 工程根目錄下Target |
目標檔名稱 | dashboardservice-0.0.1-SNAPSHOT.jar |
訪問方式
專案 | 訪問方法(URL) |
---|---|
hystrixdashboard | http://localhost:8101/hystrix |
啟動
>啟動各個服務後,也啟動dashboardservice
Eureka確認
Hystrix Dashboard
總結
>本文簡單說明了斷路器的原理以及如何在專案中匯入Turbine和Hystrix Dashboard。至於實現熔斷的具體例項最好還是結合多節點多服務的更加接近實際環境的例子理解起來才更加容易,我們會在後續與容器以及PAAS管理平臺K8S等的結合中繼續展開。
再分享一下我老師大神的人工智慧教程吧。零基礎!通俗易懂!風趣幽默!希望你也加入到我們人工智慧的隊伍中來!http://www.captainbed.net