服务降级:

服务熔断是服务端出现异常或服务响应超时,对方法进行熔断及使用备用

服务降级是客户端从整体网站请求负载考虑,当某个服务熔断或关闭之后,服务将不再被调用,此时在客户端,我们可以准备一个FallbackFactory,返回一个默认值(缺省值)做出处理不影响客户端运行使用,服务水平降低,但比直接挂掉好

使用Feign的服务端:

Hystrix-服务降级

创建降级服务类,实现降级工厂接口FallbackFactory,重写方法,方法返回客户端接口

工厂接口是feign.hystrix包内的

package com.stt.springcloud.service;

import com.stt.springcloud.pojo.Dept;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;

import java.util.List;

// 降级
@Component  // 注入容器
public class DeptClientServiceFallbackFactory implements FallbackFactory<DeptClientService> {
    @Override
    public DeptClientService create(Throwable throwable) {
        // 返回接口实现方法,并提示降级信息
        return new DeptClientService() {
            @Override
            public Dept queryById(Long id) {
                return new Dept()
                        .setDeptno(id)
                        .setDname("id=>"+id+"没有对应信息,客户端提供了降级提示信息,这个服务已经关闭")
                        .setDb_source("没有数据");
            }

            @Override
            public List<Dept> queryAll() {
                return null;
            }

            @Override
            public boolean addDept(Dept dept) {
                return false;
            }
        };
    }
}

接口:

在注解@FeignClient内添加降级提示类

@Component                                       // 指定降级的类
@FeignClient(value = "SPRINGCLOUD-PROVIDER-DEPT", fallbackFactory = DeptClientServiceFallbackFactory.class)
public interface DeptClientService {

    @GetMapping("/dept/get/{id}")
    public Dept queryById(@PathVariable("id") Long id);

    @GetMapping("/dept/list")
    public List<Dept> queryAll();

    @PostMapping("/dept/add")
    public boolean addDept(Dept dept);
}

配置类:

# 开启降级feign.hystrix
feign:
  hystrix:
    enabled: true

小结:

Feign对服务提供者声明可以是用的接口有哪些,服务消费者通过Feign可以调用提供者的接口方法,Hystrix服务降级对这些声明的接口做故障处理,在服务提供者发生故障,而消费者通过Feign可以得到默认值。表示服务不可用。

Dashboard流监控:

创建hystrix-dashboard监控客户端:

Hystrix-服务降级

导入依赖:

主要Hystrix依赖与dashboard监控页面依赖

<!--实体类+web-->
    <dependencies>
        <!--Hystrix依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
        <!--dashboard监控页面依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.stt</groupId>
            <artifactId>springcloud-api</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--热部署工具-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <!--ribbon依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
        <!--Eureka-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>

    </dependencies>

配置文件修改端口号

启动类:

注解@EnableHystrixDashboard开启监控页面

@SpringBootApplication
@EnableHystrixDashboard  // 开启监控页面
public class DeptConsumerDashboard_9001 {
    public static void main(String[] args) {
        SpringApplication.run(DeptConsumerDashboard_9001.class, args);
    }
}

服务提供者导入依赖:

若提供者能被监控需要导入依赖

<!--actuator完善监控信息-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<!--Hystrix依赖-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
    <version>1.4.6.RELEASE</version>
</dependency>

服务提供者启动类:

是在使用Hystrix的服务提供者的启动类中增加一个servlet配合监控使用,固定写法

@SpringBootApplication
@MapperScan("com.stt.springcloud.dao")
@EnableEurekaClient
@EnableDiscoveryClient  // 服务发现
@EnableCircuitBreaker  // 添加对熔断支持
public class DeptProviderHystrix_8001 {
    public static void main(String[] args) {
        SpringApplication.run(DeptProviderHystrix_8001.class, args);
    }

    // 增加一个servlet,固定写法,配合监控使用
    @Bean
    public ServletRegistrationBean hystrixMetricsStreamServlet(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
        // 添加访问路径
        registrationBean.addUrlMappings("/actuator/hystrix.stream");
        return registrationBean;
    }

测试:

若连接不上重启或等待

浏览器服务提供者访问 /actuator/hystrix.stream查看连接是否ping通

Hystrix-服务降级

监控页面输入地址http://localhost:8001/actuator/hystrix.stream查看监控情况

Hystrix-服务降级

Hystrix-服务降级

监控页面:

Hystrix-服务降级

Hystrix-服务降级

Hystrix-服务降级

Hystrix-服务降级

相关文章:

  • 2021-09-06
  • 2021-11-16
  • 2020-04-24
  • 2022-01-16
  • 2021-05-25
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-01
  • 2021-08-26
  • 2021-07-23
  • 2022-12-23
  • 2021-11-09
相关资源
相似解决方案