• Spring Cloud实战之初级入门(四)— 利用Hystrix实现服务熔断与服务监控


    1.环境介绍

    本篇文章涉及到前面文章的工程,mirco-service-providermirco-service-consumer以及需要另外新建一个工程mirco-service-turbine-hystrix-dashbord

    2.服务监控

    2.1 加入依赖

    为“mirco-service-provider”工程的pom文件中加入

    		<dependency>
    			<groupId>org.springframework.cloud</groupId>
    			<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    		</dependency>
    		
    		<dependency>
    			<groupId>org.springframework.cloud</groupId>
    			<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
    		</dependency>
    

    2.2 修改配置文件

    这里有点东西需要跟小伙伴们说明,因为上一篇文章SpringCloud实战之初级入门(三)— spring cloud config搭建git配置中心中,我们将服务提供的工程配置文件上传到了github上,那么我们需要修改github上provider.yml,这是第一种方法。第二种方法是直接在bootstrap.yml中加入我们想要的配置,也是一样的生效,如果不明白我这里写了什么,可以看我的视频。我们这里介绍第一种方法:

    1. 在工程目录打开命令行
      在这里插入图片描述

    2. 使用vi provider.yml编辑配置文件

    3. 保存配置文件

    4. 使用"git status"查看我们工程中哪些文件进行了修改

    5. 使用"git add provider-test.yml"加入本地仓库的提交列表

    6. 使用"git commit -m '提交说明'"将要提交的文件提交到本地仓库

    7. 使用"git push origin master"已经提交到本地仓库的文件上传至远程仓库。

    2.3 修改启动文件

    1. 在启动文件中加上"@EnableHystrixDashboard 、@EnableCircuitBreaker"注解,然后启动工程。

    2. 打开浏览器访问http://localhost:7001/hystrix,可以看到如下图内容,说明服务调用的监控就搭建完成了。

    2.4 监控服务

    我在网络上看了一下,很多教程让你在输入框中输入http://localhost:8001/hystrix.stream然后点击下面的"monitor stream" 按钮基本上就完了,但是在我的环境里面是不行的,可能是spring cloud和spring boot的版本不一样。下面用步骤讲一下怎么做监控:

    • 在浏览器的地址栏中输入http://localhost:8001/hystrix.stream是无法访问的,因为我们没有将“/hystrix.stream”这目录加入工程的访问目录。

    • 修改启动文件如下:

    @EnableHystrixDashboard
    @EnableCircuitBreaker
    @EnableDiscoveryClient
    @SpringBootApplication
    public class MircoServiceProviderApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(MircoServiceProviderApplication.class, args);
    	}
    
    	@Bean
    	public ServletRegistrationBean<HystrixMetricsStreamServlet> getServlet() {
    		HystrixMetricsStreamServlet hystrixMetricsStreamServlet = new HystrixMetricsStreamServlet() ;
    		ServletRegistrationBean<HystrixMetricsStreamServlet> servletRegistrationBean = new ServletRegistrationBean<HystrixMetricsStreamServlet>() ;
    		servletRegistrationBean.setServlet(hystrixMetricsStreamServlet);
    		servletRegistrationBean.setLoadOnStartup(10);
    		servletRegistrationBean.addUrlMappings("/hystrix.stream");
    		servletRegistrationBean.setName("HystrixMetricsStreamServlet");
    		return servletRegistrationBean ;
    	}
    }
    
    
    • 重复第一个步骤,你会看到有一个ping一直在ping,其实就是在ping我们工程中hystrix默认给我们埋下的监控点,这也是为什么我们要用spring全家桶的原因之一,在其他的框架中像这样的事情,是需要我们自己去设计,开发的。而spring直接帮我们集成进来了,配置、配置就可以用了。效果如下:

    • 我们再次访问http://localhost:8001/hystrix,监控http://localhost:8001/hystrix.stream这个地址,你会发现如图下界面,但是很不幸的告诉你,我们仍然看不到效果,为什么?卖个关子,请接着往下看。

    • 请使用同样的方法将"mrico-service-consumer"工程改造,改造完后,我们访问http://localhost:8002/hystrix,输入http://localhost:8002/hystrix.stream这个地址进行监控,你会发现奇迹,但是一样看不到东西,这时候我们访问一下消费服务的接口http://localhost:8002/consumerHelloWorld?name=rose,然后再返回看一下监控页面,如果页面没有自动刷新,就手动刷一下页面,你就会看到效果了。这里面的英文是什么意思,请自行搜索,效果如下:

    2.5 小结

    经过上面的步骤,我们大致明白了一些问题:

    1. 服务的整个监控是由spring cloud帮我们集成的,而且不需要我们写任何代码。
    2. 对于服务监控,监控的是调用方,而不是服务提供方,我个人认为这也是一种思想,不用过度设计,比如你服务方和消费方都去监控。我相信在spring cloud没有出来之前应该有人这么做,但我尝试这种思想后过后觉得意义并不大。那么刚才我们讲的监控埋点,也是埋在了消费方的接口。对此有兴趣的小伙伴如果想知道更详细的内容,可以去百度一下。
    3. 为什么我第二个视频SpringCloud实战之初级入门(二)— 服务注册与服务调用会讲到说,在真实环境中像这种一个提供服务,一个提供调用的单向调用基本不存在,今天两个工程都加入了服务监控,大家也都看到了,第一个工程根本看不到任何东西,因为它没有调用其他的服务。
    4. 为什么先讲服务的监控后讲熔断?因为你只有明白了监控是怎么一过程,你才能更好的了解熔断,因为熔断也是在消费方实现的。

    3. 利用hystrix实现消费服务熔断

    3.1 加入服务熔断

    其实如果你在spring全家桶里面做集成,做这个熔断非常简单,接下来我们写MyFristConsumerHystrix这个类,如下:

    @Component
    public class MyFristConsumerHystrix implements MyFristConsumer{
    	@Override
    	public String helloWorld(String name) {
    		return "server is closed , your send failed";
    	}
    }
    

    3.2 测试服务熔断

    1. 关掉服务提供工程

    2. 访问调用接口,你会看如下结果:

    3. 查看监控,你就会发现图标变红了。

    3.3 小结

    1. 关于服务熔断,在真实使用中也不是说所有的服务都需要熔断,这个要根据实际情况来。
    2. 服务熔断的目的是为了避免微服务环境下,应用因为网络抖动或者服务异常的解决方案。如果应用长时间的服务异常,并且服务与服务之间依赖过多,就会引起“级联”效果,造成服务雪崩。

    4. 利用turbine监控所有应用

    4.1 创建工程

    创建一个名称"mirco-service-turbine-hystrix-dashboard"的工程,在pom文件中要有以下依赖:

    		<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-dashboard</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-netflix-turbine</artifactId>
    		</dependency>
    

    因为我们的服务都已经注册到了eureka上,所以只需要连上eureka,从eureka上找到需要监控的服务就可以了。

    4.2 修改配置文件

    在application.yml文件中另入以下配置

    spring.application.name: turbine-hystrix-dashboard
    server.port: 6001
    
    turbine:
      appConfig: service-provider,service-consumer
      clusterNameExpression: new String("default")
      instanceUrlSuffix: hystrix.stream
      aggregator:
        clusterConfig: default
    
    eureka:
      client: 
        serviceUrl:
          defaultZone: http://localhost:9001/eureka/
    

    4.3 修改启动文件

    @EnableDiscoveryClient
    @EnableTurbine
    @EnableHystrixDashboard
    @SpringBootApplication
    public class MircoServiceTurbineHystrixDashboardApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(MircoServiceTurbineHystrixDashboardApplication.class, args);
    	}
    
        @Bean
        public ServletRegistrationBean<TurbineStreamServlet> getServlet() {
        	TurbineStreamServlet streamServlet = new TurbineStreamServlet();
            ServletRegistrationBean<TurbineStreamServlet> registrationBean = new ServletRegistrationBean<TurbineStreamServlet>(streamServlet);
            registrationBean.setLoadOnStartup(1);
            registrationBean.addUrlMappings("/turbine.stream");
            registrationBean.setName("TurbineStreamServlet");
            return registrationBean;
        }     
    }
    

    4.4 启动

    1. 依次启动“mirco-service-config”、“mirco-service-eureka”、"mirco-service-provider"、"mirco-service-consumer"、“mirco-service-turbine-hystrix-dashboard”这五个工程
    2. 访问http://localhost:8002/consumerHelloWorld?name=rose
    3. 访问http://localhost:6001/hystrix,监控http://localhost:6001/turbine.stream,就可以看到刚才在"mirco-service-consumer"监控里面看到的内容了,如果你想看到多个服务,那么你写多几个服务,只要被调用过都可以看到,亲测可用。

    5.一点点重要的事情

  • 相关阅读:
    git 记录
    js 技巧
    首页三张幻灯片放什么
    6系统盈利方向
    WordPress获取某个分类关联的标签
    目标型长尾如何优化
    简介如何去除WordPress主题版权保护的方法
    2017.7.7 长尾关键词系统优化
    2017.7.6 目标关键词分析
    2017.7.2 seo知识总纲
  • 原文地址:https://www.cnblogs.com/yb2020/p/10264331.html
Copyright © 2020-2023  润新知