其他分享
首页 > 其他分享> > Hystrix容错监控机制

Hystrix容错监控机制

作者:互联网

六: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

标签:Hystrix,springframework,id,容错,监控,org,import,public,cloud
来源: https://www.cnblogs.com/HJZ114152/p/16407184.html