Hystrix与Feign的服务集群和监控服务集群的turbine
1、Hystrix集群及监控turbine
在microservice-student-provider-hystrix-1004的基础上建立新项目microservice-student-provider-hystrix
参考博文https://blog.youkuaiyun.com/water_java/article/details/103933914
改变一下yml文件
---
server:
port: 1004
context-path: /
eureka:
instance:
hostname: localhost #eureka客户端主机实例名称
appname: microservice-student #客户端服务名
instance-id: microservice-student:1004 #客户端实例名称
prefer-ip-address: true #显示IP
client:
service-url:
defaultZone: http://eureka2001.hsl.com:2001/eureka/,http://eureka2002.hsl.com:2002/eureka/,http://eureka2003.hsl.com:2003/eureka/ #把服务注册到eureka注册中心
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: 1234
jpa:
hibernate:
ddl-auto: update
show-sql: true
application:
name: microservice-student
profiles: provider-hystrix-1004
#暴露全部的监控信息
#management:
# endpoints:
# web:
# exposure:
# include: "*"
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 1500
info:
groupId: com.hsl.testSpringcloud
artifactId: microservice-student-provider-hystrix-1004
version: 1.0-SNAPSHOT
userName: http://hsl.com
phone: 123456
---
server:
port: 1005
context-path: /
eureka:
instance:
hostname: localhost #eureka客户端主机实例名称
appname: microservice-student #客户端服务名
instance-id: microservice-student:1005 #客户端实例名称
prefer-ip-address: true #显示IP
client:
service-url:
defaultZone: http://eureka2001.hsl.com:2001/eureka/,http://eureka2002.hsl.com:2002/eureka/,http://eureka2003.hsl.com:2003/eureka/ #把服务注册到eureka注册中心
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: 1234
jpa:
hibernate:
ddl-auto: update
show-sql: true
application:
name: microservice-student
profiles: provider-hystrix-1005
#暴露全部的监控信息
#management:
# endpoints:
# web:
# exposure:
# include: "*"
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 1500
info:
groupId: com.hsl.testSpringcloud
artifactId: microservice-student-provider-hystrix-1005
version: 1.0-SNAPSHOT
userName: http://hsl.com
phone: 123456
---
server:
port: 1006
context-path: /
eureka:
instance:
hostname: localhost #eureka客户端主机实例名称
appname: microservice-student #客户端服务名
instance-id: microservice-student:1006 #客户端实例名称
prefer-ip-address: true #显示IP
client:
service-url:
defaultZone: http://eureka2001.hsl.com:2001/eureka/,http://eureka2002.hsl.com:2002/eureka/,http://eureka2003.hsl.com:2003/eureka/ #把服务注册到eureka注册中心
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: 1234
jpa:
hibernate:
ddl-auto: update
show-sql: true
application:
name: microservice-student
profiles: provider-hystrix-1006
#暴露全部的监控信息
#management:
# endpoints:
# web:
# exposure:
# include: "*"
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 1500
info:
groupId: com.hsl.testSpringcloud
artifactId: microservice-student-provider-hystrix-1006
version: 1.0-SNAPSHOT
userName: http://hsl.com
phone: 123456
新建项目microservice-student-consumer-hystrix-turbine-91
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.hsl</groupId>
<artifactId>t237microservice</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>
yml文件配置
server:
port: 91
context-path: /
eureka:
client:
service-url:
defaultZone: http://eureka2001.hsl.com:2001/eureka/,http://eureka2002.hsl.com:2002/eureka/,http://eureka2003.hsl.com:2003/eureka/
turbine:
app-config: microservice-student # 指定要监控的应用名称
clusterNameExpression: "'default'" #表示集群的名字为default
spring:
application:
name: turbine
启动类:MicroserviceStudentConsumerHystrixTurbine91Application 添加注解@EnableTurbine
package com.hsl.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);
}
}
StudentProviderController
配置差异性启动:
两份microservice-student-provider-hystrix,分别为1005,1006
启动注册中心,1004,1005,1006,microservice-student-consumer-80,dashboard,turbine
注册中心
访问:http://localhost/student/hystrix
访问:http://localhost:91/turbine.stream
http://localhost:90/hystrix进入仪表盘
输入地址:http://localhost:91/turbine.stream
2、Feign、Hystrix整合之服务熔断服务降级彻底解耦
之前使用@HystrixCommand fallbackMethod是很不好的,因为和业务代码耦合度太高,不利于维护,所以需要解耦,这里使用feign和hystrix整合
service层添加代码
StudentService
/**
* 测试Hystrix服务降级
* @return
*/
public Map<String,Object> hystrix();
实现类:
@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;
}
controller层
microservice-common项目新建FallbackFactory类,解耦服务熔断服务降级
StudentClientService接口,新增getInfo方法
/**
* 服务熔断降级
* @return
*/
@GetMapping(value="/student/hystrix")
public Map<String,Object> hystrix();
新建 StudentClientFallbackFactory 类,实现FallbackFactory<StudentClientService>
接口
package com.hsl.microservicecommon.service;
import com.hsl.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-student-consumer-feign-80的yml文件修改,加上hystrix支持
feign:
hystrix:
enabled: true
microservice-student-consumer-feign-80的启动类上添加公共模块
注意:
公共子项目与当前子项目的基包都要扫描到;
只指定公共子模块为基包会导致本子项目的springmvc功能失效;
只指定本子项目为基包会导致feign与Hystrix集成失败,从而导致服务熔断功能失效
@ComponentScan(basePackages = {"com.hsl.microservicecommon","com.hsl.microservicestudentconsumerfeign80"})
改变microservice-student-provider-hystrix的controller层
改变microservice-student-provider-hystrix-1004的controller层
测试:
开启三个eureka,以及带hystrix的provider,和带feign,hystrix的consummer
访问http://localhost:1004/student/hystrix服务正常服务
http://localhost:1005/student/hystrix和http://localhost:1006/student/hystrix应该出现服务熔断,服务繁忙的提示
可是出现的结果如下:
搭建失败,熔断未生效,可能出现雪崩
出现这样现象的原因:
虽然在Hystrix中的yml文件中设置了超时设置:
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 1500
但是还有一个 feign 也有一个超时时间的设置,当然feign底层是 ribbon的封装,所以直接配置ribbon,ribbon默认超时也是1秒。
所以这里都是强制要求,ribbon的超时时间要大于hystrix的超时时间,否则 hystrix自定义的超时时间毫无意义。
解决方式就是后面的集群后超时设置
3、集群后超时设置
为解决上面的问题,得在microservice-student-consumer-feign-80的yml文件上加个 ribbon超时时间设置
ribbon:
ReadTimeout: 10000
ConnectTimeout: 9000
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 1500
这样既把代码解耦了,也解决了雪崩的问题
测试:
启动注册中心,启动三个生产者(1004,1005,1006),启动dashboard,turbine,feign