springcloud hystrix 容错机制

1.pom.xml



    4.0.0
    
        springcloudDemo
        org.example
        1.0-SNAPSHOT
    

    com.weitao
    springcloud-hystrix
    0.0.1-SNAPSHOT
    springcloud-hystrix
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.cloud
            spring-cloud-starter-openfeign
            2.2.6.RELEASE
        

        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
            2.2.6.RELEASE
        

        
            org.springframework.cloud
            spring-cloud-starter-netflix-hystrix
            2.2.6.RELEASE
        

        
            org.springframework.boot
            spring-boot-starter-actuator
            2.2.6.RELEASE
        

        
            org.springframework.cloud
            spring-cloud-starter-netflix-hystrix-dashboard
            2.2.6.RELEASE
        

        
            org.example
            springcloud-api
            1.0-SNAPSHOT
        


    


    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    
    
        
            spring-snapshots
            Spring Snapshots
            https://repo.spring.io/snapshot
            
                false
            
        
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
            
                false
            
        
    


2.application.yml

server:
  port: 8060
spring:
  application:
    name: hystrix
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${server.port}
#断路器,起到降级作用,避免服务雪崩
feign:
  hystrix:
    enabled: true

management:
  endpoints:
    web:
      exposure:
        include: 'hystrix.stream'
hystrix:
  dashboard:
    proxy-stream-allow-list: localhost

3.代码结构

springcloud hystrix 容错机制_第1张图片

4.具体代码

package com.example.springcloud.controller;

import com.example.springcloud.fegin.FeginDept;
import com.example.springcloud.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/hystrix")
public class HystrixController {

    @Autowired
    private FeginDept feginDept;

    @GetMapping("/select")
    public List get() {
        return feginDept.selectAll();
    }

    @GetMapping("/select/{id}")
    public Dept selectByID(@PathVariable(name = "id") Long id) {
        return feginDept.selectByID(id);
    }

    @PostMapping(value = "/save", produces = "application/json")
    public boolean saveDept(@RequestBody Dept dept) {
        return feginDept.saveDept(dept);
    }

    //假如服务提供者没启动,直接访问/handle/getInfo 那么容错机制就会调用FeginDept  fallback属性设置FeginError 里的方法
    @GetMapping("/getInfo")
    public String getInfo() {
        return feginDept.getInfo();
    }
}
package com.example.springcloud.fegin;

import com.example.springcloud.pojo.Dept;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * 接口注解方式
 * value="provider" 调用提供者的应用名称
 */
@FeignClient(value = "provider")
public interface FeginDept {

    @PostMapping(value = "/dept/save",produces = "application/json")
    boolean saveDept(@RequestBody  Dept dept);


    @GetMapping("/dept/select/{id}")
    Dept selectByID(@PathVariable("id") Long id);

    @GetMapping("/dept/select")
    List selectAll();


    @GetMapping("/dept/getInfo")
    String getInfo();

}
package com.example.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
@EnableHystrix
@EnableHystrixDashboard           //声明启用可视化数据监控
public class SpringcloudHystrixApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringcloudHystrixApplication.class, args);
    }

}

5.运行注册中心,提供者,和 Hystrix模块入口

springcloud hystrix 容错机制_第2张图片

6.  http://localhost:8060/actuator/hystrix.stream 

这个界面一直ping输出
 

7.访问接口

http://localhost:8060/hystrix/select

 

8.  http://localhost:8060/hystrix  

这个打开hystrix仪表盘 ,可以输入http://localhost:8060/actuator/hystrix.stream 

springcloud hystrix 容错机制_第3张图片

 

9.跳转到页面

springcloud hystrix 容错机制_第4张图片

 

你可能感兴趣的:(笔记,cloud,dashboard)