目录
本篇文章涉及到前面文章的工程,mirco-service-provider、mirco-service-consumer以及需要另外新建一个工程mirco-service-turbine-hystrix-dashbord。
为“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>
这里有点东西需要跟小伙伴们说明,因为上一篇文章springcloud实战之初级入门(三)— spring cloud config搭建git配置中心中,我们将服务提供的工程配置文件上传到了github上,那么我们需要修改github上provider.yml,这是第一种方法。第二种方法是直接在bootstrap.yml中加入我们想要的配置,也是一样的生效,如果不明白我这里写了什么,可以看我的视频。我们这里介绍第一种方法:
在启动文件中加上"@enablehystrixdashboard 、@enablecircuitbreaker"注解,然后启动工程。
打开浏览器访问http://www.lhsxpumps.com/_localhost:7001/hystrix,可以看到如下图内容,说明服务调用的监控就搭建完成了。
我在网络上看了一下,很多教程让你在输入框中输入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://www.lhsxpumps.com/_localhost:8002/consumerhelloworld?name=rose,然后再返回看一下监控页面,如果页面没有自动刷新,就手动刷一下页面,你就会看到效果了。这里面的英文是什么意思,请自行搜索,效果如下:
经过上面的步骤,我们大致明白了一些问题:
其实如果你在spring全家桶里面做集成,做这个熔断非常简单,接下来我们写myfristconsumerhystrix这个类,如下:
@component public class myfristconsumerhystrix implements myfristconsumer{ @override public string helloworld(string name) { return "server is closed , your send failed"; } }
查看监控,你就会发现图标变红了。
服务熔断的目的是为了避免微服务环境下,应用因为网络抖动或者服务异常的解决方案。如果应用长时间的服务异常,并且服务与服务之间依赖过多,就会引起“级联”效果,造成服务雪崩。
创建一个名称"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上找到需要监控的服务就可以了。
在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/
@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; } }
如对本文有疑问, 点击进行留言回复!!
[杭电多校2020]第一场 1004 Distinct Sub-palindromes
Swift -- 将本地生成的UIImage进行持久化保存(存到文件中fileManager.createFile)
网友评论