基于spring-cloud-hystrix的断路器使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Little_Matches/article/details/84501356

改造之前的模块feign-client

在pom.xml增加hystrixy依赖

<?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>
    <artifactId>spring-cloud-hystrix</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <file.encoding>UTF8</file.encoding>
        <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
    </parent>

    <dependencies>
        <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-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </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:

server.port=8885
spring.application.name=hystrix-client
#开启断路器,默认不开启,需手动打开
feign.hystrix.enabled=true

eureka.client.service-url.defaultZone=http://localhost:8881/eureka/

在application.java里增加本地调用失败时的方法,并加入callback属性

package top.littlematch.hystrix;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@RestController
public class HystrixApplication
{
    public static void main(String[] args){
        SpringApplication.run(HystrixApplication.class, args);
    }

    /**
     * 调用的服务名,以接口的方式定义
     * 增加fallback = HelloError.class
     * 表明调用失败时,使用的降级本地服务
     *
     */

    @FeignClient(value = "eureka-client",fallback = HelloError.class)
    public interface BaseService {
        //调用的方法所暴露的url,其实就相当于页面调用方法一样
        @RequestMapping(value = "/hello", method = RequestMethod.GET)
        public String hello(@RequestParam("name") String name);
    }

    /**
     * 增加本地可以调用的方法,提供给服务调用失败时使用
     */
    @Component
    public class HelloError implements BaseService{
        @Override
        public String hello(String name){
            return "hello,"+name+"! The server now cannot use!";
        }
    }

    /**
     * 注入该方法,idea这里可能会报红
     *  这里就如dubbo的方法调用,只是这里需重新定义个接口接收方法
     *  基于http会多这一步,但也因为基于http其扩展性很强,能轻易实现与其他语言基于http服务调用
     */

    @Autowired
    private BaseService baseService;


    @RequestMapping("/hello")
    public String hello(@RequestParam("name")String name){
        //调用方法
        return baseService.hello(name);
    }

}

依次启动eureka-server,eureka-client,hystrix-client,再调用 http://localhost:8885/hello?name=match

浏览器返回:

hello! match

关闭eureka-client,再次调用
http://localhost:8885/hello?name=match, 返回:
hello,match! The server now cannot use!

开启Hystrix Dashboard

在pom.xml加入如下依赖:

<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-actuator</artifactId>
</dependency>

由于springboot升级到2.0的原因需要在配置里面增加:
application.properties

management.endpoints.web.exposure.include: hystrix.stream

application.java加入如下注解:

@EnableHystrixDashboard
@EnableCircuitBreaker

重启服务hystrix-client:

打开连接:
http://localhost:8885/hystrix

得到HystrixDashboard界面:
image

在url里面填写:

1.5版本为http://localhost:8885/hystrix.stream

http://localhost:8885/actuator/hystrix.stream

image

需要注意的是:如果没有调用接口的话,会出现loading…,所以需选调用几下接口, 然后就可以看到信息了

gitee代码地址:https://gitee.com/leftbehindmatches/spring-cloud-examples/tree/master/spring-cloud-hystrix

猜你喜欢

转载自blog.csdn.net/Little_Matches/article/details/84501356