hystrix集群及集群监控turbine

一、hystrix集群及监控turbine

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

在microservice-student-provider-hystrix-1004项目的基础上再创建一个三合一工程microservice-student-provider-hystrix

  1. 导入pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.xfz</groupId>
        <artifactId>xfzmicroservice</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>microservice-student-provider-hystrix</artifactId>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
        </dependency>
        <!--  修改后立即生效,热部署  -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>com.xfz</groupId>
            <artifactId>microservice-common</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>

        <!--添加注册中心Eureka相关配置-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <!-- 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>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

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

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

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

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

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

  1. 启动类中添注解
package com.xfz.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.xfz.*.*")
@EnableEurekaClient
@SpringBootApplication
public class MicroserviceStudentProviderHystrixApplication {

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

}

  1. 将microservice-student-provider-hystrix-1004中的业务代码拷贝到新建的三合一工程中

在这里插入图片描述

完成了上述操作以后,就有了hystrix集群

  1. 接着新建项目microservice-student-consumer-hystrix-turbine-91的监控平台,在原基础上添加turbine的pom依赖
<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>
  1. 添加新建的yml依赖
server:
  port: 91
  context-path: /
eureka:
  client:
    service-url:
      defaultZone: http://eureka2001.xfz.com:2001/eureka/,http://eureka2002.xfz.com:2002/eureka/,http://eureka2003.xfz.com:2003/eureka/
turbine:
  app-config: microservice-student   # 指定要监控的应用名称
  clusterNameExpression: "'default'" #表示集群的名字为default
spring:
  application:
    name: turbine

  1. 在启动类中添加注解
package com.xfz.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);
    }

}

  1. 进行测试

按下列顺序进行操作
在这里插入图片描述

注意:在测试时要在三合一工程的方法中修改一下睡眠时间
在这里插入图片描述

注册中心效果:
在这里插入图片描述
两秒出现的未超时效果
在这里插入图片描述
超时效果
在这里插入图片描述
查看监控效果
在这里插入图片描述
确认地址没错以后再填入地址栏中(http://localhost:91/turbine.stream)
在这里插入图片描述
监控效果:
在这里插入图片描述

二、feign、hystrix整合之服务熔断服务降级彻底解耦

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

  1. 在microservice-student-provider-hystrix项目里将生产者controller层的代码抽取到service中

studentservice.java

public Map<String,Object> hystrix();

实现接口 studentserviceimpl.java

@Value("${server.port}")
    private Integer port;

@Override
    public Map<String, Object> hystrix() {
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("code",200);
        map.put("info","工号【"+port+"】正在为您服务");
        return map;
    }
  1. 之前做熔断的处理,现在不在controller层处理了,重新在microservice-common项目里新建熔断处理类
package com.xfz.microservicecommon.service;

import com.xfz.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 xfz
 * @site www.xfz.com
 * @company zking
 * @create  2020-01-13 20:21
 */
@Component
public class StudentClientFallbackFactory implements FallbackFactory<StudentClientService> {

    @Override
    public StudentClientService create(Throwable cause) {
        return new StudentClientService() {

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

            @Override
            public List<Student> list() {
                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;
            }

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

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

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

}

  1. 在StudentClientService中添加服务熔断降级的方法
/**
     *
     * 服务熔断降级
     * @return
     */
    @GetMapping(value = "/student/hystrix")
    public Map<String,Object> hystrix();
  1. 要完全实现解耦那么需要在实现接口的类中新增注解
@FeignClient(value="MICROSERVICE-STUDENT",fallbackFactory = StudentClientFallbackFactory.class)
  1. 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();
    }
  1. microservice-student-consumer-feign-80里添加yml配置文件加上hystrix支持
feign:
  hystrix:
    enabled: true
  1. microservice-student-consumer-feign-80的启动类上添加公共模块的注解
@ComponentScan(basePackages = {"com.xfz.microservicecommon","com.xfz.microservicestudentconsumerfeign80"})

注意:1、公共子项目与当前子项目的基包都要扫描到;2、只指定公共子模块为基包会导致本子项目的springmvc功能失效;3、只指定本子项目为基包会导致feign与hystrix集成失败,从而导致服务熔断功能失效

  1. 测试

StudentProviderController.java

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

运行
在这里插入图片描述
得到结果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
将StudentProviderController方法中的睡眠时间修改成两秒超时,将单体项目1004中的StudentProviderController方法修改成0.2秒正常服务
重新启动结果1005,1006还显示正在服务,并没有熔断
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
设置的睡眠时间是1.5秒,现在没有显示繁忙那么证明熔断并没有生效,然而这样会容易造成雪崩。

三、集群后超时设置

解决上述代码熔断没生效的错误:
这里因为还有一个 feign 也有一个超时时间的设置,当然feign底层是 ribbon的封装,所以 直接配置ribbon,ribbon默认超时也是1秒。
所以这里都是强制要求,ribbon的超时时间要大于hystrix的超时时间,否则 hystrix自定义的超时时间毫无意义。
所以还得microservice-student-consumer-feign-80上加个 ribbon超时时间设置
application.yml

ribbon:
  ReadTimeout: 10000
  ConnectTimeout: 9000

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

显示效果:
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值