码迷,mamicode.com
首页 > 其他好文 > 详细

Hystrix集群及集群监控turbine

时间:2019-12-11 00:13:48      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:pack   ash   snapshot   method   text   out   execution   add   autoconf   

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这个也启动,方便测试;

dashboardturbine启动;

 

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

 

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

 技术图片

 

 

 技术图片

 

 

 技术图片

 

 

 

FeignHystrix整合

前面的代码,用@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();
}

 

 

4microservice-student-consumer-feign-80application.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功能失效;

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

 

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

标签:pack   ash   snapshot   method   text   out   execution   add   autoconf   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!