Spring-cloud-netflix-hystrix

服务注册中心eureka-server已经搭好,并且SPRING-CLOUD-NETFLIX-EUREKA-CLIENT-APPLICATION提供一个hello服务

畏怯还编写一个eureka-client-consumer服务消费者,去消费该服务,如果在真实环境中SPRING-CLOUD-NETFLIX-EUREKA-CLIENT-APPLICATION服务挂了,就需要采用熔断机制hystrix

Hystrix特性

1.断路器机制

断路器很好理解, 当Hystrix Command请求后端服务失败数量超过一定比例(默认50%), 断路器会切换到开路状态(Open). 这时所有请求会直接失败而不会发送到后端服务. 断路器保持在开路状态一段时间后(默认5秒), 自动切换到半开路状态(HALF-OPEN). 这时会判断下一次请求的返回情况, 如果请求成功, 断路器切回闭路状态(CLOSED), 否则重新切换到开路状态(OPEN). Hystrix的断路器就像我们家庭电路中的保险丝, 一旦后端服务不可用, 断路器会直接切断请求链, 避免发送大量无效请求影响系统吞吐量, 并且断路器有自我检测并恢复的能力.

2.Fallback

Fallback相当于是降级操作. 对于查询操作, 我们可以实现一个fallback方法, 当请求后端服务出现异常的时候, 可以使用fallback方法返回的值. fallback方法的返回值一般是设置的默认值或者来自缓存.

3.资源隔离

在Hystrix中, 主要通过线程池来实现资源隔离. 通常在使用的时候我们会根据调用的远程服务划分出多个线程池. 例如调用产品服务的Command放入A线程池, 调用账户服务的Command放入B线程池. 这样做的主要优点是运行环境被隔离开了. 这样就算调用服务的代码存在bug或者由于其他原因导致自己所在线程池被耗尽时, 不会对系统的其他服务造成影响. 但是带来的代价就是维护多个线程池会对系统带来额外的性能开销. 如果是对性能有严格要求而且确信自己调用服务的客户端代码不会出问题的话, 可以使用Hystrix的信号模式(Semaphores)来隔离资源.

构建服务熔断项目

 一、新建module,勾选springcloud对应模块

   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>lf.liyouyou</groupId>
        <artifactId>spring-cloud-netflix-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>lf.youyou</groupId>
    <artifactId>spring-cloud-eureka-client-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cloud-eureka-client-consumer</name>
    <description>Demo project for Spring Boot</description>


    <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-openfeign</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>

</project>

二、启动类添加注解

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class SpringCloudEurekaClientConsumerApplication {

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

}

三、编写代码

  Feign接口,指定熔断类

@FeignClient(name="spring-cloud-netflix-eureka-client-application",fallback = HelloRemoteHystrix.class)
public interface HelloRemote {

    @RequestMapping(value = "/hello")
    public String hello(@RequestParam(value = "name") String name);
}

熔断类:

@Component
public class HelloRemoteHystrix implements HelloRemote{

    @Override
    public String hello(@RequestParam(value = "name") String name) {
        return "hello " +name+", this messge send failed ";
    }
}

Controller:

@RestController
public class HelloController {

    @Autowired
    HelloRemote helloRemote;

    @RequestMapping("/hello/{name}")
    public String index(@PathVariable("name") String name) {
        return helloRemote.hello(name);
    }

    @RequestMapping("/methodHystrix")
    @HystrixCommand(fallbackMethod = "methodHystrix")//方法熔断
    public String getString() {
        int i = 1/0;
        return "methodHystrix";
} public String methodHystrix(){ return "methodHystrix";
} }

四、配置application.properties:

spring.application.name=spring-cloud-netflix-consumer-hystrix
server.port=9091
eureka.client.service-url.defaultZone=http://localhost:8000/eureka/
feign.hystrix.enabled=true

启动项目,访问注册中心 http://localhost:8000/ 查看eureka面板(若之前项目未启动,则需要全部启动)

 访问:http://localhost:9091/hello/lf

hello lf,nice to meet you!

关闭spring-cloud-netflix-eureka-client-application服务,再访问,即可看到

hello lf, this messge send failed

直接访问:http://localhost:9091/methodHystrix 方法熔断

返回:methodHystrix

spring.application.name=spring-cloud-netflix-consumer-hystrix
server.port=9091
eureka.client.service-url.defaultZone=http://localhost:8000/eureka/
feign.hystrix.enabled=true
#默认只开启了health和info,设置为*,则包含所有的web入口端点
management.endpoints.web.exposure.include=*
hystrix.dashboard.proxy-stream-allow-list=*

访问:http://localhost:9091/hello/lf

hello lf,this is first messge

关闭spring-cloud-netflix-service-application服务,再访问,即可看到

hello lf, this messge send failed

直接访问:http://localhost:9091/methodHystrix

返回:methodHystrix

输入:http://localhost:9091/hystrix 进入如下页面:

注意自己的应用是在本地还是外部,本地用http,不同版本路径不同,2.0版本路径为../actuator/hystrix.stream

输入之后点击 monitor,进入页面

出现 Unable to connect to Command Metric Stream.显示未连接

(1)访问自己的应用服务http://localhost:9091/actuator/hystrix.stream,显示ping,调用熔断接口http://localhost:9091/hello/lf,返回data

 排除的代码、注解、包的问题

(2)查看debug日志,若出现

  Proxy opening connection to: http://localhost:9091/actuator/hystrix.stream

  WARN 6980 --- [nio-9091-exec-8] ashboardConfiguration$ProxyStreamServlet : Failed opening connection to http://localhost:9091/actuator/hystrix.stream : 404 : HTTP/1.1 404 

  则需要在application.properties配置中,打开actuator访问

management.endpoints.web.exposure.include=*

(3)查看debug日志,若出现

 WARN 9888 --- [nio-9091-exec-3] ashboardConfiguration$ProxyStreamServlet : Origin parameter: http://localhost:9091/actuator/hystrix.stream is not in the allowed list of proxy host names.

 If it should be allowed add it to hystrix.dashboard.proxyStreamAllowList.

则需要在application.properties配置中,打开代理访问

hystrix.dashboard.proxy-stream-allow-list=*

 重新启动项目,重新访问,进入hystrix面板

 访问熔断接口,面板如下

 If it should be allowed add it to hystrix.dashboard.proxyStreamAllowList.

则需要在application.properties配置中,打开代理访问

spring-cloud-netflix-consumer-hystrix
原文地址:https://www.cnblogs.com/flgb/p/13802083.html