springcloud集群监控turbine
Hystrix集群及监控turbine
前面Dashboard演示的仅仅是单机服务监控,实际项目基本都是集群,所以这里集群监控用的是turbine。
turbine是基于Dashboard的。
先搞个集群;
再microservice-student-provider-hystrix-1004项目的基础上再搞一个microservice-student-provider-hystrix-1005
代码和配置都复制一份,然后修改几个地方;
1、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_stu?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.hu.com:2001/eureka/,http://eureka2002.hu.com:2002/eureka/,http://eureka2003.hu.com:2003/eureka/
info:
groupId: com.hu.testSpringcloud
artifactId: microservice-student-provider-hystrix-1004
version: 1.0-SNAPSHOT
userName: http://hu.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/tb_stu?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.hu.com:2001/eureka/,http://eureka2002.hu.com:2002/eureka/,http://eureka2003.hu.com:2003/eureka/
info:
groupId: com.hu.testSpringcloud
artifactId: microservice-student-provider-hystrix-1005
version: 1.0-SNAPSHOT
userName: http://hu.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/tb_stu?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.hu.com:2001/eureka/,http://eureka2002.hu.com:2002/eureka/,http://eureka2003.hu.com:2003/eureka/
info:
groupId: com.hu.testSpringcloud
artifactId: microservice-student-provider-hystrix-1006
version: 1.0-SNAPSHOT
userName: http://hu.com
phone: 123456
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 1500
这个里面需要注意的就是我们设置了hystrix的默认请求超时时间,因为我们在后面还要设置集群后的Ribbon的超时时间所以在这里讲清楚
前面提过,我们默认的请求超时是1S,这里的请求包括注册中心对生产者的请求和消费者对注册中心的请求,但是消费者对注册中心的请求时长就包括了注册中心对生产者的请求时长,而我们是利用Ribbon来连接注册中心和消费者、Hystrix来设置生产者请求注册中心的熔断,但是呢我们Ribbon的操作时间就包括了Hystrix的请求时间,所以我们设置Ribbon的默认超时时间一定要比Hystrix长
项目启动类
package com.hu.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.hu.*.*")
@EnableEurekaClient
@SpringBootApplication
public class MicroserviceStudentProviderHystrixApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceStudentProviderHystrixApplication.class, args);
}
}
这样我们的hystrix的集群就搭建好了
然后我们创建microservice-student-consumer-hystrix-turbine-91来监控这个集群
导入pom依赖
<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:
port: 91
context-path: /
eureka:
client:
service-url:
defaultZone: http://eureka2001.hu.com:2001/eureka/,http://eureka2002.hu.com:2002/eureka/,http://eureka2003.hu.com:2003/eureka/
turbine:
app-config: microservice-student # 指定要监控的应用名称
clusterNameExpression: "'default'" #表示集群的名字为default
spring:
application:
name: turbine
配置启动类
package com.hu.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);
}
}
然后我们先把1004启动、然后把1005、1006带hystrix启动,然后启动80消费者,就能测试了
这样的话 http://localhost/student/hystrix 就能调用服务集群
http://localhost:91/turbine.stream 我们输入看是否能出现ping的字,能就是配置成功了,我们就能进入监控平台了,然后就在Hystrix中输入地址
同时我们在http://localhost/student/hystrix不断请求就能看到监控数据了
Feign和Hystrix整合
然后可以看到我们测试的代码如果是实际业务,那么重复代码就太多了,在多个项目中都是一样的操作,所以我们就利用Feign把公共部分再抽取出来,进行代码的解耦
首先再外面集群的项目microservice-student-provider-hystrix
在service定义方法
/**
* 测试Hystrix服务降级
* @return
*/
public Map<String,Object> hystrix();
serviceimpl
@Value("${server.port}")
private String 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调用方法
/**
* 测试Hystrix服务降级
* @return
* @throws InterruptedException
*/
@ResponseBody
@GetMapping(value="/hystrix")
@HystrixCommand(fallbackMethod="hystrixFallback")
public Map<String,Object> hystrix() throws InterruptedException{
Thread.sleep(2000);
return studentService.hystrix();
}
wmicroservice-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都能访问了,并且一样能进行错误处理