Springcloud学习(五)Hystrix入门服务熔断降级

Hystrix入门服务熔断降级

  • Hystrix就是隔离措施的一种实现,可以设置在某种超时或者失败情形下断开依赖调用或者返回指定逻辑,从而提高分布式系统的稳定性.

比如:订单系统请求库存系统,结果一个请求过去,因为各种原因,网络超时,在规定几秒内没反应,或者服务本身就挂了,这时候更多的请求来了,不断的请求库存服务,不断的创建线程,因为没有返回,也就资源没有释放,这也导致了系统资源被耗尽,你的服务奔溃了,这订单系统好好的,你访问了一个可能有问题的库存系统,结果导致你的订单系统也奔溃了,你再继续调用更多的依赖服务,可会会导致更多的系统奔溃,这时候Hystrix可以实现快速失败。

  • 本文描述客户端调用服务时,如果服务提供者宕机将使用hystrix进行自动降级。
  • 代码地址https://github.com/Jacwo/eureka-server-hystrix 欢迎start

开始

  1. 使用idea新建一个maven项目 可以使用spring Initializr
  2. 替换pom.xml
    
    
        4.0.0
    
        com.yyl
        eureka-server-hystrix
        0.0.1-SNAPSHOT
        eureka-server-hystrix
        Demo project for Spring Boot
    
        
            org.springframework.boot
            spring-boot-starter-parent
            1.5.9.RELEASE
            
        
    
        
            
                org.springframework.boot
                spring-boot-starter-web
            
            
                org.springframework.cloud
                spring-cloud-starter-eureka
                RELEASE
            
            
                org.springframework.cloud
                spring-cloud-starter-ribbon
                RELEASE
            
            
            
                org.springframework.boot
                spring-boot-starter-actuator
            
    
            
            
                org.springframework.cloud
                spring-cloud-starter-hystrix
                RELEASE
            
        
    
        
            
                    
                        org.springframework.cloud
                        spring-cloud-dependencies
                        Camden.SR3
                        pom
                        import
                    
                
        
    
        
            
                
                    org.springframework.boot
                    spring-boot-maven-plugin
                
                
                    org.apache.maven.plugins
                    maven-compiler-plugin
                    
                        1.8
                        1.8
                    
                
            
        
    
    
    

     

  3. 编写启动类
    package com.yyl.hystrix;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    import org.springframework.cloud.netflix.hystrix.EnableHystrix;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.client.RestTemplate;
    
    /**
     * @author yangyuanliang
     */
    @SpringBootApplication
    @EnableHystrix
    public class EurekaServerHystrixApplication {
        @Bean
        @LoadBalanced
        RestTemplate getRestTemplate(){
            return new RestTemplate();
        }
        public static void main(String[] args) {
            SpringApplication.run(EurekaServerHystrixApplication.class, args);
        }
    
    }
    

     

  4. 编写controller
    package com.yyl.hystrix.controller;
    
    import com.yyl.hystrix.service.RibbonService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * author:yangyuanliang Date:2019-08-15 Time:11:00
     **/
    @RestController
    public class RibbonController {
        @Autowired
        private RibbonService ribbonService;
        @RequestMapping("/hystrix/test")
        public String testHystrix(){
            return ribbonService.helloService();
        }
    }
    

     

  5. 编写service
    package com.yyl.hystrix.service;
    
    import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.web.client.RestTemplate;
    
    /**
     * author:yangyuanliang Date:2019-08-15 Time:11:00
     **/
    @Service
    public class RibbonService {
        @Autowired
        private RestTemplate restTemplate;
        @HystrixCommand(fallbackMethod = "hystrixFallback")
        public String helloService(){
            return restTemplate.getForEntity("http://hello-service/hello",String.class).getBody();
        }
    
        public String hystrixFallback(){
            return "error";
        }
    
    }
    

     

  6. 编写application.properties
    server.port=8088
    spring.application.name=hystrix
    eureka.client.service-url.defaultZone=http://localhost:8672/eureka/,http://localhost:8673/eureka/
    

     

  7. 启动mvn spring-boot:run
  8. 启动注册中心参考https://blog.csdn.net/cccfire/article/details/99453834
  9. 启动生产者参考https://blog.csdn.net/cccfire/article/details/99455466
  10. 测试输入http://localhost:8088/hystrix/test 关闭生产者测试熔断

你可能感兴趣的:(springclond相关,Springcloud学习,hysteria,spring,cloud,服务熔断,降级)