Hystrix集群及监控turbine

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

turbine是基于Dashboard的。

先搞个集群;

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

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

1、yml配置

---
server:
port:
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:
jpa:
hibernate:
ddl-auto: update
show-sql: true
application:
name: microservice-student
profiles: provider-hystrix- eureka:
instance:
hostname: localhost
appname: microservice-student
instance-id: microservice-student:
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-
version: 1.0-SNAPSHOT
userName: http://jt.com
phone: hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: ---
server:
port:
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:
jpa:
hibernate:
ddl-auto: update
show-sql: true
application:
name: microservice-student
profiles: provider-hystrix- eureka:
instance:
hostname: localhost
appname: microservice-student
instance-id: microservice-student:
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-
version: 1.0-SNAPSHOT
userName: http://jt.com
phone: hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: ---
server:
port:
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:
jpa:
hibernate:
ddl-auto: update
show-sql: true
application:
name: microservice-student
profiles: provider-hystrix- eureka:
instance:
hostname: localhost
appname: microservice-student
instance-id: microservice-student:
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-
version: 1.0-SNAPSHOT
userName: http://jt.com
phone: hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds:

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:
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", );
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();
// 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", );
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:
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:
ConnectTimeout:

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);
} }

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

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

    ​ 本文是Spring Cloud专栏的第八篇文章,了解前七篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Clo ...

  2. SpringCloud之Hystrix集群及集群监控turbine

    目的: Hystrix集群及监控turbine Feign.Hystrix整合之服务熔断服务降级彻底解耦 集群后超时设置 Hystrix集群及监控turbine 新建一个springboot工程mic ...

  3. Hystrix集群及监控turbine

    Hystrix集群及监控turbine 前面Dashboard演示的仅仅是单机服务监控,实际项目基本都是集群,所以这里集群监控用的是turbine. turbine是基于Dashboard的. 先搞个 ...

  4. Spring Cloud :断路器集群监控(Turbine)

    一. 简介      上一篇文章我们已经实现了对单个服务实例的监控,当然在实际应用中,单个实例的监控数据没有多大的价值,我们更需要的是一个集群系统的监控信息,这时我们就需要引入Turbine.Turb ...

  5. Kubernetes容器集群管理环境 - Prometheus监控篇

    一.Prometheus介绍之前已经详细介绍了Kubernetes集群部署篇,今天这里重点说下Kubernetes监控方案-Prometheus+Grafana.Prometheus(普罗米修斯)是一 ...

  6. 049.Kubernetes集群管理-集群监控Metrics

    一 集群监控 1.1 Metrics Kubernetes的早期版本依靠Heapster来实现完整的性能数据采集和监控功能,Kubernetes从1.8版本开始,性能数据开始以Metrics API的 ...

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

    原文链接:[MongoDB]windows平台搭建Mongo数据库复制集(类似集群)(一) Replica  Sets(复制集)是在mongodDB1.6版本开始新增的功能,它可以实现故障自动切换和自 ...

  8. Redis集群--Redis集群之哨兵模式

    echo编辑整理,欢迎转载,转载请声明文章来源.欢迎添加echo微信(微信号:t2421499075)交流学习. 百战不败,依不自称常胜,百败不颓,依能奋力前行.--这才是真正的堪称强大!!! 搭建R ...

  9. zookeeper集群+kafka集群 部署

    zookeeper集群 +kafka 集群部署 1.Zookeeper 概述: Zookeeper 定义 zookeeper是一个开源的分布式的,为分布式框架提供协调服务的Apache项目 Zooke ...

随机推荐

  1. Xtrabackup 增量备份 和 增量还原

    目录 测试数据准备 进行全量备份 第一次增量插入一条数据 进行第一次增量备份 注意 第二次增量插入一条数据 进行第二次增量备份 注意 查看xtrabackup_binlog_info中的binlog位 ...

  2. 前端工具-定制ESLint 插件以及了解ESLint的运行原理

    这篇文章目的是介绍如何创建一个ESLint插件和创建一个ESLint rule,用以帮助我们更深入的理解ESLint的运行原理,并且在有必要时可以根据需求创建出一个完美满足自己需求的Lint规则. 插 ...

  3. 洛谷 题解 P5535 【【XR-3】小道消息】

    我又双叒叕被包菜辣! P5535 [XR-3]小道消息(这道题是个大水题 在题干中这位良心的作者就提醒了我们: 你可能需要用到的定理--伯特兰-切比雪夫定理. 那么什么是伯特兰-切比雪夫定理? 我也不 ...

  4. 华为OSPF与ACL综合应用

    一. 实验拓扑图 二.实验要求 1.企业内网运行OSPF路由协议,区域规划如图所示:2.财务和研发所在的区域不受其他区域链路不稳定性影响:3.AR1.AR2.AR3只允许被IT登录管理:4.YF和CW ...

  5. JWT攻击手册:如何入侵你的Token

    JSON Web Token(JWT)对于渗透测试人员而言,可能是一个非常吸引人的攻击途径.因为它不仅可以让你伪造任意用户获得无限的访问权限,而且还可能进一步发现更多的安全漏洞,如信息泄露,越权访问, ...

  6. 数据库Oracle多表链接

    多表查询: 当查询的数据并不是来源一个表时,需要使用多表链接操作完成查询.根据不同表中的数据之间的关系查询相关联的数据. 多表链接方式: 内连接:(等值连接,非等值连接,自连接,SQL99有交叉连接( ...

  7. [TimLinux] HTTP cookie与session技术

    1. HTTP特点 基于TCP/IP协议实现,上层应用协议 版本:HTTP/1.0, HTTP/1.1 HTTP/1.0默认短连接,HTTP/1.1默认长连接 HTTP请求与响应的无状态性 无状态性与 ...

  8. MySQL面试总结

    MySQL面试总结 # MySQL的存储引擎 `MyISAM`(默认表类型):非事务的存储引擎,基于传统的`ISAM`(有索引的顺序访问方法)类型,是存储记录和文件的标准方法,不是事务安全,不支持外键 ...

  9. ARTS-S golang panic返回默认值

    package main import "fmt" func fn_test_panic() (a int) { a = 2 panic("This is panic&q ...

  10. 【H5】344- 微信 H5 页面兼容性解决方案

    点击上方"前端自习课"关注,学习起来~ 最近给公司微信公众号,写了微信h5业务页面,总结分享一下前端开发过程中的几个兼容性坑,项目直接拿的公司页面,所以下文涉及图片都模糊处理了. ...