0%

SpringCloud-Hystrix

Hystrix简介

  • Hystrix简介

    在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用。为了保证其高可用,单个服务通常会集群部署。由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,Servlet容器的线程资源会被消耗完毕,导致服务瘫痪。服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这就是服务故障的“雪崩”效应。

    为了解决这个问题,业界提出了断路器模型。

Hystrix使用

在Feign中使用Hystrix

1:Feign工程中pom.xml中加入Hystrix依赖(feign工程参见《SpringCloud-feign》文章):

     <!--hystrix断路器-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>

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

2.Feign是自带断路器的,它没有默认打开。需要在配置文件中配置打开它

spring:
  application:
    name: service-feign
eureka:
  client:
    service-url:
      defaultZone: http://172.96.251.101:8761/eureka
server:
  port: 8083

feign:
  hystrix:
    enabled: true
management:
  server:
    servlet:
      context-path: /

3:启动类上添加@EnableHystrix注解

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

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

4:创建productServiceClientHystric.java实现ProductServiceClient接口(我的feign工程中有这个接口)

package com.ux.feign.serviceClient;

import com.ux.feign.pojo.Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class productServiceClientHystric implements ProductServiceClient {

    @Override
    public Product plist(String productId) {
        Product product = new Product();
        product.setProductName("访问出错了~~");
        log.info("~~~~~访问出错了,断路器起作用了~~~~~~~~");
        return product;
    }
}

5:ProductServiceClient接口添加fallback属性

package com.ux.feign.serviceClient;

import com.ux.feign.pojo.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(name = "WXPROJECT",fallback = productServiceClientHystric.class)
@Service
public interface ProductServiceClient {

    @GetMapping("/product/productInfo")
    Product plist(@RequestParam("productId") String productId);
}

6.启动改造后的feign工程,不启动WXPROJECT服务,此时访问,会提示在productServiceClientHystric中返回的信息:

product.setProductName("访问出错了~~");即:访问出错了~~

7:Hystrix Dashboard (断路器:Hystrix 仪表盘)

1.pom.xml中已经添加 <!--hystrix dashboard-->依赖
2.启动类添加注解@EnableHystrixDashboard

两步即可

8:访问http://localhost:8083/hystrix

1.http://localhost:8083/hystrix.stream
2.title填写:feignClient
3.点击monitor stream
4.进入下一个界面访问http://localhost:8083/product/list?productId=11
5.正常情况下应该出现分析数据,但是springboot2.0下可能不出现,解决办法如下:

    启动类下添加getServlet()方法并以bean方式注入即可:
    
    @SpringBootApplication
    @EnableEurekaClient
    @EnableFeignClients
    @EnableHystrixDashboard
    @EnableHystrix
    public class FeignApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(FeignApplication.class, args);
        }
    
        /**
         * hystrix-dashboard不显示解决方法
         *
         * @return
         */
        @Bean
        public ServletRegistrationBean getServlet() {
            HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
            ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
            registrationBean.setLoadOnStartup(1);
            registrationBean.addUrlMappings("/hystrix.stream");
            registrationBean.setName("HystrixMetricsStreamServlet");
            return registrationBean;
        }
    
        /***
         * http://localhost:8083/hystrix
         *
         * http://localhost:8083/hystrix.stream
         * feignClient
         * 1000
         *
         */
        }

小结

  • 小结

    访问feign工程,feign抵用远程服务,但是远程服务挂了怎么办?此时可以通过断路器Hystrix返回一个信息,说明这个服务挂掉了。