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都能訪問了,並且一樣能進行錯誤處理