Hystrix集群及集群监控turbine
Hystrix集群
前篇博客仅仅是单机服务监控,实际项目基本都是集群,所以这里集群监控用的是turbine。
turbine是基于Dashboard的。
这里为了演示方便,一样用我们一个项目直接代替3个项目的集群了
导入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.xy</groupId>
<artifactId>springcloud01</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.xy</groupId>
<artifactId>microservice-student-provider-hystrix</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>microservice-student-provider-hystrix</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.xy</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>
<!--添加注册中心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>
<dependency>
<groupId>com.xy</groupId>
<artifactId>microservice-common</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</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/springboot?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.chen.com:2001/eureka/,http://eureka2002.chen.com:2002/eureka/,http://eureka2003.chen.com:2003/eureka/
info:
groupId: com.chen.springcloud
artifactId: microservice-student-provider-hystrix-1004
version: 1.0-SNAPSHOT
userName: http://chen.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/springboot?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.chen.com:2001/eureka/,http://eureka2002.chen.com:2002/eureka/,http://eureka2003.chen.com:2003/eureka/
info:
groupId: com.chen.springcloud
artifactId: microservice-student-provider-hystrix-1005
version: 1.0-SNAPSHOT
userName: http://chen.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/springboot?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.chen.com:2001/eureka/,http://eureka2002.chen.com:2002/eureka/,http://eureka2003.chen.com:2003/eureka/
info:
groupId: com.chen.springcloud
artifactId: microservice-student-provider-hystrix-1006
version: 1.0-SNAPSHOT
userName: http://chen.com
phone: 123456
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 1500
启动类配置
package com.chen.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.chen.*.*")
@EnableEurekaClient
@SpringBootApplication
public class MicroserviceStudentProviderHystrixApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceStudentProviderHystrixApplication.class, args);
}
}
再新建项目microservice-student-consumer-hystrix-turbine-91
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.chen</groupId>
<artifactId>springcloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>microservice-student-consumer-hystrix-turbine-91</artifactId>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
启动类
package com.chen.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);
}
}
yml配置
server:
port: 91
context-path: /
eureka:
client:
service-url:
defaultZone: http://eureka2001.chen.com:2001/eureka/,http://eureka2002.chen.com:2002/eureka/,http://eureka2003.chen.com:2003/eureka/
turbine:
app-config: microservice-student # 指定要监控的应用名称
clusterNameExpression: "'default'" #表示集群的名字为default
spring:
application:
name: turbine
测试:
先启动三个eureka,然后把1004 1005 带hystrix的服务都启动;
microservice-student-consumer-80这个也启动,方便测试;
dashboard,turbine启动;
这样的话 http://localhost/student/hystrix 就能调用服务集群;
http://localhost:91/turbine.stream 可以监控数据,实时ping 返回data
输入http://localhost:90/hystrix进入仪表盘,输入地址,进行实时监控测试
Feign、Hystrix整合
前面的代码,用@HystrixCommand fallbackMethod是很不好的,因为和业务代码耦合度太高,不利于维护,所以需要解耦,这我们讲下Feign Hystrix整合。
1、microservice-student-provider-hystrix项目修改
我们不用原先那套。按照正常的逻辑来写;
StudentService加新的接口方法:
package com.chen.microservicestudentproviderhystrix.service;
import com.chen.microservicecommon.entity.Student;
import java.util.List;
import java.util.Map;
public interface StudentService {
public void save(Student student);
public Student findById(Integer id);
public List<Student> list();
public void delete(Integer id);
/**
* 测试Hystrix服务降级
* @return
*/
public Map<String,Object> hystrix();
}
impl进行实现
@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方法:
microservice-student-consumer-feign-80修改 支持Hystrix
Controller
/**
* 测试Hystrix服务降级
* @return
*/
@GetMapping(value="/hystrix")
@ResponseBody
public Map<String,Object> hystrix(){
return studentClientService.hystrix();
}
microservice-common的application.yml加上hystrix支持
feign:
hystrix:
enabled: true
然后就会出现一个问题,就是我们之前说的,我们只设置了Hystrix的超时时长,还没设置Ribbon的时间哎,所以它依然会超时
所以我们就需要设置Ribbon的默认时间
microservice-student-consumer-feign-80上加个 ribbon超时时间设置,我们设置的Hystrix默认时长是1500,所以我们只需要比1500大就Ok了
以前hystrix的设置
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 1500
现在添加的Ribbon的设置
ribbon:
ReadTimeout: 10000
ConnectTimeout: 9000
如果不设置,它访问到对应的服务器时就会报错,比如我们1004是另外一个项目的,然后只要不是1004,我没有添加配置,它就会报错
当我们设置了,1005和1006都能访问了,并且一样能进行错误处理