2.1.2 启动类配置开启 hystrix-dashboard
3.2 manager-server服务搭建(测试turbine)
一、项目结构
二、Hystrix-DashBoard实现
2.1 搭建hystrix-dashboard项目
2.1.1 引入依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
</dependencies>
2.1.2 启动类配置开启 hystrix-dashboard
package com.wj.hystrix.dashboard;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
/**
* hystrix的仪表盘项目,只能监控一个微服务
* @author wangjian
*/
@EnableHystrixDashboard//开启仪表盘功能
@SpringBootApplication
public class HystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardApplication.class,args);
}
}
2.1.3 application.yml配置端口号
server:
port: 9300
2.2 portal-server配置端点
2.2.1 引入依赖
<!--springboot服务监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.2.2 application.yml文件
# 端口号配置
server:
port: 9100
# 应用名称配置
spring:
application:
name: portal-server
#eureka客户端配置
eureka:
instance:
# 向注册中心发送心跳间隔时间,告诉注册中心自己还活着;默认时间是30s
lease-renewal-interval-in-seconds: 10
# 如果30秒内没有向注册中心发送心跳,代表发生故障,从注册中心移除掉,默认时间是90s
lease-expiration-duration-in-seconds: 30
#告诉服务端,服务实例以ip作为连接,而不是机器名,默认是false
prefer-ip-address: true
#告诉服务端,服务实例的名称
instance-id: ${spring.application.name}
client:
#注册中心连接地址
service-url:
defaultZone: http://127.0.0.1:8761/eureka/,http://localhost:8762/eureka/
#hystrix配置
#hystrix的超时时间可在配置文件或注解配置
#hystrix.command.default.execution.timeout.enabled说明:
#为true时,需两个方法配置超时设置,分别是Ribbon的ReadTimeout和hystrix的timeoutInMilliseconds,以值小的为准
#为false时,hystrix不进行超时熔断,根据Ribbon的ReadTimeout抛出的异常进行熔断,具体情况取决于ribbon的connectTimeout配置的超时时间和其他情况为准
hystrix:
command:
default:
execution:
timeout:
enabled: true #为true时,需两个方法配置超时设置,分别是Ribbon的ReadTimeout和hystrix的timeoutInMilliseconds,以值小的为准
isolation:
thread:
timeoutInMilliseconds: 5000
ribbon:
ReadTimeout: 5000
ConnectTimeout: 5000
#feign整合hystrix配置
#SpringCloud Dalston版本默认关闭,需配置打开,不然feign调用不能熔断降级
feign:
hystrix:
enabled: true
#hystrix-dashboard配置:
#hystrix-dashboard需要暴露endpoints,默认只能访问health和info,所以hystrix-dashboard需要配置*或者hystrix.stream;设置*报错
management:
endpoints:
web:
exposure:
include: hystrix.stream
2.3 测试
- 注意事项
先访问portal-server的接口,如果先访问hystrix.stream接口,会一直ping
- 访问portal-server的feign调用接口
- 访问http://localhost:9300/hystrix,并输入输入http://localhost:9100/actuator/hystrix.stream;
- 进入到hystrix的页面
三、整合turbine,监控多个hystrix服务
3.1 搭建hystrix-turbine服务
3.1.1 引入依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-study</artifactId>
<groupId>com.wj</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>hystrix-turbine</artifactId>
<description>hystrix-turbine聚合项目,监控多个微服务</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
</dependencies>
</project>
3.1.2 启动类开启turbine
package com.wj.hystrix.turbine;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
/**
* turbine项目,监控多个微服务
* @author wangjian
*/
@EnableTurbine//开启turbine
@SpringBootApplication
public class HystrixTurbineApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixTurbineApplication.class,args);
}
}
3.1.3 配置文件配置
server:
port: 9301
#注册中心相关配置
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://127.0.0.1:8761/eureka/,http://localhost:8762/eureka/
#turbine配置
#app-config表示对哪些hystrix服务进行聚合汇总
turbine:
app-config: PORTAL-SERVER,MANAGER-SERVER
#集群名称配置
cluster-name-expression: "'default'"
aggregator:
cluster-config: default
3.2 manager-server服务搭建(测试turbine)
3.2.1 引用依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-study</artifactId>
<groupId>com.wj</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<description>后台管理服务</description>
<artifactId>manager-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.wj</groupId>
<artifactId>cloud-common-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--引入hystrix依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!--springboot服务监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
3.2.2 配置文件配置
# 端口号配置
server:
port: 9400
# 应用名称配置
spring:
application:
name: manager-server
#eureka客户端配置
eureka:
instance:
# 向注册中心发送心跳间隔时间,告诉注册中心自己还活着;默认时间是30s
lease-renewal-interval-in-seconds: 10
# 如果30秒内没有向注册中心发送心跳,代表发生故障,从注册中心移除掉,默认时间是90s
lease-expiration-duration-in-seconds: 30
#告诉服务端,服务实例以ip作为连接,而不是机器名,默认是false
prefer-ip-address: true
#告诉服务端,服务实例的名称
instance-id: ${spring.application.name}
client:
#注册中心连接地址
service-url:
defaultZone: http://127.0.0.1:8761/eureka/,http://localhost:8762/eureka/
#hystrix配置
#hystrix的超时时间可在配置文件或注解配置
#hystrix.command.default.execution.timeout.enabled说明:
#为true时,需两个方法配置超时设置,分别是Ribbon的ReadTimeout和hystrix的timeoutInMilliseconds,以值小的为准
#为false时,hystrix不进行超时熔断,根据Ribbon的ReadTimeout抛出的异常进行熔断,具体情况取决于ribbon的connectTimeout配置的超时时间和其他情况为准
hystrix:
command:
default:
execution:
timeout:
enabled: true #为true时,需两个方法配置超时设置,分别是Ribbon的ReadTimeout和hystrix的timeoutInMilliseconds,以值小的为准
isolation:
thread:
timeoutInMilliseconds: 5000
ribbon:
ReadTimeout: 5000
ConnectTimeout: 5000
#feign整合hystrix配置
#SpringCloud Dalston版本默认关闭,需配置打开,不然feign调用不能熔断降级
feign:
hystrix:
enabled: true
#hystrix-dashboard配置:
#hystrix-dashboard需要暴露endpoints,默认只能访问health和info,所以hystrix-dashboard需要配置*或者hystrix.stream;设置*报错
management:
endpoints:
web:
exposure:
include: hystrix.stream
3.2.3启动类
package com.wj.manager.server;
/**
* 后台管理服务
* @author wangjian
*/
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
/**
* @EnableDiscoveryClient和@EnableEurekaClient共同点:让注册中心发现,扫描到该服务。
* 不同点:@EnableEurekaClient只适用于Eureka作为注册中心,@EnableDiscoveryClient 可以是其他注册中心,比如zookeeper。
* @EnableCircuitBreaker和@EnableHystrix的作用是一样的,开启断路器;
* @SpringCloudApplication:此注解的作用相当于@EnableCircuitBreaker、@EnableDiscoveryClient和@SpringBootApplication
* @author wangjian
*/
//开启eureka客户端
@EnableEurekaClient
//@EnableDiscoveryClient
@EnableFeignClients("com.wj.common.api.feign")//开启feign,并配置扫描路径
@SpringBootApplication
@EnableCircuitBreaker//启用hystrix
//@EnableHystrix
//@SpringCloudApplication
@ComponentScan(value = {"com.wj.manager.server.*","com.wj.common.api.*"})
public class ManagerServerApplication {
public static void main(String[] args) {
SpringApplication.run(ManagerServerApplication.class,args);
}
}
3.2.4 配置类
package com.wj.manager.server.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* 门户配置类
* @author wangjian
*/
@Configuration
public class ManagerConfig {
/**
* 注入RestTemplate
* @LoadBalanced:Ribbon实现负载均衡
* @return
*/
@LoadBalanced
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
3.2.5 controller
package com.wj.manager.server.controller;
import com.wj.common.api.base.Result;
import com.wj.common.api.entity.Goods;
import com.wj.common.api.feign.GoodsFeignHystrixRemoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* feign整合hystrix
* @author wangjian
*/
@RestController
@RequestMapping("/feignHystrix")
public class FeignHystrixController {
@Autowired
private GoodsFeignHystrixRemoteService goodsFeignHystrixRemoteService;
@GetMapping("/goods/hystrix/list")
public Result<List<Goods>> feignList(){
//feign调用
Result<List<Goods>> result = goodsFeignHystrixRemoteService.feignHystrixList();
return result;
}
@GetMapping("/goods/info")
public Result<Goods> info(){
//feign调用
Result<Goods> result = goodsFeignHystrixRemoteService.feignHystrixInfo();
return result;
}
}
3.3 测试
- 访问portal-server服务的http://localhost:9100/feignHystrix/goods/hystrix/list接口
- 访问manager-server服务的 http://localhost:9400/feignHystrix/goods/info接口
- 访问hystrix主页: http://localhost:9300/hystrix,并输入turbine的监控地址:http://localhost:9301/turbine.stream
- 页面结果
同时监控两个hystrix服务