Hystrix集群及集群监控turbine

Hystrix集群及监控turbine

前面Dashboard演示的仅仅是单机服务监控,实际项目基本都是集群,所以这里集群监控用的是turbine。

turbine是基于Dashboard的。

先搞个集群;

再microservice-student-provider-hystrix-1004项目的基础上再搞一个microservice-student-provider-hystrix-1005

代码和配置都复制一份,然后修改几个地方;

1、yml配置

---
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/xm1?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.jt.com:2001/eureka/,http://eureka2002.jt.com:2002/eureka/,http://eureka2003.jt.com:2003/eureka/

info:
  groupId: com.jt.testSpringcloud
  artifactId: microservice-student-provider-hystrix-1004
  version: 1.0-SNAPSHOT
  userName: http://jt.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/xm1?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.jt.com:2001/eureka/,http://eureka2002.jt.com:2002/eureka/,http://eureka2003.jt.com:2003/eureka/

info:
  groupId: com.jt.testSpringcloud
  artifactId: microservice-student-provider-hystrix-1005
  version: 1.0-SNAPSHOT
  userName: http://jt.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/xm1?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.jt.com:2001/eureka/,http://eureka2002.jt.com:2002/eureka/,http://eureka2003.jt.com:2003/eureka/

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

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

2、启动类配置

package com.jt.microservicestudentproviderhystrix;

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.jt.*.*")
@EnableEurekaClient
@SpringBootApplication
public class MicroserviceStudentProviderHystrixApplication {

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

}

3、我们新建项目microservice-student-consumer-hystrix-turbine-91

pom.xml加下依赖

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

application.yml

server:
  port: 91
  context-path: /

eureka:
  client:
    service-url:
      defaultZone: http://eureka2001.jt.com:2001/eureka/,http://eureka2002.jt.com:2002/eureka/,http://eureka2003.jt.com:2003/eureka/

turbine:
  app-config: microservice-student   # 指定要监控的应用名称
  clusterNameExpression: "‘default‘" #表示集群的名字为default

spring:
  application:
    name: turbine

1、新建启动类MicroserviceStudentConsumerHystrixTurbine91Application 加注解:@EnableTurbine

package com.jt.microservicestudentconsumerhystrixturbine91;

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;

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

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

}

测试:

先启动三个eureka,然后把1004  1005 带hystrix的服务都启动;

microservice-student-consumer-80这个也启动,方便测试;

dashboard,turbine启动;

这样的话 http://localhost/student/hystrix 就能调用服务集群;

http://localhost:91/turbine.stream  可以监控数据,实时ping 返回data

Feign、Hystrix整合

前面的代码,用@HystrixCommand fallbackMethod是很不好的,因为和业务代码耦合度太高,不利于维护,所以需要解耦,这我们讲下Feign Hystrix整合。

1、microservice-student-provider-hystrix项目修改

我们不用原先那套。按照正常的逻辑来写;

StudentService加新的接口方法:

/**
 * 测试Hystrix服务降级
 * @return
 */
public Map<String,Object> hystrix();

StudentServiceImpl写具体实现:

@Override
public Map<String, Object> hystrix() {
    Map<String,Object> map=new HashMap<String,Object>();
    map.put("code", 200);
    map.put("info","工号【"+port+"】正在为您服务");
    return map;
}

StudentProviderController正常调用service方法:

/**
     * 测试Hystrix服务降级
     * @return
     * @throws InterruptedException
     */
    @ResponseBody
    @GetMapping(value="/hystrix")
//    @HystrixCommand(fallbackMethod="hystrixFallback")
    public Map<String,Object> hystrix() throws InterruptedException{
        Thread.sleep(100);
//        Map<String,Object> map=new HashMap<String,Object>();
//        map.put("code", 200);
//        map.put("info","工号【"+port+"】正在为您服务");
        return this.studentService.hystrix();
    }

//    public Map<String,Object> hystrixFallback() throws InterruptedException{
//        Map<String,Object> map=new HashMap<String,Object>();
//        map.put("code", 500);
//        map.put("info", "系统【"+port+"】繁忙,稍后重试");
//        return map;
//    }

2、microservice-common项目新建FallbackFactory类,解耦服务熔断服务降级

StudentClientService接口,新增getInfo方法;

/**
 * 服务熔断降级
 * @return
 */
@GetMapping(value="/student/hystrix")
public Map<String,Object> hystrix();

新建 StudentClientFallbackFactory 类,实现FallbackFactory<StudentClientService>接口;

package com.jt.microservicecommon.service;

import com.jt.microservicecommon.entity.Student;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;

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

/**
 * @author jt
 * @site www.xiaomage.com
 * @company xxx公司
 * @create  2019-12-10 21:50
 */
@Component
public class StudentClientFallbackFactory implements FallbackFactory<StudentClientService> {
    @Override
    public StudentClientService create(Throwable throwable) {
        return new StudentClientService() {
            @Override
            public Student get(Integer id) {
                return null;
            }

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

            @Override
            public boolean save(Student student) {
                return false;
            }

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

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

            @Override
            public Map<String, Object> hystrix() {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("code", 500);
                map.put("info", "系统繁忙,请稍后重试。。。。");
                return map;
            }
        };
    }
}

StudentClientService接口的@FeignClient注解加下 fallbackFactory属性

@FeignClient(value="MICROSERVICE-STUDENT",fallbackFactory=StudentClientFallbackFactory.class)

这类我们实现了 降级处理方法实现;

3、microservice-student-consumer-feign-80修改 支持Hystrix

StudentConsumerFeignController新增方法调用

/**
 * Feign整合Hystrix服务熔断降级
 * @return
 * @throws InterruptedException
 */
@GetMapping(value="/hystrix")
public Map<String,Object> hystrix() throws InterruptedException{
    return studentClientService.hystrix();
}

4、microservice-student-consumer-feign-80的application.yml加上hystrix支持

server:
  port: 8080
  context-path: /
eureka:
  client:
    service-url:
      defaultZone: http://eureka2001.jt.com:2001/eureka/,http://eureka2002.jt.com:2002/eureka/,http://eureka2003.jt.com:2003/eureka/
    register-with-eureka: false

feign:
  hystrix:
    enabled: true

ribbon:
  ReadTimeout: 10000
  ConnectTimeout: 9000

1、microservice-student-consumer-feign-80的启动类上添加公共模块

@ComponentScan(basePackages = {"com.javaxl.microservicecommon","com.javaxl.microservicestudentconsumerfeign80"})

注意:

公共子项目与当前子项目的基包都要扫描到;

只指定公共子模块为基包会导致本子项目的springmvc功能失效;

只指定本子项目为基包会导致feign与Hystrix集成失败,从而导致服务熔断功能失效

package com.jt.microservicestudentconsumerfeign80;

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;

@ComponentScan(basePackages = {"com.jt.microservicecommon","com.jt.microservicestudentconsumerfeign80"})//扫描公共模块
@EnableFeignClients(value = "com.jt.*.*")
@EnableEurekaClient
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class MicroserviceStudentConsumerFeign80Application {

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

}

原文地址:https://www.cnblogs.com/ztbk/p/12019641.html

时间: 2024-08-01 01:39:02

Hystrix集群及集群监控turbine的相关文章

Spring Cloud第八篇 | Hystrix集群监控Turbine

? 本文是Spring Cloud专栏的第八篇文章,了解前七篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Cloud第二篇 | 使用并认识Eureka注册中心 Spring Cloud第三篇 | 搭建高可用Eureka注册中心 Spring Cloud第四篇 | 客户端负载均衡Ribbon Spring Cloud第五篇 | 服务熔断Hystrix Spring Cloud第六篇 | Hystrix仪表盘监控

用python + hadoop streaming 编写分布式程序(二) -- 在集群上运行与监控

写在前面 前文:用python + hadoop streaming 编写分布式程序(一) -- 原理介绍,样例程序与本地调试 为了方便,这篇文章里的例子均为伪分布式运行,一般来说只要集群配置得当,在伪分布式下能够运行的程序,在真实集群上也不会有什么问题. 为了更好地模拟集群环境,我们可以在mapred-site.xml中增设reducer和mapper的最大数目(默认为2,实际可用数目大约是CPU核数-1). 假设你为Hadoop安装路径添加的环境变量叫$HADOOP_HOME(如果是$HAD

Oracle数据库精讲课程之Rac管理(集群组件、性能监控及调整、节点管理、备份和恢复)

对这个课程有兴趣的朋友可以加我的QQ2059055336和我联系 本课程主要是介绍Oracle RAC体系结构与工作机制,了解并掌握RAC数据库下的相关技术,如:cache Fusion. Failover.load balance.FAN.OCR和Voting disk等,通过VMWARE虚拟环境,实践演练RAC数据库的安装部署.RAC数据库日常性能监控.备份和恢复.实例增加和删除以及补丁安装等操作,通过本课程的学习,学员在掌握RAC理论知识基础上,能够熟练掌握RAC数据库的日常管理操作. 课

【MongoDB】windows平台搭建Mongo数据库复制集(类似集群)(转)

原文链接:[MongoDB]windows平台搭建Mongo数据库复制集(类似集群)(一) Replica  Sets(复制集)是在mongodDB1.6版本开始新增的功能,它可以实现故障自动切换和自动修复功能成员节点的功能,各个DB之间的数据完全一致,大大降低了单点故障的风险. [] 以上图示是三个节点的Replica Set架构.该图来源于红丸编写的<Mongodb管理与开发精要>这本书.从上图可以看出,结构类似与一个集群,其实完全可以当做一个集群.因为它确实和集群实现的作用是相同的. 一

搭建高可用的MongoDB集群副本集

什么是副本集呢?打魔兽世界总说打副本,其实这两个概念差不多一个意思.游戏里的副本是指玩家集中在高峰时间去一个场景打怪,会出现玩家暴多怪物少的情况,游戏开发商为了保证玩家的体验度,就为每一批玩家单独开放一个同样的空间同样的数量的怪物,这一个复制的场景就是一个副本,不管有多少个玩家各自在各自的副本里玩不会互相影响. mongoDB的副本也是这个,主从模式其实就是一个单副本的应用,没有很好的扩展性和容错性.而副本集具有多个副本保证了容错性,就算一个副本挂掉了还有很多副本存在,并且解决了上面第一个问题"

【ZooKeeper系列】1.ZooKeeper单机版、伪集群和集群环境搭建

ZooKeeper安装模式主要有3种: 单机版(Standalone模式)模式:仅有一个ZooKeeper服务 伪集群模式:单机多个ZooKeeper服务 集群模式:多机多ZooKeeper服务 1 单机版(Standalone模式)安装 ZooKeeper官网下载地址:http://zookeeper.apache.org/releases.html#download 如图所示进行操作: 注意一点,如果不想当小白鼠,请务必下稳定版(stable release),非稳定版安装时可能出各种未知的

zookeeper(单机、伪集群、集群)部署

ZooKeeper是一个分布式的.开源的分布式应用程序协调服务,可以在分布 式环境中实现应用配置管理.统一命名服务.状态同步服务等功能.ZooKeeper是一种为分布式应用所设计的高可用.高性能的开源协调服务,它提供了一项基本服务:分布式锁 服务.由于ZooKeeper开源的特性,在其分布式锁实现的基础上,又被摸索出了其它的功用,譬如:配置维 护.组服务.分布式消息队列等等. ZooKeeper维护了一个类似文件系统的数据结构,其内部每个子目录都被 称作znode(目录节点),与文件系统一样,我

《数据结构与算法分析:C语言描述》复习——第八章“并查集”——并查集

2014.06.18 14:16 简介: “并查集”,英文名为“union-find set”,从名字就能看出来它支持合并与查找功能.另外还有一个名字叫“disjoint set”,中文名叫不相交集合.可能我们倾向于用最短的名字,所以就出现了“并查集”翻译为“disjoint set”的情况.并查集是一种树形结构,但与之前讲的树不同的是,这里的树节点只记录父节点,因此是一对一的,就可以用数组来表示并查集. 图示: 并查集可以认为是一个“森林”,也就是多棵树: 既然是并查集,先看看合并3和5之后结

Spring Cloud实战之初级入门(四)— 利用Hystrix实现服务熔断与服务监控

目录 1.环境介绍 2.服务监控 2.1 加入依赖 2.2 修改配置文件 2.3 修改启动文件 2.4 监控服务 2.5 小结 3. 利用hystrix实现消费服务熔断 3.1 加入服务熔断 3.2 测试服务熔断 4. 利用turbine监控所有应用 4.1 创建工程 4.2 修改配置文件 4.3 修改启动文件 4.4 启动 5.一点点重要的事情 1.环境介绍 本篇文章涉及到前面文章的工程,mirco-service-provider.mirco-service-consumer以及需要另外新建