springcloud(二) 熔断器Hystrix,Hystrix dashboard(熔断器仪盘表),Turbine(集群监控)

(一)熔断器Hystrix

   参考:Hystrix

   由于某些原因,公开服务会引发异常。在这种情况下使用Hystrix 我们定义了一个回退方法。如果在公开服务中发生异常,则回退方法返回一些默认值(其实就是服务降级fallback)异常处理之后没有异常了在走正常逻辑。

   代码实现其实不难,我也是跟随者大神的脚步一步步前行,在此表示感谢!

   首先大神已经说得很明白了,加熔断器相应代码是在consumer模块里。我是在我上一篇博客基础上加的(Eureak集群,负载均衡,健康机制(生产者里面加))。

首先:

@Component
public class HelloRemoteHystrix implements HelloRemoteService{
    @Override
    public String hello(@RequestParam(value = "name") String name) {
        return "hello" +name+", this messge send failed,please check your system!!!";
    }
}

随后在HelloRemoteService 接口注解上加:
fallback = HelloRemoteHystrix.class

@FeignClient(name = "production", path = "/home",fallback = HelloRemoteHystrix.class)

最后配置文件上加:

#熔断器(只作用于服务调用端也就是消费端)
feign.hystrix.enabled=true

由于我健康检查时间设置较短和次数较少所以当跑完项目就Down了,所有加入熔断器后刚好模拟production故障,直接请求consumer http://localhost:9001/hello/lj 直接返回熔断器HelloRemoteHystrix 中的自定义错误提示语。不加熔断器则页面直接无法请求报错!!!!
在这里插入图片描述
(二)Hystrix dashboard(熔断器仪盘表)

   Spring Cloud Hystrix Dashboard只是spring cloud基于Hystrix Dashboard,将实时监控数据通过页面呈现出来。Spring Cloud Hystrix Dashboard的底层原理是间隔一定时间去“Ping”目标服务,返回的结果是最新的监控数据,最后将数据显示出来。
   上文的目标服务,必须具备两个条件。第一,服务本身有对Hystrix做监控统计(spring-cloud-starter-hystrix启动依赖);第二,暴露hystrix.stream端口(spring-boot-starter-actuator启动依赖)。

操作我也是在我那个Eureka集群大项目上的consumer加的代码

首先导入依赖:

<dependency>
  <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-hystrix</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-netflix-hystrix-dashboard</artifactId>
 </dependency>

在启动类ConsumerApplication 上加:

@EnableHystrixDashboard  //Hystrix Dashboard

同时在启动类加上:

 //Hystrix Dashboard
    @Bean
    public ServletRegistrationBean getServlet(){
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean regBean = new ServletRegistrationBean(streamServlet);
        regBean.setLoadOnStartup(1);
        List mappingList = new ArrayList();
        mappingList.add("/hystrix.stream");
        regBean.setUrlMappings(mappingList);
        regBean.setName("hystrixServlet");
        return regBean;
    }

最后访问 http://localhost:9001/hystrix:
在这里插入图片描述
上图中出现3个输入框。作用如下:

  • 目标服务暴露的hystrix.stream端口
  • “Ping”的间隔时间 2000ms 即2秒
  • 目标服务的别名,可以随便取

输入:localhost:9001/hystrix.stream 。最后还有一个“Monitor Stream”按钮,在3个输入框输入无误后,点击该按钮,开始“监控”目标服务。

可能出现的问题:启动 Hystrix Dashboard 报错
请参考:启动 Hystrix Dashboard 报错
   解决后启动 hystrix,刚开始访问会一直 Load …. 也一直在ping,此时需要刷新http://localhost:9001/hello/lj 随后在点击输入localhost:9001/hystrix.stream后的Monitor Stream

输出如图:
在这里插入图片描述
(三)Turbine(集群监控)

    对于查看单个Eureka实例的健康情况是没有多大用处的,如果查看单个实例的健康情况,可以直接通过Hystrix提供的hystrix.stream就可以实现(把对应的URL地址放入Hystrix Dashboard中查看状态),对于一个系统的所有集群的健康状态,是我们了解系统健康状态的最宏观也是最有用的方式,Turbine就是聚合系统的所有集群的健康状态,就是把多个/hystrix.stream,全部聚合在/turbine.stream中,通过Hystrix Dashboard中可视化查看。

首先导入依赖:

<dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-turbine</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-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-dashboard</artifactId>
	</dependency>
</dependencies>

项目:hystrix-dashboard-turbine

DashboardApplication

package com.neo;
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 //标明Turbine
public class DashboardApplication {
	public static void main(String[] args) {
		SpringApplication.run(DashboardApplication.class, args);
	}
}
spring.application.name=hystrix-dashboard-turbine
server.port=8001
//这是两个消费者的spring-application-name
turbine.appConfig=node01,node02   
turbine.aggregator.clusterConfig= default
turbine.clusterNameExpression= new String("default")
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

(1)turbine.appConfig :配置Eureka中的serviceId列表,表明监控哪些服务。

(2)turbine.aggregator.clusterConfig :指定聚合哪些集群,多个使用”,”分割,默认为default。可使用http://…/turbine.stream?cluster={clusterConfig之一}访问。

(3)turbine.clusterNameExpression :

  1. clusterNameExpression指定集群名称,默认表达式appName;此时:turbine.aggregator.clusterConfig需要配置想要监控的应用名称;
  2. 当clusterNameExpression: default时,turbine.aggregator.clusterConfig可以不写,因为默认就是default;
  3. 当clusterNameExpression: metadata[‘cluster’]时,假设想要监控的应用配置了eureka.instance.metadata-map.cluster: ABC,则需要配置,同时turbine.aggregator.clusterConfig: ABC

项目:spring-cloud-consumer-node1

Consumer1Application

package com.neo;
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.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableCircuitBreaker
public class Consumer1Application {
	public static void main(String[] args) {
		SpringApplication.run(Consumer1Application.class, args);
	}
}

ConsumerController

@RestController
public class ConsumerController {

    @Autowired
    HelloRemote HelloRemote;
	
    @RequestMapping("/hello/{name}")
    public String index(@PathVariable("name") String name) {
        return HelloRemote.hello(name);
    }
}

HelloRemote (其实我这里没有写spring-cloud-producer 生产者)

@FeignClient(name= "spring-cloud-producer", fallback = HelloRemoteHystrix.class)
public interface HelloRemote {
    @RequestMapping(value = "/hello")
    public String hello(@RequestParam(value = "name") String name);
}

HelloRemoteHystrix

package com.neo.remote;
import com.neo.remote.HelloRemote;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Created by summer on 2017/5/15.
 */
@Component
public class HelloRemoteHystrix implements HelloRemote{
    @Override
    public String hello(@RequestParam(value = "name") String name) {
        return "hello " +name+", this messge send failed ";
    }
}

spring.application.name=node01
server.port=9001
feign.hystrix.enabled=true
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

项目:spring-cloud-consumer-node2 和spring-cloud-consumer-node1类似在此就不写了。具体可以看gitee上的代码。

项目:spring-cloud-eureka

SpringCloudEurekaApplication

package com.neo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class SpringCloudEurekaApplication {
	public static void main(String[] args) {
		SpringApplication.run(SpringCloudEurekaApplication.class, args);
	}
}
spring.application.name=spring-cloud-eureka
server.port=8000
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

先请求几个服务:例如:
http://localhost:9002/hello/lj http://localhost:9001/hello/lj

在访问http://localhost:8001/turbine.stream
在这里插入图片描述
最后访问 http://localhost:8001/hystrix
在这里插入图片描述
在这里插入图片描述
这个和http://localhost:8001/turbine.stream 图中展示接口对应上了!!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值