Spring基础:快速入门spring cloud(5):断路器之Hystrix
2017-01-04 08:00
876 查看
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>
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.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 |
启动
>启动各个服务后,也启动dashboardserviceEureka确认
Hystrix Dashboard
总结
>本文简单说明了断路器的原理以及如何在项目中导入Turbine和Hystrix Dashboard。至于实现熔断的具体实例最好还是结合多节点多服务的更加接近实际环境的例子理解起来才更加容易,我们会在后续与容器以及PAAS管理平台K8S等的结合中继续展开。相关文章推荐
- spring cloud快速入门教程(七)服务断路器Hystrix
- Spring基础:快速入门spring cloud(2):服务发现之eureka
- Spring基础:快速入门spring cloud(4):API网关之Zuul
- spring基础:快速入门spring cloud(6):统一配置管理之spring cloud config
- Spring基础:快速入门spring cloud(1):Spring Cloud介绍
- Spring基础:快速入门spring cloud(3):业务逻辑服务
- Spring基础:快速入门spring boot(6):用docker进行Build/Ship/Run
- Spring基础:快速入门spring(8):bean scope
- Spring基础:快速入门spring(10):Java配置注入
- Spring基础:快速入门spring boot(3):创建Restful的Web服务
- Spring基础:快速入门spring(6):properties文件方式的值注入
- SpringCloud入门4:断路器(Hystrix)
- Spring基础:快速入门spring boot(5):Spring中的Cron--Scheduling
- Spring基础:快速入门spring(4):setter注入
- Spring Cloud 入门教程(八): 断路器指标数据监控Hystrix Dashboard 和 Turbine
- Spring Cloud入门教程-Hystrix断路器实现容错和降级
- Spring基础:快速入门spring boot(1):CLI方式的Helloworld
- Spring基础:快速入门spring(1):基础概念
- Spring基础:快速入门spring(7):bean的lifecyle
- Spring基础:快速入门spring(3):构造注入