SpringCloud通过hystrix进行熔断以及HystrixDashboard看监控图小记
提取码:qlu9
1、添加服务hystrix-feign-one。
2、pom中引入相关包以及启动类中添加注解。
<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>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--想要用spring-cloud-netflix-hystrix-dashboard
但是@enablehystrixdashboard引入不进去
属于springBot版本问题,使用以下包,begin-->
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-hystrix-dashboard</artifactId>
<!-- 默认使用的版本是 2.2.2.RELEASE-->
<version>2.2.2.RELEASE</version>
</dependency>
<!--@enablehystrixdashboard包引入End-->
package com.example.hystrixfeignone;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableCircuitBreaker
public class HystrixFeignOneApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixFeignOneApplication.class, args);
}
}
3、配置文件中添加相关基础配置
server.port=9001
spring.application.name=hystrix-feign-one
#是否以ip地址进行注册
eureka.instance.prefer-ip-address=true
#是否注册
eureka.client.register-with-eureka=true
#是否需要从eureka上获取信息
eureka.client.fetch-registry=true
#注册地址
eureka.client.service-url.defaultZone=http://127.0.0.1:8081/eureka/
#心跳检测与续约时间(本地开发时将时间设置小些,保证服务关闭后注册中心及时剔除服务)
#eureka客户端向服务端发送心跳的时间间隔,单位为秒
eureka.instance.lease-renewal-interval-in-seconds=1
#eureka服务端在收到最后一次心跳之后等待的时间上限,单位为秒,超过则剔除
eureka.instance.lease-expiration-duration-in-seconds=2
#熔断器默认超时时间 单位秒 全局配置
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=10000
#开启feign熔断
feign.hystrix.enabled=true
#请求连接的超时时间
feign.client.config.default.connectTimeout=20000
#请求处理的超时时间
feign.client.config.default.readTimeout=20000
#feign调用使用OKhttp
feign.okhttp.enabled=true
#暴露端点
management.endpoints.web.exposure.include=*
4、添加接口以及熔断机制,“fallback = ServiceUserFeignFullBack.class”代表出现熔断请求“ServiceUserFeignFullBack”接口中的对应方法。
package com.example.hystrixfeignone.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(value = "service-user", fallback = ServiceUserFeignFullBack.class)
public interface ServiceUserFeign {
@GetMapping(value = "/getUserName")
String getUserName();
}
5、添加熔断触发后请求方法。
package com.example.hystrixfeignone.feign;
import org.springframework.stereotype.Service;
@Service
public class ServiceUserFeignFullBack implements ServiceUserFeign {
@Override
public String getUserName() {
return "Feign 请求超时";
}
}
6、增加ServiceFeignController类请求接口。
package com.example.hystrixfeignone.web;
import com.example.hystrixfeignone.feign.ServiceUserFeign;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class ServiceFeignController {
@Resource
ServiceUserFeign serviceUserFeign;
@GetMapping(value = "getUserName")
public String getUserName() {
String userName = serviceUserFeign.getUserName();
return "Feign-One: " + userName;
}
}
7、添加服务hystrix-feign-two,同添加服务hystrix-feign-one调整端口跟名称即可。
8、添加服务hystrix-turbine。
9、引入相关包。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-turbine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<!--想要用spring-cloud-netflix-hystrix-dashboard
但是@enablehystrixdashboard引入不进去
属于springBot版本问题,使用以下包,begin-->
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-hystrix-dashboard</artifactId>
<!-- 默认使用的版本是 2.2.2.RELEASE-->
<version>2.2.2.RELEASE</version>
</dependency>
<!--@enablehystrixdashboard包引入End-->
10、添加基本配置。
#是否以ip地址进行注册
eureka.instance.prefer-ip-address=true
#是否注册
eureka.client.register-with-eureka=true
#是否需要从eureka上获取信息
eureka.client.fetch-registry=true
#注册地址
eureka.client.service-url.defaultZone=http://127.0.0.1:8081/eureka/
server.port=9003
spring.application.name=hystrix-turbine
turbine.appConfig=hystrix-feign-one,hystrix-feign-two
turbine.aggregator.clusterConfig= default
turbine.clusterNameExpression= new String("default")
11、启动类中添加注解。
package com.example.hystrixturbine;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
@SpringBootApplication
@EnableHystrixDashboard
@EnableTurbine
public class HystrixTurbineApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixTurbineApplication.class, args);
}
}
12、启动相关服务,访问hystrix-turbine的地址“http://127.0.0.1:9003/hystrix”打开小熊监控的首页,输入“http://127.0.0.1:9003/turbine.stream”点击“Monitor Stream”即可看到配置的需要监控的所有服务。