Hystrix集群及集群监控turbine

本文详细介绍了在微服务架构下,如何利用Hystrix进行服务熔断和服务降级,确保系统的稳定性和可用性。同时,讲解了如何配置Turbine进行集群监控,以及Feign与Hystrix的整合,实现服务间的解耦。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、Hystrix集群及监控turbine

Netflix Hystrix 可以为应用程序中的命令状态提供实时指标。这些数据通过一个名为Hystrix Dashboard的非常酷的界面以图形形式展现出来。但是它只能提供单个服务的信息,如果需要调用几十个微服务,我们希望看到的是这些微服务的性能,而不是一个微服务一个微服务的去查看。所以我们需要一个工具能让我们汇总系统内多个服务的数据并显示到Hystrix Dashboard上,使用Turbine可以做到这一点。

将Turbine集成到基于Spring-Cloud的应用中非常简单,只需要像以前一样拉入Turbine的依赖,定义好要集群哪些公开信息以及如何汇总有关特定集群的信息

在这里我们可以来写一个生产集群
microservice-student-provider-hystrix-1004

---
server:
  port: 1004
  context-path: /
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/t224?useUnicode=true&characterEncoding=utf8
    username: root
    password: 123
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
  application:
    name: microservice-student
  profiles: provider-hystrix-1004

eureka:
  instance:
    hostname: localhost
    appname: microservice-student
    instance-id: microservice-student:1004
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://eureka2001.xyx.com:2001/eureka/,http://eureka2002.xyx.com:2002/eureka/,http://eureka2003.xyx.com:2003/eureka/

info:
  groupId: com.xyx.testSpringcloud
  artifactId: microservice-student-provider-hystrix-1004
  version: 1.0-SNAPSHOT
  userName: http://xyx.com
  phone: 123456

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 1500

---
server:
  port: 1005
  context-path: /
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/t224?useUnicode=true&characterEncoding=utf8
    username: root
    password: 123
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
  application:
    name: microservice-student
  profiles: provider-hystrix-1005

eureka:
  instance:
    hostname: localhost
    appname: microservice-student
    instance-id: microservice-student:1005
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://eureka2001.xyx.com:2001/eureka/,http://eureka2002.xyx.com:2002/eureka/,http://eureka2003.xyx.com:2003/eureka/

info:
  groupId: com.xyx.testSpringcloud
  artifactId: microservice-student-provider-hystrix-1005
  version: 1.0-SNAPSHOT
  userName: http://xyx.com
  phone: 123456

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 1500

---
server:
  port: 1006
  context-path: /
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/t224?useUnicode=true&characterEncoding=utf8
    username: root
    password: 123
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
  application:
    name: microservice-student
  profiles: provider-hystrix-1006

eureka:
  instance:
    hostname: localhost
    appname: microservice-student
    instance-id: microservice-student:1006
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://eureka2001.xyx.com:2001/eureka/,http://eureka2002.xyx.com:2002/eureka/,http://eureka2003.xyx.com:2003/eureka/

info:
  groupId: com.xyx.testSpringcloud
  artifactId: microservice-student-provider-hystrix-1006
  version: 1.0-SNAPSHOT
  userName: http://xyx.com
  phone: 123456

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 1500

启动类:
MicroserviceBookProviderHystrixApplication

package com.xyx.microservicebookproviderhystrix;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;


@EnableCircuitBreaker //关键部分
@EntityScan("com.xyx.*.*")
@EnableEurekaClient
@SpringBootApplication
public class MicroserviceBookProviderHystrixApplication {

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

}


Run/Debug Configurations
在这里插入图片描述
消费者集群配置完成了
新建项目microservice-book-consumer-hystrix-turbine-91

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-turbine</artifactId>
</dependency>

yml文件

server:
  port: 91
  context-path: /
eureka:
  client:
    service-url:
      defaultZone: http://eureka2001.xyx.com:2001/eureka/,http://eureka2002.xyx.com:2002/eureka/,http://eureka2003.xyx.com:2003/eureka/
turbine:
  app-config: microservice-book   # 指定要监控的应用名称
  clusterNameExpression: "'default'" #表示集群的名字为default
spring:
  application:
    name: turbine

启动类
MicroserviceBookConsumerHystrixTurbine91Application

package com.xyx.microservicebookconsumerhystrixturbine91;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.cloud.netflix.turbine.EnableTurbine;

@EnableTurbine
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class MicroserviceBookConsumerHystrixTurbine91Application {

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

}

先启动三个eureka,然后把带刚刚配置带的 hystrix 的服务都启动;
microservice-book-consumer-80(消费者)
这个也启动,方便测试;启动dashboard服务,启动turbine服务;
在这里插入图片描述
输入http://localhost:90/hystrix进入仪表盘,输入地址
在这里插入图片描述
点击 进入集群监控仪表:
在这里插入图片描述

2、Feign、Hystrix整合之服务熔断服务降级彻底解耦

所谓降级,一般是从整体负荷考虑。就是当某个服务熔断之后,服务器将不再被调用此时客户端可以自己准备一个本地的fallback回调,返回一个缺省值。这样做,虽然服务水平下降,但好歹可用,比直接挂掉要强。

整体资源快不够了,忍痛将某些服务先关闭,待度过难关,再开启回来.例如:
假设银行有3个窗口A,B,C,每个窗口提供不同的业务,现在A窗口的服务人员,人手不够急需从C或者B窗口调人处理业务,C或者B窗口挂起暂停服务的牌子

服务降级处理是在客户端,也就是消费者(调用方)实现完成的,与服务端没有关系

服务熔断的做法需要一个方法就需要一个回复的方法,并且回复的方法和业务逻辑方法在一起,代码过于耦合,所以接下来试试这个服务降级的操作:

通用模块配置
服务降级要配合 feign 调用方式才能使用,降级是在消费者方做的 所以直接修改通用模块的代码,先写一个降级处理类,实现FallbackFactory接口;

package com.xyx.common.service;

import com.xyx.common.entity.Book;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 这是降级处理类
 * @Author: xyx
 * @Date: 2019-11-22 18:34
 * @Version: V1.0
 */
@Component // 不要忘记添加,不要忘记添加,千万不要忘记 这是一个巨大的坑
public class BookServiceFallbackFactory implements FallbackFactory<BookClientService> {

    @Override
    public BookClientService create(Throwable throwable) {
       	//实现BookClientService 这个接口,当调用服务端失败的时候就走这里面对应方法的逻辑
        return new BookClientService() {
            @Override
            public Map<String, Object> hystrix() {
                Map<String,Object> map=new HashMap<String,Object>();
                map.put("code", 500);
                map.put("info", "这是在客户端做服务降级");
                return map;
            }

            @Override
            public Book get(Integer id) {
                return null;
            }

            @Override
            public List<Book> list() {
                return null;
            }

            @Override
            public boolean save(Book book) {
                return false;
            }

            @Override
            public boolean delete(Integer id) {
                return false;
            }

            @Override
            public String ribbon() {
                return null;
            }
        };
    }
}


feign 调用接口:重点是在FeignClient注解上加了fallbackFactory 这个参数

package com.xyx.common.service;

import com.xyx.common.entity.Book;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

/**
 * Book Feign接口客户端
 * @author Administrator
 *
 */
 // fallbackFactory 是指定降级处理实现类 
@FeignClient(value = "MICROSERVICE-BOOK", fallbackFactory = BookServiceFallbackFactory.class)
public interface BookClientService {
 
    /**
     * 根据id查询学生信息
     * @param id
     * @return
     */
    @GetMapping(value="/book/get/{id}")
    public Book get(@PathVariable("id") Integer id);
     
    /**
     * 查询学生信息
     * @return
     */
    @GetMapping(value="/book/list")
    public List<Book> list();
     
    /**
     * 添加或者修改学生信息
     * @param book
     * @return
     */
    @PostMapping(value="/book/save")
    public boolean save(Book book);
     
    /**
     * 根据id删除学生信息
     * @return
     */
    @GetMapping(value="/book/delete/{id}")
    public boolean delete(@PathVariable("id") Integer id);

    @RequestMapping("/book/ribbon")
    public String ribbon();


    /**
     * 这是测试服务熔断的
     * @return
     */
    @RequestMapping("/book/hystrix")
    public Map<String, Object> hystrix();
}

microservice-feign-consumer-feign-80修改(具体的服务调用方)
BookConsumerController

package com.xyx.microservicebookconsumerfeign80.controller;

import com.xyx.common.entity.Book;
import com.xyx.common.service.BookClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/book")
public class BookConsumerController  {

    @Autowired
    private BookClientService bookClientService;

    @PostMapping(value = "/save")
    private boolean save(Book book) {
        return bookClientService.save(book);
    }

    @GetMapping(value = "/list")
    public List<Book> list() {
        return bookClientService.list();
    }

    @GetMapping(value = "/get/{id}")
    public Book get(@PathVariable("id") Integer id) {
        return bookClientService.get(id);
    }

    @GetMapping(value = "/delete/{id}")
    public boolean delete(@PathVariable("id") Integer id) {
        try {
            bookClientService.delete(id);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @RequestMapping("/ribbon")
    public String ribbon(){
        return bookClientService.ribbon();
    }

    @RequestMapping("/hystrix")
    public Map<String, Object> hystrix(){
        return bookClientService.hystrix();
    }

}

启动类配置

package com.xyx.microservicebookconsumerfeign80;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;

@EnableEurekaClient
@EnableFeignClients(basePackages = {"com.xyx"})
//注意这里,如果通用模块包名不同那么是可能无法将对应的类加入ioc容器,这个配置非常的重要
@ComponentScan(basePackages = {"com.xyx.common.service", "com.xyx.microservicebookconsumerfeign80"})
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class MicroserviceBookConsumerFeign80Application {

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

}

feign:    #feign负载均衡启动 hystrix
  hystrix:
    enabled: true

测试
启动eureka集群,启动服务提供者集群,启动服务消费者,测试调用 http://localhost/book/hystrix,当服务提供者那边调用不到,或者超时异常的时候
在这里插入图片描述

3、集群后超时设置

使用Feign调用接口分两层,ribbon的调用和hystrix的调用,所以ribbon的超时时间和Hystrix的超时时间的结合就是Feign的超时时间

# 这是 ribbon 调用超时时间设置
ribbon:
  ReadTimeout: 10000
  ConnectTimeout: 9000

#hystrix的超时时间
hystrix:
  command:
    default:
      execution:
        timeout:
          enabled: true
        isolation:
          thread:
            timeoutInMilliseconds: 9000

这样就整合完毕了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值