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

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

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

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

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

2、啟動類配置

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

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

}

這樣的話 就有了 hystrix集群服務;

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>

4application.yml

server:
  port: 91
  context-path: /
eureka:
  client:
    service-url:
      defaultZone: http://eureka2001.huang.com:2001/eureka/,http://eureka2002.huang.com:2002/eureka/,http://eureka2003.huang.com:2003/eureka/
turbine:
  app-config: microservice-student   # 指定要監控的應用名稱
  clusterNameExpression: "'default'" #表示集群的名字為default
spring:
  application:
    name: turbine

5、新建啟動類MicroserviceStudentConsumerHystrixTurbine91Application 加注解:@EnableTurbine

package com.huang.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

輸入http://localhost:90/hystrix進入儀表盤,輸入地址

點擊 進入集群監控儀表:

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.huang.microservicecommon.service;

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

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

@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 Student get(Integer id) {
                return null;
            }

            @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支持

feign:
  hystrix:
    enabled: true

1、microservice-student-consumer-feign-80啟動類上添加公共模塊

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

注意:

  • 公共子項目與當前子項目的基包都要掃描到;
  • 只指定公共子模塊為基包會導致本子項目的springmvc功能失效;
  • 只指定本子項目為基包會導致feignHystrix集成失敗,從而導致服務熔斷功能失效
package com.huang.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.javaxl.microservicecommon","com.huang.microservicestudentconsumerfeign80"})//掃描公共模塊
@EnableFeignClients(value = "com.javaxl.*.*")
@EnableEurekaClient
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class MicroserviceStudentConsumerFeign80Application {

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

}

測試開啟三個eureka,以及帶hystrixprovider,和帶feign,hystrixconsummer

測試的話,也是沒問題的。0.9秒的話,返回正常信息;超過1秒的話,就返回錯誤提示;

 

 

 

集群后超時設置

 

上面錯誤是什么原因呢,咱們明明在Hystrix中的application.yml中設置了

 

 

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

 

這里因為還有一個 feign 也有一個超時時間的設置,當然feign底層是 ribbon的封裝,所以 直接配置ribbon,ribbon默認超時也是1秒。

所以這里都是強制要求,ribbon的超時時間要大於hystrix的超時時間,否則 hystrix自定義的超時時間毫無意義。

所以還得microservice-student-consumer-feign-80上加個 ribbon超時時間設置

 

 

ribbon:
  ReadTimeout: 10000
  ConnectTimeout: 9000

 

這樣就完工了。可以自行測試。

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM