美文网首页
Spring Cloud 12 -- 使用 Hystric Da

Spring Cloud 12 -- 使用 Hystric Da

作者: 半碗鱼汤 | 来源:发表于2019-08-13 15:42 被阅读0次

一、说明

在微服务架构中为例保证程序的可用性,防止程序出错导致网络阻塞,出现了断路器模型。断路器的状况反应了一个程序的可用性和健壮性,它是一个重要指标。Hystrix Dashboard是作为断路器状态的一个组件,提供了数据监控和友好的图形化界面。
接下来,我们改造一下 client01。

二、添加依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>

完整依赖如下

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>com.dhsg.sc</groupId>
    <artifactId>client01</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>client01</name>
    <description>client01</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

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

        <!-- feign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <!-- config client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <!-- spring cloud bus -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </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-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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>

三、启动类添加注解 @EnableHystrix 和 @EnableHystrixDashboard

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * 添加注解 @EnableFeignClients 开启 Feign 功能
 * 添加注解 @EnableHystrix 开启 Hystrix 功能
 * 添加注解 @EnableHystrixDashboard 开启 HystrixDashboard 功能
 */

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
@EnableHystrixDashboard
public class Client01Application {

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

}

四、写一个接口去访问 client02 的接口,并在接口上面加上注解 @HystrixCommand(fallbackMethod = "hiError")

    /**
     * 通过 feign 组件,访问 client02 的接口,并且使用了 hystric 断路器
     * @return String
     */
    @RequestMapping(value = "getclient02namewithhystric",method = RequestMethod.GET)
    @HystrixCommand(fallbackMethod = "hiError")
    public String getClient02NameWithHystric(){
        return useOtherApiWithHystricService.getClient02Name();
    }

    /**
     * hystrix 断路器的 fallback 方法
     * @return String
     */
    public String hiError() {
        return "sorry, hystric say error!";
    }

具体思路类似我们之前学习使用 feign 那样

五、启动项目

访问 http://localhost:8763/getclient02namewithhystric

结果

之前我们使用了 feign 的断路器,现在在之前的接口上面添加 hystric 的注解 @HystrixCommand(fallbackMethod = "hiError") ,观察他会使用 hystric 还是 feign 的断路器。

     /**
     * 该接口是通过 feign 组件,访问 client02 的接口
     * 并且服务层的 getClient02Name 添加了 feign 的断路器
     * 但是 hystrix 的优先级高于 feign 的,所以这里会使用 hystrix 的断路器
     * 注解 @HystrixCommand(fallbackMethod = "hiError") 是使用了 hystric 的断路器
     * @return client02 的端口和服务名
     */
    @RequestMapping(value = "/getclient02name", method = RequestMethod.GET)
    @HystrixCommand(fallbackMethod = "hiError")
    public String getClient02Name() {
        return userOtherApiService.getClient02Name();
    }

访问 http://localhost:8763/getclient02name

结果

所以,hystrix 的优先级高于 feign 自带的断路器。

六、访问 http://localhost:8763/hystrix

依次输入 http://localhost:8763/actuator/hystrix.stream 、2000 、dhsg(随便起个名字),点击 Monitor Stream

结果

这就是监控界面了,这个时候你再去调用 client01 的接口,这边就会监控到

相关文章

网友评论

      本文标题:Spring Cloud 12 -- 使用 Hystric Da

      本文链接:https://www.haomeiwen.com/subject/hbqbjctx.html