Spring Cloud學習筆記(三)Hystrix斷路器

點擊此處查看完整源碼
微服務中,我們希望單個模塊出錯不影響整個系統,當底層服務不可用時,可以阻斷故障的傳播。這就是斷路器的作用。他是系統服務穩定性的最後一重保障。
在springcloud中斷路器組件就是Hystrix。

使用Hystrix

首先創建springboot項目hystrix,添加依賴選擇如下
在這裏插入圖片描述
完整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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>hystrix</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hystrix</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</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>
        </plugins>
    </build>
</project>

接下來配置application.properties

#服務名
spring.application.name=hystrix
#端口號
server.port=8084
#注入目標,配置服務中心url,與服務端的配置保持一致
eureka.client.service-url.defaultZone=http://localhost:8081/eureka/

啓動類上添加註解@EnableCircuitBreaker(也可以使用@SpringCloudApplication代替@EnableCircuitBreaker和@SpringBootApplication兩個註解)開啓斷路器,同時提供一個RestTemplate

package com.example.hystrix;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableCircuitBreaker
public class HystrixApplication {

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

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

到這裏就配置完成,下面進行簡單測試
首先創建類HelloService

package com.example.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;

@Service
public class HelloService {
    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "error")//調用接口失敗去調用error
    public String hello() {
        return restTemplate.getForObject("http://provider/hello", String.class);
    }

    /**
     * 要和調用方法返回結果一致
     * @return
     */
    public String error() {
        return "error";
    }
}

在創建類HelloController

package com.example.hystrix.controller;

import com.example.hystrix.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @Autowired
    private HelloService helloService;

    @GetMapping("hello")
    public String hello() {
        return helloService.hello();
    }
}

啓動eureka和provider,這兩個項目可以看我前兩章筆記,Eureka搭建服務註冊與消費
provider命令行啓動兩個實例

cd provider/target
java -jar provider-0.0.1-SNAPSHOT.jar --server-port=8100
java -jar provider-0.0.1-SNAPSHOT.jar --server-port=8200

啓動hystrix項目,這個時候去訪問http://localhost:8084/hello可以看到效果和上一章的項目consumer項目效果是一樣的,我們關閉provider其中一個實例在訪問http://localhost:8084/hello就會看到和以下兩種結果
在這裏插入圖片描述
在這裏插入圖片描述
這個就是@HystrixCommand(fallbackMethod = “error”)這一行的作用
到這裏一個簡單的示例就完成了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章