转载请标明出处:https://blog.csdn.net/men_ma/article/details/106847165.
本文出自 不怕报错 就怕不报错的小猿猿 的博客
SpringCloud之Hystrix集群及监控turbine+Feign、Hystrix整合之服务熔断服务降级彻底解耦+集群后超时设置
目标
1、Hystrix集群及监控turbine
2、Feign、Hystrix整合之服务熔断服务降级彻底解耦
3、集群后超时设置
1.Hystrix集群及监控turbine
前面的博客Dashboard演示的仅仅是单机服务监控,实际项目基本都是集群,所以这里集群监控用的是turbine。
turbine是基于Dashboard的。
先搞个集群;
再microservice-student-provider-hystrix-1004项目的基础上再搞一个
microservice-student-provider-hystrix(三合一项目,1004,1005,1006)
pom.xml和microservice-student-provider-hystrix-1004一样的
代码和配置都复制一份,然后修改几个地方;(这些在博主上一篇SpringCloud之熔断器Hystrix及服务监控Dashboard(单机版)博客中有)
- 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/t243?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.javaxl.com:2001/eureka/,http://eureka2002.javaxl.com:2002/eureka/,http://eureka2003.javaxl.com:2003/eureka/
info:
groupId: com.javaxl.testSpringcloud
artifactId: microservice-student-provider-hystrix-1004
version: 1.0-SNAPSHOT
userName: http://javaxl.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/t243?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.javaxl.com:2001/eureka/,http://eureka2002.javaxl.com:2002/eureka/,http://eureka2003.javaxl.com:2003/eureka/
info:
groupId: com.javaxl.testSpringcloud
artifactId: microservice-student-provider-hystrix-1005
version: 1.0-SNAPSHOT
userName: http://javaxl.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/t243?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.javaxl.com:2001/eureka/,http://eureka2002.javaxl.com:2002/eureka/,http://eureka2003.javaxl.com:2003/eureka/
info:
groupId: com.javaxl.testSpringcloud
artifactId: microservice-student-provider-hystrix-1006
version: 1.0-SNAPSHOT
userName: http://javaxl.com
phone: 123456
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 1500
- 启动类配置MicroserviceStudentProviderHystrixApplication
package com.xiaoqing.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.xiaoqing.*.*")
@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加下依赖:
<?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.xiaoqing</groupId>
<artifactId>t243microservice</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>
- application.yml
server:
port: 91
context-path: /
eureka:
client:
service-url:
defaultZone: http://eureka2001.javaxl.com:2001/eureka/,http://eureka2002.javaxl.com:2002/eureka/,http://eureka2003.javaxl.com:2003/eureka/
turbine:
app-config: microservice-student # 指定要监控的应用名称
clusterNameExpression: "'default'" #表示集群的名字为default
spring:
application:
name: turbine
- 启动类配置MicroserviceStudentConsumerHystrixTurbine91Application
package com.xiaoqing.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);
}
}
- 配置Configurations
- 测试:
先启动三个eureka(2001,2002,203),然后把1004 1005 1006带hystrix的服务都启动;
microservice-student-consumer-80这个也启动,方便测试;然后再起90的监控平台,再起91的监控平台服务
dashboard,turbine启动;
注册中心的效果:
访问http://localhost:8080/student/hystrix调用服务集群;的效果:
http://localhost:91/turbine.stream 可以监控数据,实时ping 返回data
输入http://localhost:90/hystrix进入仪表盘,输入地址
点击 进入集群监控仪表:
2.Feign、Hystrix整合之服务熔断服务降级彻底解耦
在博主的上一篇博客中写到的案例是一个方法写了一个熔断方法处理,那么在实际开发过程中,可能会有好多个方法都要熔断方法处理,我们不可能每一个方法都要写一个熔断处理的方法,这样会显得很乱,不便于管理,所以现在我们要跟Feign进行一个整合
前面的代码,用@HystrixCommand fallbackMethod是很不好的,因为和业务代码耦合度太高,不利于维护,所以需要解耦,这我们讲下Feign Hystrix整合。
- microservice-student-provider-hystrix项目修改
我们不用原先那套。按照正常的逻辑来写;
StudentService加新的接口方法:
/**
* 测试Hystrix服务降级
* @return
*/
public Map<String,Object> hystrix();
StudentServiceImpl写具体实现:
@Value("${server.port}")
private Integer 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服务降级
*
* HystrixCommand注解
* @return
* @throws InterruptedException
*/
@GetMapping(value="/hystrix")
// @HystrixCommand(fallbackMethod="hystrixFallback")
public Map<String,Object> hystrix() throws InterruptedException{
// 非正常响应
Thread.sleep(200);
// 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;
}*/
- microservice-common项目新建FallbackFactory类,解耦服务熔断服务降级
StudentClientService接口,新增getInfo方法;
/**
* 服务熔断降级
* @return
*/
@GetMapping(value="/student/hystrix")
public Map<String,Object> hystrix();
新建 StudentClientFallbackFactory 类,实现FallbackFactory接口;
package com.xiaoqing.microservicecommon.service;
import com.xiaoqing.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属性
这类我们实现了 降级处理方法实现;
- microservice-student-consumer-feign-80修改 支持Hystrix
StudentConsumerFeignController新增方法调用
/**
1. Feign整合Hystrix服务熔断降级
2. @return
3. @throws InterruptedException
*/
@GetMapping(value="/hystrix")
public Map<String,Object> hystrix() throws InterruptedException{
return studentClientService.hystrix();
}
- microservice-student-consumer-feign-80的application.yml加上hystrix支持
feign:
hystrix:
enabled: true
- microservice-student-consumer-feign-80的启动类上添加公共模块
package com.xiaoqing.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;
@EnableEurekaClient
@EnableFeignClients(value = "com.xiaoqing.*.*")
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@ComponentScan(basePackages = {"com.xiaoqing.microservicecommon","com.xiaoqing.microservicestudentconsumerfeign80"})//扫描公共模块
public class MicroserviceStudentConsumerFeign80Application {
public static void main(String[] args) {
SpringApplication.run(MicroserviceStudentConsumerFeign80Application.class, args);
}
}
注意:
- 公共子项目与当前子项目的基包都要扫描到;
- 只指定公共子模块为基包会导致本子项目的springmvc功能失效;
- 只指定本子项目为基包会导致feign与Hystrix集成失败,从而导致服务熔断功能失效
测试开启三个eureka,以及带hystrix的provider,和带feign,hystrix的consummer的服务。
测试的话,也是没问题的。0.9秒的话,返回正常信息;超过1秒的话,就返回错误提示;
3.集群后超时设置
上面错误是什么原因呢,咱们明明在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
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 1500
这样就完工了。自行测试结果如下