Hystrix: 服务熔断

Hystrix: 服务熔断_第1张图片

Hystrix: 服务熔断_第2张图片 

 

Hystrix: 服务熔断_第3张图片

 

开始搭建8001服务的熔断机制

创建子模块

同样拷一份pom依赖



    
        springcloud
        com.kuang
        1.0-SNAPSHOT
    
    4.0.0

    springcloud-provider-dept-hystrix-8001

    
        8
        8
    


    
        
        
            org.springframework.cloud
            spring-cloud-starter-eureka
            1.4.6.RELEASE
        

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


        
        
            com.kuang
            springcloud-api
            1.0-SNAPSHOT
        

        
        
            junit
            junit
        

        
        
            mysql
            mysql-connector-java
        
        
        
            com.alibaba
            druid
        
        
        
            ch.qos.logback
            logback-core
        
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
        
        
        
            org.springframework.boot
            spring-boot-test
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            org.springframework.boot
            spring-boot-starter-jetty
        
        
        
            org.springframework.boot
            spring-boot-devtools
        


    

 

 再加一个依赖-hystrix

        
        
            org.springframework.cloud
            spring-cloud-starter-hystrix
            1.4.6.RELEASE
        

Controller

package com.kuang.springcloud.controller;

import com.kuang.springcloud.pojo.Dept;
import com.kuang.springcloud.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
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.RestController;

import java.util.List;

//提供Restful服务
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;


    @GetMapping("/dept/get/{id}")
    @HystrixCommand(fallbackMethod = "hystrixGet")
    public Dept get(@PathVariable("id") Long id){
        Dept dept = deptService.queryDeptById(id);

        if (dept==null){
            throw  new RuntimeException("id=>"+id+",不存在该用户,或者信息无法找到~");
        }

        return dept;
    }

    //备选方案
    public Dept hystrixGet(@PathVariable("id") Long id){

        return new Dept().setDeptno(id).setDname("id=>"+id+",没有对应的信息,null--@Hystrix").setDb_source("no this database in MySQL");
    }

}

主启动类

package com.kuang.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient //在服务启动后自动注册到 eureka服务端
@EnableDiscoveryClient//服务发现~
@EnableCircuitBreaker//添加对熔断的支持  ,开启断路器
public class DeptApplicationProviderHystrix_8001 {
    public static void main(String[] args) {
        SpringApplication.run(DeptApplicationProviderHystrix_8001.class,args);
    }
}

Hystrix: 服务熔断_第4张图片 

 

Hystrix: 服务熔断_第5张图片

 输入不存在ID

Hystrix: 服务熔断_第6张图片

 没写服务熔断机制的就

Hystrix: 服务熔断_第7张图片

加了这个可以显示地址,可以显示服务的ip 

 Hystrix: 服务熔断_第8张图片Hystrix: 服务熔断_第9张图片

 

 

你可能感兴趣的:(SpringCloud,hystrix,java,开发语言)