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

这里写图片描述

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等的结合中继续展开。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值