文章目录
1、Hystrix集群及监控turbine
我上一篇博客讲解的是Hystrix单机监控Dashboard,
这篇博客讲解的是集群,集群监控用的是turbine。
(有任何不明白的请看我的上几篇博客。)
microservice-student-provider-hystrix
首先新建一个项目:microservice-student-provider-hystrix
(这是重写生产者的项目)
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.zlk</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.zlk</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>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>com.zlk</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>
启动类:
package com.zlk.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.zlk.*.*")
@EnableEurekaClient
@SpringBootApplication
public class MicroserviceStudentProviderHystrixApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceStudentProviderHystrixApplication.class, args);
}
}
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/t224?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.zlk.com:2001/eureka/,http://eureka2002.zlk.com:2002/eureka/,http://eureka2003.zlk.com:2003/eureka/
info:
groupId: com.zlk.springcloud
artifactId: microservice-student-provider-hystrix-1004
version: 1.0-SNAPSHOT
userName: http://zlk.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/t224?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.zlk.com:2001/eureka/,http://eureka2002.zlk.com:2002/eureka/,http://eureka2003.zlk.com:2003/eureka/
info:
groupId: com.zlk.springcloud
artifactId: microservice-student-provider-hystrix-1005
version: 1.0-SNAPSHOT
userName: http://zlk.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/t224?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.zlk.com:2001/eureka/,http://eureka2002.zlk.com:2002/eureka/,http://eureka2003.zlk.com:2003/eureka/
info:
groupId: com.zlk.springcloud
artifactId: microservice-student-provider-hystrix-1006
version: 1.0-SNAPSHOT
userName: http://zlk.com
phone: 123456
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 1500
这样的话 就有了 hystrix集群服务;
microservice-student-consumer-hystrix-turbine-91
我们在新建一个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.zlk</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-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>
yml:
server:
port: 91
context-path: /
eureka:
client:
service-url:
defaultZone: http://eureka2001.zlk.com:2001/eureka/,http://eureka2002.zlk.com:2002/eureka/,http://eureka2003.zlk.com:2003/eureka/
turbine:
app-config: microservice-student # 指定要监控的应用名称
clusterNameExpression: "'default'" #表示集群的名字为default
spring:
application:
name: turbine
启动类:
package com.zlk.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);
}
}
测试
接下来就可以测试了:
先启动eureka,—————》
带hystrix的服务都启动和1004(1004的controller中的hystrix方法设置Thread.sleep(3000)这个操作的意义在于演示超时。)------------》
再启动90 、91、80
这样的话 http://localhost/student/hystrix 就能调用服务集群;
http://localhost:91/turbine.stream 可以监控数据,实时ping 返回data
输入http://localhost:90/hystrix进入仪表盘,
输入地址http://localhost:91/turbine.stream点击按钮,然后调用方法,即可监控成功↓↓↓↓↓↓↓
Feign、Hystrix整合
我之前做回退的时候,是通过 @HystrixCommand(fallbackMethod = “hystrixFallback”)这种方式,这种方式耦合度太高,因为我是直接在controller中写好然后调用的:
不利于维护,所以需要解耦,这我们讲下Feign Hystrix整合。
首先是把controller中的hystrix方法变成正常从service中调用的:
(带hystrix的生产者中的):
service:
/**
* 测试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;
}
controller:
/**
* 测试Hystrix服务降级
*
* @return
* @throws InterruptedException
*/
@ResponseBody
@GetMapping(value = "/hystrix")
@HystrixCommand(fallbackMethod = "hystrixFallback")
public Map<String, Object> hystrix() throws InterruptedException {
Thread.sleep(200);
return studentService.hystrix();
}
然后在common中新建一个 StudentClientFallbackFactory 类,实现FallbackFactory<StudentClientService>
接口;
每一个方法都得有回退方法。
这里我只写hystrix的回退方法,
package com.zlk.microservicecommon.service;
import com.zlk.microservicecommon.entity.Student;
import feign.hystrix.FallbackFactory;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author 大宝
* @company 太厉害公司
* @create 2019-11-24 20:51
*/
@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;
}
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)
再在消费者那边新增这个方法:
/**
* 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
接下来测试,开启eureka,生产者,消费者,
测试代码:http://localhost/student/hystrix
集群后超时设置
结果会报错,会报超时的错,这是因为走的是ribbon,ribbon默认超时时常是1秒,所以这里都是强制要求,ribbon的超时时间要大于hystrix的超时时间,否则 hystrix自定义的超时时间毫无意义。我们得更改:
ribbon:
ReadTimeout: 10000
ConnectTimeout: 9000