1、Hystrix集群及监控turbine
上一篇博客Dashboard演示的仅仅是单机服务监控,实际项目基本都是集群,所以这里集群监控用的是turbine。
turbine是基于Dashboard的。
先搭建一个集群:
新建一个 microservice-student-provider-hystrix 项目 改造上一篇博客写的 microservice-student-provider-hystrix-1004 项目 代码和配置都复制一份,然后修改几个地方;
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/tb_student?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:2001/eureka/,http://eureka2002:2002/eureka/,http://eureka2003:2003/eureka/
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 1500
info:
groupId: com.hyf.testSpringcloud
artifactId: microservice-student-provider-hystrix-1004
version: 1.0-SNAPSHOT
userName: http://hyf.com
phone: 18188888888
---
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/tb_student?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:2001/eureka/,http://eureka2002:2002/eureka/,http://eureka2003:2003/eureka/
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 1500
info:
groupId: com.hyf.testSpringcloud
artifactId: microservice-student-provider-hystrix-1005
version: 1.0-SNAPSHOT
userName: http://hyf.com
phone: 18188888888
---
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/tb_student?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:2001/eureka/,http://eureka2002:2002/eureka/,http://eureka2003:2003/eureka/
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 1500
info:
groupId: com.hyf.testSpringcloud
artifactId: microservice-student-provider-hystrix-1006
version: 1.0-SNAPSHOT
userName: http://hyf.com
phone: 18188888888
2、启动类配置
hystrix集群服务搭建好了
3、我们新建项目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:
context-path: /
port: 91
eureka:
client:
service-url:
defaultZone :
http://eureka2001:2001/eureka/,http://eureka2002:2002/eureka/,http://eureka2003:2003/eureka/
turbine:
app-config: microservice-student #指定要监控的应用名称
cluster-name-expression: "'default'" #表示集群的名字为default
spring:
application:
name: turbine
4、新建启动类MicroserviceStudentConsumerHystrixTurbine91Application 加注解:@EnableTurbine
测试:
先启动三个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进入仪表盘,输入地址
2、Feign、Hystrix整合
前面的代码,用@HystrixCommand fallbackMethod是很不好的,因为和业务代码耦合度太高,不利于维护,所以需要解耦,这我们讲下Feign Hystrix整合。
在你的公共项目microservice-common
新建一个类
StudentClientFallbackFactory
package com.hyf.microservicecommon.server;
import com.hyf.microservicecommon.entity.Student;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author xhy
* @site www.4399.com
* @company xxx公司
* @create 2020-01-13 19:20
*/
@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;
}
@Override
public Map<String, Object> hystrix() {
Map<String,Object> map = new HashMap<>();
map.put("code",500);
map.put("info","系统繁忙,稍后再试");
return map;
}
};
}
}
在StudentClientService添加注解和方法
package com.hyf.microservicecommon.server;
import com.hyf.microservicecommon.entity.Student;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
import java.util.Map;
/**
* Student Feign接口客户端
* @author Administrator
*
*/
@FeignClient(value="MICROSERVICE-STUDENT",fallbackFactory =StudentClientFallbackFactory.class )
public interface StudentClientService {
/**
* 根据id查询学生信息
* @param id
* @return
*/
@GetMapping(value="/student/get/{id}")
public Student get(@PathVariable("id") Integer id);
/**
* 查询学生信息
* @return
*/
@GetMapping(value="/student/list")
public List<Student> list();
/**
* 添加或者修改学生信息
* @param student
* @return
*/
@PostMapping(value="/student/save")
public boolean save(Student student);
/**
* 根据id删除学生信息
* @return
*/
@GetMapping(value="/student/delete/{id}")
public boolean delete(@PathVariable("id") Integer id);
@RequestMapping("/student/ribbon")
public String ribbon();
/**
* 服务熔断降级
* @return
*/
@GetMapping(value="/student/hystrix")
public Map<String,Object> hystrix();
}
项目microservice-student-provider-hystrix中
在service定义方法
/**
* 测试Hystrix服务降级
* @return
*/
public Map<String,Object> hystrix();
serviceimpl
package com.hyf.microservicestudentproviderhystrix.service.impl;
import com.hyf.microservicecommon.entity.Student;
import com.hyf.microservicestudentproviderhystrix.repository.StudentRepository;
import com.hyf.microservicestudentproviderhystrix.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class StudentServiceImpl implements StudentService {
@Value("${server.port}")
private Integer port;
@Autowired
private StudentRepository studentRepository;
@Override
public void save(Student student) {
studentRepository.save(student);
}
@Override
public Student findById(Integer id) {
return studentRepository.findOne(id);
}
@Override
public List<Student> list() {
return studentRepository.findAll();
}
@Override
public void delete(Integer id) {
studentRepository.delete(id);
}
@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(2000);
return studentService.hystrix();
}
microservice-student-consumer-feign-80修改 支持Hystrix
Controller
/**
* Feign整合Hystrix服务熔断降级
* @return
* @throws InterruptedException
*/
@GetMapping(value="/hystrix")
public Map<String,Object> hystrix() throws InterruptedException{
return studentClientService.hystrix();
}
wmicroservice-student-consumer-feign-80的application.yml加上hystrix支持
feign:
hystrix:
enabled: true
集群后超时设置
这里因为还有一个 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
运行时:
单体项目1004时:
1005,1006