您的位置:首页 > 编程语言 > Java开发

Spring基础:快速入门spring cloud(5):断路器之Hystrix

2017-01-04 08:00 876 查看


Spring Cloud是Spring总多的Project中的一个,它提供了一整套的工具帮助系统架构师们在进行分布式设计的时候可以拿来即用, 在创建和发布微服务时极为便捷和有效。

本系列文章将会使用最简单的例子和最为容易的方式来学习Spring Cloud。本文将会介绍如何引入Netflix Hystrix在微服务的架构中如何实现断路器。

构成

项目详细
Config ServiceSpring Cloud Config:统一配置管理服务
Dashboard ServiceHystrix Dashboard
Api Route ServiceZuul:Api Gateway
Discovery ServiceEureka:服务发现
User ServiceRESTFUL的用户相关的服务
Org ServiceRESTFUL的组织相关的服务


断路器

>断路器本身是电路上的一种过载保护装置,当线路中有电器发生短路时,它能够及时的切断故障电路以防止严重后果发生。



而在系统架构之中同样的熔断机制也是需要的。



MatinFowler作过详细阐述,有兴趣的可以查看下面这篇文章。

URLhttp://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>


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);
}
}


设定文件

server.port=8101
spring.application.name=dashboardservice
eureka.client.serviceUrl.defaultZone=http://localhost:8801/eureka/


项目详细
server.portdashboardService提供服务所用Port
spring.application.name向Eureka Server进行注册时使用的服务名
eureka.client.serviceUrl.defaultZonehttp://localhost:8801/eureka/ 注意此处的8801端口号需要跟Server端一致。

生成Package

项目详细
命令mvn clean package
执行场所pom所在目录
目标文件所在目录工程根目录下Target
目标文件名称dashboardservice-0.0.1-SNAPSHOT.jar

访问方式

项目访问方法(URL)
hystrixdashboardhttp://localhost:8101/hystrix

启动

>启动各个服务后,也启动dashboardservice

Eureka确认



Hystrix Dashboard



总结

>本文简单说明了断路器的原理以及如何在项目中导入Turbine和Hystrix Dashboard。至于实现熔断的具体实例最好还是结合多节点多服务的更加接近实际环境的例子理解起来才更加容易,我们会在后续与容器以及PAAS管理平台K8S等的结合中继续展开。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息