• 微服务架构之spring cloud hystrix&hystrix dashboard


    在前面介绍spring cloud feign中我们已经使用过hystrix,只是没有介绍,spring cloud hystrix在spring cloud中起到保护微服务的作用,不会让发生的异常无边界的蔓延下去,很像我们电路中的保险设置,有超压或者线路有问题就即时的断开,保护用电设备不被损坏,这篇文章就来介绍spring cloud hystrix及其hystrix dashboard。

    (一) 版本说明

    a) Spring boot 2.0.6.RELEASE

    b) Spring cloud Finchley.SR2

    c) Java version 1.8

    d) spring-cloud-starter-netflix-hystrix 2.0.2.RELEASE

    e) spring-cloud-starter-netflix-hystrix-dashboard 2.0.2.RELEASE

    (二) 项目配置

    1. 项目设置

    a) POM设置

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

    b) application.yml配置文件

    eureka:
    
    datacenter: ctm
    
    environment: dev
    
    instance:
    
    hostname: 192.168.1.78
    
    prefer-ip-address: true
    
    ip-address: 192.168.1.129
    
    lease-renewal-interval-in-seconds: 10
    
    lease-expiration-duration-in-seconds: 30
    
    instance-id: ${eureka.instance.ip-address}:${server.port}
    
    client:
    
    service-url:
    
    defaultZone: http://${eureka.instance.hostname}:1001/eureka/,http://${eureka.instance.hostname}:1002/eureka/,http://${eureka.instance.hostname}:1003/eureka/
    
    fetch-registry: true
    
    register-with-eureka: true
    
    healthcheck:
    
    enabled: true
    
    feign:
    
    hystrix:
    
    enabled: true

    c) 主要参数说明

    i. eureka.instance.prefer-ip-address 使用IP显示注册信息

    ii. eureka.instance.ip-address 实例IP地址,

    iii. eureka.instance.instance-id 自定义实例id,服务之间调用就是使用该配置,多个实例必须保证唯一性

    iv. eureka.client.service-url.defaultZone 注册中心地址

    v. feign.Hystrix.enabled 在feign中其中熔断器

    d) 服务提供者API

    @Value("${server.port}")
    
    private String getPort;
    
    @GetMapping(name = "index", value = "/index")
    
    public String Index() {
    
    return getPort;
    
    }

    2. feign消费端项目配置

    a) POM设置

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

    i. 这里把服务消费端也注册到了服务治理中心,消费者同时也是其它服务的提供者。

    b) application.yml配置文件

    eureka:
    
    datacenter: ctm
    
    environment: dev
    
    instance:
    
    hostname: 192.168.1.78
    
    prefer-ip-address: true
    
    ip-address: 192.168.1.129
    
    lease-renewal-interval-in-seconds: 10
    
    lease-expiration-duration-in-seconds: 30
    
    instance-id: ${eureka.instance.ip-address}:${server.port}
    
    client:
    
    service-url:
    
    defaultZone: http://${eureka.instance.hostname}:1001/eureka/,http://${eureka.instance.hostname}:1002/eureka/,http://${eureka.instance.hostname}:1003/eureka/
    
    fetch-registry: true
    
    register-with-eureka: true
    
    healthcheck:
    
    enabled: true

    c) feign服务消费者API

    @Configuration
    
    public class FeignConfig {
    
    @Bean
    
    public Retryer feignRetryer(){
    
    return new Retryer.Default(100, TimeUnit.SECONDS.toMillis(1),3);
    
    }
    
    }
    
    @Component
    
    public class FallBackService implements FeignService {
    
    @Override
    
    public String Index() {
    
    return "hi,feign,error!";
    
    }
    
    }
    
    @Component
    
    @FeignClient(name = "DEMOSERVICEIMPL",configuration = FeignConfig.class,fallback = FallBackService.class)
    
    public interface FeignService {
    
    @GetMapping(value = "/index")
    
    String Index();
    
    }

    i. FeignClient feign客户端配置,这里配置了服务名称、重试规则、失败回调,fallback 属性就是熔断器提供的功能,这点在后面的熔断器看板中可以更直观的看到。

    3. 项目运行

    a) 运行服务提供者

    i. 运行3个服务者实例后,会在服务中心看到如下效果,服务提供者已经注册成功

    clip_image002

    b) feign运行服务消费者

    i. 运行消费者,如下图所示

    clip_image004

    c) 在浏览器中输入消费者地址服务地址,即可看到如下效果

    clip_image006

    在看板地址中输入http://192.168.1.129:1201/actuator/hystrix.stream,然后点击Monitor Stream 按钮,即可看到熔断器监控,如果没有数据显示,可以用任何客户端调用几次API,即可看到类似效果。

    clip_image008

      这样spring cloud hystrix&hystrix dashboard就介绍完了,如果在开发中遇到问题,也可以留言共同探讨共同进步。

  • 相关阅读:
    开机自动挂载分区
    Wine安装
    ubuntu 将idea/vscode快捷方式加入到启动器中
    在Linux上安装Java
    httpclient
    shiro
    redis-随笔
    maven
    spring的aop
    spring事务知识梳理
  • 原文地址:https://www.cnblogs.com/lzh-boy/p/10381443.html
Copyright © 2020-2023  润新知