Hystrix集羣及監控turbine
Dashboard演示的僅僅是單機服務監控
這裏集羣監控用的是turbine
turbine是基於Dashboard的。
pom.xml
<?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.zh</groupId>
<artifactId>springcloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>microservice-student-provider-hystrix</artifactId>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.zh</groupId>
<artifactId>microservice-common</artifactId>
</dependency>
<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>com.zh</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>
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/test?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.zh.com:2001/eureka/,http://eureka2002.zh.com:2002/eureka/,http://eureka2003.zh.com:2003/eureka/
info:
groupId: com.zh.springcloud
artifactId: microservice-student-provider-hystrix-1004
version: 1.0-SNAPSHOT
userName: http://zh.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/test?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.zh.com:2001/eureka/,http://eureka2002.zh.com:2002/eureka/,http://eureka2003.zh.com:2003/eureka/
info:
groupId: com.zh.springcloud
artifactId: microservice-student-provider-hystrix-1005
version: 1.0-SNAPSHOT
userName: http://zh.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/test?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.zh.com:2001/eureka/,http://eureka2002.zh.com:2002/eureka/,http://eureka2003.zh.com:2003/eureka/
info:
groupId: com.zh.springcloud
artifactId: microservice-student-provider-hystrix-1006
version: 1.0-SNAPSHOT
userName: http://zh.com
phone: 123456
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 1500
從1004複製過來一份(前幾篇博客有具體代碼)
啓動類配置
package com.zh.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.zh.*.*")
@EnableEurekaClient
@SpringBootApplication
public class MicroserviceStudentProviderHystrixApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceStudentProviderHystrixApplication.class, args);
}
}
這樣的話 就有了 hystrix集羣服務;
我們新建項目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.zh.com:2001/eureka/,http://eureka2002.zh.com:2002/eureka/,http://eureka2003.zh.com:2003/eureka/
turbine:
app-config: microservice-student # 指定要監控的應用名稱
clusterNameExpression: "'default'" #表示集羣的名字爲default
spring:
application:
name: turbine
啓動類加註解
package com.zh.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;
@EnableTurbine
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class MicroserviceStudentConsumerHystrixTurbine91Application {
public static void main(String[] args) {
SpringApplication.run(MicroserviceStudentConsumerHystrixTurbine91Application.class, args);
}
}
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寫具體實現:
@Value("${server.port}")
private String 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;
}
StudentProviderController正常調用service方法:
/**
* 測試Hystrix服務降級
* @return
* @throws InterruptedException
*/
@ResponseBody
@GetMapping(value="/hystrix")
@HystrixCommand(fallbackMethod="hystrixFallback")
public Map<String,Object> hystrix() throws InterruptedException{
Thread.sleep(2000);
// Map<String,Object> map=new HashMap<String,Object>();
// map.put("code", 200);
// map.put("info","工號【"+port+"】正在爲您服務");
return studentService.hystrix();
}
microservice-common項目新建FallbackFactory類,解耦服務熔斷服務降級
StudentClientService接口,新增getInfo方法;
/**
* 服務熔斷降級
* @return
*/
@GetMapping(value="/student/hystrix")
public Map<String,Object> hystrix();
新建 StudentClientFallbackFactory 類,實現FallbackFactory接口;
package com.zh.microservicecommon.service;
import com.zh.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 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;
}
};
}
}
StudentClientService接口的@FeignClient註解加下 fallbackFactory屬性
@FeignClient(value="MICROSERVICE-STUDENT",fallbackFactory=StudentClientFallbackFactory.class)
這類我們實現了 降級處理方法實現
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();
}
microservice-common的application.yml加上hystrix支持:
feign:
hystrix:
enabled: true
測試:
集羣后超時設置
上面錯誤是什麼原因呢,咱們明明在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