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

Hystrix入门服务熔断降级

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

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

开始

  1. 使用idea新建一个maven项目 可以使用spring Initializr
  2. 替换pom.xml
    <?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">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.yyl</groupId>
        <artifactId>eureka-server-hystrix</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>eureka-server-hystrix</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.9.RELEASE</version>
            <relativePath/>
        </parent>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
                <version>RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-ribbon</artifactId>
                <version>RELEASE</version>
            </dependency>
            <!--暴露各种指标-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    
            <!--hystrix熔断器-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-hystrix</artifactId>
                <version>RELEASE</version>
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                    <dependency>
                        <groupId>org.springframework.cloud</groupId>
                        <artifactId>spring-cloud-dependencies</artifactId>
                        <version>Camden.SR3</version>
                        <type>pom</type>
                        <scope>import</scope>
                    </dependency>
                </dependencies>
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

     

  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 关闭生产者测试熔断
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值