05.SpringCloud之服务监控

一.简介

微服务运行过程中常需要对微服务进行监控,Hystrix提供了服务监控功能。

二.HystrixDashboard 监控

1.微服务端引入hystrix 和 actuator

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

2.对需要监控的方法增加@HystrixCommand注解

package com.vincent.controller;


import com.alibaba.fastjson.JSONObject;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
public class UserController {

    @GetMapping("/detail")
    @HystrixCommand
    public Object detail(Integer id, HttpServletRequest request){
        JSONObject rst = new JSONObject();
        rst.put("date",new Date());
        rst.put("name",22);
        rst.put("port",request.getLocalPort());
        return rst;
    }
}

3.微服务启动类增加@EnableHystrix注解

4.新建监控类微服务模块hystrix-dashboard

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

5.修改hystrix-dashboard模块配置

server:
  port: 9901

6.编写hystrix-dashboard模块启动类

package com.vincent;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

@SpringBootApplication
@EnableHystrixDashboard
public class DashboardApp {
    public static void main(String[] args) {
        SpringApplication.run(DashboardApp.class,args);
    }
}

7.访问http://localhost:9901/hystrix
在这里插入图片描述
在页面中输入跟踪监控的微服务:http://localhost:8001/service-user/hystrix.stream

在这里插入图片描述
8.访问消费端以调用微服务后监控界面效果
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Zllvincent/article/details/108431876