spring cloud 入门实践系列

来源:互联网 发布:linux常用的命令有哪些 编辑:程序博客网 时间:2024/06/05 14:38

Hystrix is a latency and fault tolerance library designed to isolate points of access to remote systems, services and 3rd party libraries, stop cascading failure and enable resilience in complex distributed systems where failure is inevitable.
Hystrix是一个延迟和容错库,旨在隔离对远程系统,服务和第三方库的访问点,在复杂的分布式系统中,当故障是不可避免时,阻止级联故障和启用恢复能力。

Circuit Breaker   断路器

Hystrix在微服务架构中的位置

微服务架构

这里写图片描述

Hystrix在微服务架构位置

这里写图片描述

Hystrix阻止了级联故障

Hystrix使用

创建spring boot工程

这里写图片描述

配置pom文件引入相关jar包

<?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.zhang</groupId>    <artifactId>hystrixtest</artifactId>    <version>0.0.1-SNAPSHOT</version>    <packaging>jar</packaging>    <name>hystrixtest</name>    <description>Demo project for Spring Boot</description>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.4.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>        <spring-cloud.version>Dalston.SR1</spring-cloud.version>    </properties>    <dependencies>        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-eureka</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-actuator</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-hystrix</artifactId>        </dependency>    </dependencies>    <dependencyManagement>        <dependencies>            <dependency>                <groupId>org.springframework.cloud</groupId>                <artifactId>spring-cloud-dependencies</artifactId>                <version>${spring-cloud.version}</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></project>

配置文件application.yml

eureka:  client:    serviceUrl:      defaultZone: http://localhost:8761/eureka/server:  port: 8768spring:  application:    name: service-hello

Application代码

package com.zhang;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.netflix.hystrix.EnableHystrix;import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;import org.springframework.stereotype.Component;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import java.util.Map;@RestController@EnableHystrixDashboard@EnableHystrix@EnableEurekaClient@SpringBootApplicationpublic class HystrixtestApplication {    public static void main(String[] args) {        SpringApplication.run(HystrixtestApplication.class, args);    }    @RequestMapping("/test")    @HystrixCommand(fallbackMethod = "defaultStores")    public String test(@RequestParam String name){        if("zxl".equals(name)){            int a=1/0;        }        return "hello "+name+",this is test";    }    public String defaultStores(String name) {        return "这是断路后返回结果";    }}

测试Hystrix是否生效

1 启动eureka注册中心,也即启动eureka服务端,可参见之前代码
2 启动HystrixtestApplication

这里写图片描述

3 url中输入测试链接 http://172.16.153.1:8768/test?name=zxla

这里写图片描述

4 url中输入测试链接 http://172.16.153.1:8768/test?name=zxl

这里写图片描述

5 Hystrix监控配置

这里写图片描述

http://172.16.153.1:8768/hystrix.stream 是长链接心跳

这里写图片描述

6 Hystrix监控

这里写图片描述

监控页面说明

这里写图片描述

原创粉丝点击