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 關閉生產者測試熔斷
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章