六:Hystrix容错监控机制

什么是微服务的容错机制

提前预设解决方案、,系统自主调节,遇到问题即时处理

什么是Hystrix

Netfix

设计原则:

  • 服务隔离机制
  • 服务降级
  • 熔断机制
  • 提供实时的监控和报警功能
  • 提供实事的配置修改功能

1.创建一个模块,配置环境

<?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">
<parent>
<artifactId>springCloud01</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>hystrix</artifactId> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
</dependencies>
</project>

2.application.yml

server:
port: 8060
spring:
application:
name: hystrix
eureka:
client:
service-url:
default: http://localhost:8761/eureka
instance:
prefer-ip-address: true
feign:
hystrix:
enabled: true
management:
endpoints:
web:
exposure:
include: 'hystrix.stream'

3.创建启动类

package com.southwind;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker
public class hystrixApplication {
public static void main(String[] args) {
SpringApplication.run(hystrixApplication.class,args);
}
}

4.实体类:

package com.southwind.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor; @Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private Integer id;
private String name; }

5.接口:

package com.southwind.fegin;

import com.southwind.entity.Student;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*; import java.util.Collection; @FeignClient(value = "provider")
public interface FeignProviderClient {
@GetMapping("/provider/findall")
public Collection<Student> findall();
@GetMapping("/provider/findbyid/{id}")
public Student findbyid(@PathVariable("id") Integer id);
@PostMapping("/provider/save")
public void save(@RequestBody Student student);
@DeleteMapping("/provider/delete/{id}")
public void delete(@PathVariable("id") Integer id);
}

6.controller

package com.southwind.Handler;

import com.southwind.entity.Student;
import com.southwind.fegin.FeignProviderClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import java.util.Collection; @RestController
@RequestMapping("/hystrix")
public class FeignHandler {
@Autowired
private FeignProviderClient feignProviderClient; @GetMapping("/findall")
public Collection<Student> findall(){
return feignProviderClient.findall();
}
@GetMapping("/findbyid/{id}")
public Student findbyid(@PathVariable("id") Integer id){
return feignProviderClient.findbyid(id);
}
@PostMapping("/save")
public void save(@RequestBody Student student){
feignProviderClient.save(student);
}
@DeleteMapping("/delete")
public void deletebyid(Integer id){
feignProviderClient.delete(id);
}
}

数据监控的url

7.添加可是化页面组件

<!--        可视化依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>

8.启动类添加组件

@EnableHystrixDashboard

package com.southwind;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker
@EnableHystrixDashboard
public class hystrixApplication {
public static void main(String[] args) {
SpringApplication.run(hystrixApplication.class,args);
}
}

http://localhost:8060/hystrix 可视化界面url

最新文章

  1. js的url解析函数封装
  2. vsftpd移植
  3. 使用Spark分析拉勾网招聘信息(二): 获取数据
  4. 很励志的帖子,转来自勉,也反省一下自己写码这几年【奋斗10年,一个.NET程序员从0到拥有5系】
  5. sqlserver 查询库中有多少张表
  6. PythonChallenge 2:爬虫和正则表达式
  7. Apache Httpd通过mod_jk连接多个Tomcat
  8. 给jdk写注释系列之jdk1.6容器(2)-LinkedList源码解析
  9. 就是爱Java
  10. win10 平台 elasticsearch 与 elasticsearch-head 的安装
  11. taskctl 软件集群安装部署
  12. redis过期机制
  13. Vim设计
  14. php之函数
  15. 【BZOJ】1832: [AHOI2008]聚会
  16. Resource View Window of Visual Studio
  17. IDEA 修改文件编码
  18. [原]F5负载均衡示例:轮寻
  19. [LeetCode&amp;Python] Problem 637. Average of Levels in Binary Tree
  20. [leetcode tree]95. Unique Binary Search Trees II

热门文章

  1. BFS和DFS学习笔记
  2. 【小项目】微信定时推送天气预报Github项目使用及原理介绍-包含cron、天气预报、常用api
  3. 【Java SE进阶】Day10 缓冲流、转换流、序列化流 、打印流
  4. pycharm 2021.2.1专业版破解
  5. windowserver中PowerShell禁止脚本执行的解决方法
  6. 利用Git同步思源笔记
  7. 大数据 - DWS层 业务实现
  8. [编程基础] C++多线程入门5-使用互斥锁解决资源竞争
  9. Redis-03 Redis事务
  10. DVWA靶场实战(一)——Brute Force