SpringCloud之Hystrix-Dashboard监控,以及踩的坑...
本文内容纲要:
-前言:
-一、创建Hystrix-Dashboard监控服务:springcloud-hystrix-dashboard
-二、创建Eureka注册中心:springcloud-eureka-server
-三、创建被监控服务:springcloud-user
-四、测试Hystrix-Dashboard监控user服务
-五:代码链接
前言:
最近刚入职,公司使用了SpringCloud,之前有了解过SpringCloud,但是长时间不去搭建不去使用很容易就忘了,因此空闲时间重新复习一下SpringCloud。但是之前开的SpringCloud的版本可能有点低,公司现在用的"Greenwich.RELEASE"的版本,SpringBoot使用了“2.1.x”的版本,算是比较新了,因此在使用Hystrix-Dashboard的时候会有点坑,因此想把踩到的坑记录下来,让更多的人避开这个坑,废话不多说,开始了!
一、创建Hystrix-Dashboard监控服务:springcloud-hystrix-dashboard
-
工程pom依赖
4.0.0 org.springframework.boot spring-boot-starter-parent2.1.7.RELEASE com.gxc.test springcloud-hystrix-dashboard 1.0.0 <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
-
yml配置文件
server: port:10000 spring: application: name:springcloud-hystrix-dashboard
-
在启动类上添加注解@EnableHystrixDashboard
@EnableHystrixDashboard @SpringBootApplication publicclassHystrixDashboardApplication{ publicstaticvoidmain(String[]args){ SpringApplication.run(HystrixDashboardApplication.class,args); } }
-
启动!浏览器访问地址:
http://localhost:10000/hystrix,如下图:
二、创建Eureka注册中心:springcloud-eureka-server
有读者可能会问:不是说玩Hystrix-Dashboard吗?为啥还要用到Eureka?
答:请耐心往下看,这也是我为什么题目说的坑...
-
工程pom依赖
4.0.0 org.springframework.boot spring-boot-starter-parent2.1.7.RELEASE com.gxc.test springcloud-hystrix-dashboard 1.0.0 <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
-
yml配置文件
server: port:9000
spring: application: name:springcloud-eureka-server
eureka: client: register-with-eureka:false fetch-registry:false service-url: defaultZone:http://127.0.0.1:${server.port}/eureka
-
启动类添加注解:@EnableEurekaServer
@EnableEurekaServer @SpringBootApplication publicclassEurekaServerApplication{ publicstaticvoidmain(String[]args){ SpringApplication.run(EurekaServerApplication.class,args); } }
-
启动!浏览器访问:
http://localhost:9000/,如下图:
三、创建被监控服务:springcloud-user
-
工程pom依赖
4.0.0 org.springframework.boot spring-boot-starter-parent2.1.7.RELEASE com.gxc.test springcloud-user 1.0.0 <java.version>1.8</java.version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <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> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
-
yml配置文件
server: port:10001
spring: application: name:springcloud-user
将服务注册到Eureka注册中心
eureka: client: service-url: defaultZone:http://127.0.0.1:9000/eureka instance: prefer-ip-address:true instance-id:${spring.application.name}:${server.port}
feign: hystrix: enabled:true
-
启动类添加注解
//@EnableCircuitBreaker //@EnableEurekaClient //@EnableFeignClients //@SpringBootApplication
//以上四个注解可以用如下两个注解取代 @EnableFeignClients @SpringCloudApplication publicclassUserApplication{ publicstaticvoidmain(String[]args){ SpringApplication.run(UserApplication.class,args); } }
-
创建测试Controller类
@RestController publicclassUserController{ @GetMapping("/user") publicObjectgetUser(){ Map<String,Object>user=Maps.newHashMap(); user.put("username","GongXincheng"); user.put("age",23); user.put("birthday",newDate()); returnuser; } }
-
启动!浏览器访问:
http://localhost:10001/user,如下图:
四、测试Hystrix-Dashboard监控user服务
-
浏览器访问:http://localhost:10001/hystrix.stream,出现了404错误
说明:第一个坑,在SpringBoot2.0之前,只要添加了Actuator依赖,不会出现404错误的
-
解决404问题:创建一个配置类,并注册一个Servlet
@Configuration publicclassServletConfigBean{ @Bean publicServletRegistrationBeanhystrixMetricsStreamServlet(){ ServletRegistrationBeanregist=newServletRegistrationBean(); regist.setServlet(newHystrixMetricsStreamServlet()); regist.setName("hystrixMetricsStreamServlet"); regist.setLoadOnStartup(1); regist.addUrlMappings("/hystrix.stream"); returnregist; } }
-
浏览器访问:http://localhost:10001/hystrix.stream,如下图:
-
将第一步的链接:http://localhost:10001/hystrix.stream,复制到Hystrix-Dashboard页面中
-
Hystrix-Dashboard会一直在loading...中,这就是第二个坑...
-
解决办法:
1:创建另一个服务(例如:springcloud-order) 2:将新的服务(order)注册到Eureka 3:在user服务写一个新的Api接口,通过Feign访问新服务(order)的Api接口 4:注意:经过本人测试通过RestTemplate的方式调用,仍然没有效果。 5:当通过Feign调用一次新服务(order)后,hystrix.stream正常,效果如下:
-
Controller层代码
@RestController publicclassUserController{
privatestaticfinalStringORDER_SERVER_URL="http://springcloud-order"; @ResourceprivateOrderFeignorderFeign; @ResourceprivateRestTemplaterestTemplate; @GetMapping("/user") publicObjectgetUser(){ Map<String,Object>user=Maps.newHashMap(); user.put("username","GongXincheng"); user.put("age",23); user.put("birthday",newDate()); returnuser; } @GetMapping("/feign/order") publicObjectfeignGetOrder(){ returnorderFeign.getOrder(); } @GetMapping("/rest/order") publicObjectrestGetOrder(){ returnrestTemplate.getForObject(ORDER_SERVER_URL+"/order",Object.class); }
}
8:user服务和oder服务目录结构和相关代码
五:代码链接
https://gitee.com/gxc6/gxc-springcloud-study
如果有什么错误的地方,欢迎大家帮忙指正!感谢!
我透...写完这个已经凌晨3点了...溜了溜了...
本文内容总结:前言:,一、创建Hystrix-Dashboard监控服务:springcloud-hystrix-dashboard,二、创建Eureka注册中心:springcloud-eureka-server,三、创建被监控服务:springcloud-user,四、测试Hystrix-Dashboard监控user服务,五:代码链接,
原文链接:https://www.cnblogs.com/gxc6/p/11407601.html