文章目录
  1. 1. 7天学会使用springcloud(五)
    1. 1.1. hystrix集成springcloud使用
    2. 1.2. 配置
    3. 1.3. 工作原理
    4. 1.4. 检测状态
    5. 1.5. 查看断路器信息状态
    6. 1.6. Turbine
    7. 1.7. 其它

7天学会使用springcloud(五)

hystrix集成springcloud使用

app-service项目中,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.service</groupId>
    <artifactId>app-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>app-service</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <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.M9</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-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <!--    增加feign服务代理依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.0.0.M2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

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

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>alimaven</id>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        </repository>
        <repository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
            <snapshots><enabled>true</enabled></snapshots>
        </repository>
    </repositories>

</project>

配置

  • 启用hystrix

在启动类上添加 @EnableCircuitBreaker与@EnableHystrixDashboard注解

paste image

  • 使用

添加@HystrixCommand注解:

 @RequestMapping(value = "/hello/{name}")
    @HystrixCommand(fallbackMethod = "error", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
            , @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "2")})
    public String hello(@PathVariable(value = "name") String name){
        return clientRemoteProxy.hello(name);
    }

    public String error(String name){
        return name+":您所调用的接口服务不可用,请重试";
    }

注意其中有两个关键属性分别为:

   execution.isolation.thread.timeoutInMilliseconds 这个是请求最大的响应时间

   circuitBreaker.requestVolumeThreshold 如果执行失败的次数等于或者超过这个值就开启保护

ClientRemoteProxy:

@Component
public class ClientRemoteProxy {

    @Autowired
    RestTemplate restTemplate;

    public String hello(String name) {
        return restTemplate.getForObject("http://APPCLIENT/hello?name="+name,String.class);
    }
}

我启动了两个client,一个client有hello接口,另一个则没有,因为robbin会有自动负载均衡策略,我们来看看什么效果?

我来回刷新请求了5-6次,会随机返回断路器信息和正确信息,但和zuul断路信息不同:

image-20220117141054189

paste image

工作原理

paste image

检测状态

如果springboot安装了actuator组件,我们可以访问请求地址:

http://localhost:9000/actuator/health 

来检测状态,当然也可以直接请求:

http://localhost:9000/actuator

来查看actuator有哪些接口可以访问。

查看断路器信息状态

Hystrix有个监控的控制台,因为启用了:

@EnableHystrixDashboard

这个引用了包:

spring-cloud-starter-netflix-hystrix-dashboard

可以看到有多少接口调用失败了,被熔断的次数,接口请求次数等数据:

paste image

参考springcloud的官方文档:

http://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/2.0.0.RC2/single/spring-cloud-netflix.html

访问dashboard的地址:

http://localhost:9000/hystrix

Turbine

Looking at an individual instance’s Hystrix data is not very useful in terms of the overall health of the system. Turbine is an application that aggregates all of the relevant /hystrix.stream endpoints into a combined /turbine.stream for use in the Hystrix Dashboard. Individual instances are located through Eureka. Running Turbine requires annotating your main class with the @EnableTurbine annotation (for example, by using spring-cloud-starter-netflix-turbine to set up the classpath). All of the documented configuration properties from the Turbine 1 wiki apply. The only difference is that the turbine.instanceUrlSuffix does not need the port prepended, as this is handled automatically unless turbine.instanceInsertPort=false.

这个turbine和dashboard配合使用的:

paste image

瞬定是会珍惜整个目录的。

关于trubine的使用我没有可分享的,需要去实践。

其它

springcloud-service
好困要睡觉了,晚安各位。。。。。。。

以下是官方文档:

http://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/2.0.0.RC2/single/spring-cloud-netflix.html

github代码

文章目录
  1. 1. 7天学会使用springcloud(五)
    1. 1.1. hystrix集成springcloud使用
    2. 1.2. 配置
    3. 1.3. 工作原理
    4. 1.4. 检测状态
    5. 1.5. 查看断路器信息状态
    6. 1.6. Turbine
    7. 1.7. 其它