SpringCloud之Hystrix-Dashboard监控,以及踩的…

2019-08-26 06:41:07来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

SpringCloud之Hystrix-Dashboard监控,以及踩的坑...

前言:

最近刚入职,公司使用了SpringCloud,之前有了解过SpringCloud,但是长时间不去搭建不去使用很容易就忘了,因此空闲时间重新复习一下SpringCloud。但是之前开的SpringCloud的版本可能有点低,公司现在用的 " Greenwich.RELEASE "的版本,SpringBoot使用了“ 2.1.x ”的版本,算是比较新了,因此在使用 Hystrix-Dashboard 的时候会有点坑,因此想把踩到的坑记录下来,让更多的人避开这个坑,废话不多说,开始了!

一、创建 Hystrix-Dashboard 监控服务:springcloud-hystrix-dashboard

  1. 工程pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.gxc.test</groupId>
    <artifactId>springcloud-hystrix-dashboard</artifactId>
    <version>1.0.0</version>

    <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>
</project>
  1. yml配置文件
server:
  port: 10000
spring:
  application:
    name: springcloud-hystrix-dashboard
  1. 在启动类上添加注解 @EnableHystrixDashboard
@EnableHystrixDashboard
@SpringBootApplication
public class HystrixDashboardApplication {
    public static void main(String[] args) {
        SpringApplication.run(HystrixDashboardApplication.class, args);
    }
}
  1. 启动!浏览器访问地址:http://localhost:10000/hystrix ,如下图:
    image.png

二、创建Eureka注册中心:springcloud-eureka-server

有读者可能会问:不是说玩 Hystrix-Dashboard 吗?为啥还要用到Eureka?
答:请耐心往下看,这也是我为什么题目说的坑...

  1. 工程pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.gxc.test</groupId>
    <artifactId>springcloud-hystrix-dashboard</artifactId>
    <version>1.0.0</version>

    <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>
</project>
  1. 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
  1. 启动类添加注解:@EnableEurekaServer
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}
  1. 启动!浏览器访问:http://localhost:9000/ ,如下图:
    image.png

三、创建被监控服务:springcloud-user

  1. 工程pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.gxc.test</groupId>
    <artifactId>springcloud-user</artifactId>
    <version>1.0.0</version>
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <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>
</project>
  1. 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
  1. 启动类添加注解
//@EnableCircuitBreaker
//@EnableEurekaClient
//@EnableFeignClients
//@SpringBootApplication

// 以上四个注解可以用如下两个注解取代
@EnableFeignClients
@SpringCloudApplication
public class UserApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class, args);
    }
}
  1. 创建测试Controller类
@RestController
public class UserController {
    @GetMapping("/user")
    public Object getUser() {
        Map<String, Object> user = Maps.newHashMap();
        user.put("username","GongXincheng");
        user.put("age", 23);
        user.put("birthday", new Date());
        return user;
    }
}
  1. 启动!浏览器访问:http://localhost:10001/user,如下图:
    image.png

四、测试 Hystrix-Dashboard 监控 user 服务

  1. 浏览器访问:http://localhost:10001/hystrix.stream ,出现了404错误
    说明:第一个坑,在SpringBoot 2.0之前,只要添加了Actuator依赖,不会出现404错误的
    image.png
  2. 解决404问题:创建一个配置类,并注册一个Servlet
@Configuration
public class ServletConfigBean {
    @Bean
    public ServletRegistrationBean hystrixMetricsStreamServlet() {
        ServletRegistrationBean regist = new ServletRegistrationBean();
        regist.setServlet(new HystrixMetricsStreamServlet());
        regist.setName("hystrixMetricsStreamServlet");
        regist.setLoadOnStartup(1);
        regist.addUrlMappings("/hystrix.stream");
        return regist;
    }
}
  1. 浏览器访问:http://localhost:10001/hystrix.stream ,如下图:
    image.png
  2. 将第一步的链接:http://localhost:10001/hystrix.stream ,复制到 Hystrix-Dashboard页面中
    image.png

  3. Hystrix-Dashboard会一直在 loading... 中,这就是第二个坑...
    image.png

  4. 解决办法:
1:创建另一个服务(例如:springcloud-order)
2:将新的服务(order)注册到Eureka
3:在user服务写一个新的Api接口,通过 Feign 访问新服务(order)的Api接口
4:注意:经过本人测试通过RestTemplate的方式调用,仍然没有效果。
5:当通过Feign调用一次新服务(order)后,hystrix.stream 正常,效果如下:

image.png
image.png

  1. Controller层代码
@RestController
public class UserController {

    private static final String ORDER_SERVER_URL = "http://springcloud-order";

    @Resource private OrderFeign orderFeign;
    @Resource private RestTemplate restTemplate;

    @GetMapping("/user")
    public Object getUser() {
        Map<String, Object> user = Maps.newHashMap();
        user.put("username","GongXincheng");
        user.put("age", 23);
        user.put("birthday", new Date());
        return user;
    }

    @GetMapping("/feign/order")
    public Object feignGetOrder() {
        return orderFeign.getOrder();
    }

    @GetMapping("/rest/order")
    public Object restGetOrder() {
        return restTemplate.getForObject(ORDER_SERVER_URL + "/order", Object.class);
    }
}

8:user服务和oder服务目录结构 和 相关代码
image.png

五:代码链接

https://gitee.com/gxc6/gxc-springcloud-study

如果有什么错误的地方,欢迎大家帮忙指正!感谢!

我透... 写完这个已经凌晨3点了... 溜了溜了...


原文链接:https://www.cnblogs.com/gxc6/p/11407601.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Spring源码剖析开篇:什么是Spring?

下一篇:服务链路跟踪 &amp;&amp; 服务监控