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监控
监控页面说明
阅读全文
1 0
- spring cloud 入门实践系列
- spring cloud 入门实践系列
- spring cloud 入门实践系列
- spring cloud 入门实践系列
- spring cloud 入门实践系列
- spring cloud 入门实践系列
- Spring Cloud 入门系列总结
- SpringBoot系列—Spring Cloud快速入门
- Spring Cloud Security系列教程一:入门
- Spring Cloud Config 实践
- spring cloud系列
- spring cloud 系列文章
- spring-cloud系列-Eureka
- spring cloud入门基础
- Spring cloud入门
- spring cloud入门学习
- Spring Cloud 入门
- Spring Cloud Eureka 入门 (一)服务注册中心详解 「Spring Cloud Eureka 入门系列」 Spring Cloud Eureka 入门 (一)服务注册中心详解 Spr
- c语言笔记6-函数
- freemarker如何获取当前时间或者时间戳?
- C++之让接口容易被正确使用,不易被误用(18)---《Effective C++》
- solr和mongodb 比较
- Vue 2.0 vue-router基础学习(一)
- spring cloud 入门实践系列
- SpringMVC 4.0 确定目标方法POJO类型入参的过程
- Mac系统下eclipse中的自动代码提示
- 二叉树的用途之一二叉搜索树
- 阿里云上线视频云剪辑 快速产出PGC短视频不再是问题!
- HDU-1195-Open the Lock(BFS)
- JS事件和CSS媒体查询对同一元素操作样式发生冲突解决方案
- vue2.0+vue-router2.0+axios+webpack开发webapp项目(二)
- SWERC 2016 F题(dfs序+主席树)