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

断路器: 简单来说,就是一个微服务在调另外一个微服务时,若失败了,则调用方调用自己相同名字的本地方法,使得自身可以立即响应返回定制的报错信息, 而不会抛出连接异常给用户。

改造之前的模块feign-client

在pom.xml增加hystrixy依赖


<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.0modelVersion>
    <artifactId>spring-cloud-hystrixartifactId>
    <version>1.0-SNAPSHOTversion>
    <packaging>jarpackaging>
    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <java.version>1.8java.version>
        <file.encoding>UTF8file.encoding>
        <spring-cloud.version>Finchley.SR2spring-cloud.version>
    properties>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.2.RELEASEversion>
    parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-openfeignartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-hystrixartifactId>
        dependency>
    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
    
project>

开启断路器(2.0版本的springboot默认关闭)

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属性, 本地方法以@Component方式注入

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.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboardartifactId>
dependency>
<dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
dependency>

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

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

application.java加入如下注解:

@EnableHystrixDashboard
@EnableCircuitBreaker

重启服务hystrix-client:

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

得到HystrixDashboard界面:
spring-cloud (三) 基于spring-cloud-hystrix的断路器使用_第1张图片

在url里面填写:

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

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

spring-cloud (三) 基于spring-cloud-hystrix的断路器使用_第2张图片

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

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

你可能感兴趣的:(Java,SpringBoot,springcloud,feigin,hystrix,spring-cloud入门)