原文: https://howtodoinjava.com/spring-cloud/microservices-monitoring/

Spring Boot 和 Spring Cloud 在交付基于微服务的应用程序时被广泛使用。 最终,基于在不同主机上运行的 Spring 运行应用程序监视微服务成为必要。 有许多工具可用来监视这些微服务的各种运行状况。

在此 SpringCloud 教程中,我们将学习使用三种监视工具,即 Hystrix 仪表板Eureka 管理仪表板Spring boot 管理仪表板

1. 概述

在此演示中,我们将创建三个应用程序。

  1. 员工服务 – 此微服务应用程序负责获取员工的数据。
  2. Api 网关 – 此应用程序将在访问不同的微服务时提供公共网关。 在以下示例中,它将充当上述员工服务的网关。
  3. Eureka 服务器 – 此微服务应用程序将提供上述微服务的服务发现和注册。

该演示是围绕 Netflix Eureka 创建的,以集中管理和监视注册的应用程序。 您可能已经知道 Netflix Eureka 服务器是用于构建服务注册表服务器和关联的 Eureka 客户端的,它们将注册自己以查找其他服务并通过 REST API 进行通信。

2. 技术栈

  • Java 1.8
  • Spring 工具套件
  • SpringCloud
  • SpringBoot
  • SpringRest
  • Maven

3. 员工服务

  • Spring boot 初始化器 / Spring 工具套件创建一个 Spring Boot 项目。具有依赖 Eureka Discovery执行器WebREST 存储库
    服务监控 – Hystrix,Eureka 管理员和 Spring Boot 管理员 - 图1

  • 主应用程序类EmployeeServiceApplication,用于启动 Spring Boot 应用程序。
    EmployeeServiceApplication.java ```java package com.howtodoinjava.example.employee; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication @EnableEurekaClient public class EmployeeServiceApplication {

public static void main(String[] args) { SpringApplication.run(EmployeeServiceApplication.class, args); } }

  1. <br />`@EnableEurekaClient` – 此注解在下面创建的[ Eureka Server 应用程序](docs_spring_#eureka-dashboard)中将该服务注册为 Eureka 客户端。
  2. -
  3. 创建一个 Rest 控制器类`EmployeeServiceController`以公开`Employee`数据。
  4. <br />`EmployeeServiceController.java`
  5. ```java
  6. package com.howtodoinjava.example.employee.controller;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. import org.springframework.web.bind.annotation.PathVariable;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.RequestMethod;
  12. import org.springframework.web.bind.annotation.RestController;
  13. import com.howtodoinjava.example.employee.beans.Employee;
  14. @RestController
  15. public class EmployeeServiceController {
  16. private static final Map<Integer, Employee> employeeData = new HashMap<Integer,Employee() {
  17. private static final long serialVersionUID = -3970206781360313502L;
  18. {
  19. put(111,new Employee(111,"Employee1"));
  20. put(222,new Employee(222,"Employee2"));
  21. }
  22. };
  23. @RequestMapping(value = "/findEmployeeDetails/{employeeId}", method = RequestMethod.GET)
  24. public Employee getEmployeeDetails(@PathVariable int employeeId) {
  25. System.out.println("Getting Employee details for " + employeeId);
  26. Employee employee = employeeData.get(employeeId);
  27. if (employee == null) {
  28. employee = new Employee(0, "N/A");
  29. }
  30. return employee;
  31. }
  32. }


关联的Employee Bean 类如下。
Employee.java

  1. package com.howtodoinjava.example.employee.beans;
  2. public class Employee {
  3. private String name;
  4. private int id;
  5. @Override
  6. public String toString() {
  7. return "Employee [name=" + name + ", id=" + id + "]";
  8. }
  9. }
  • src/main/resources目录中创建application.yml
    application.yml ```java server: port: 8011

eureka:
instance: leaseRenewalIntervalInSeconds: 5 leaseExpirationDurationInSeconds: 2 client: serviceUrl: defaultZone: http://localhost:8761/eureka/ healthcheck: enabled: true lease: duration: 5

spring:
application: name: employee-service

management: security: enabled: false

logging: level: com.self.sprintboot.learning.employee: DEBUG

  1. -
  2. 启动此应用,可访问`http://localhost:8011/findEmployeeDetails/111`
  3. <br />![](img/fefc645e57bb19dcc5d7c67b3172880b.jpg#alt=)
  4. <a name="02914a4f"></a>
  5. ## 4. 带有 Hystrix 的 API 网关
  6. -
  7. 从[ Spring boot 初始化器](https://start.spring.io/) / [Spring 工具套件](https://spring.io/tools/sts)创建一个 Spring Boot 项目。具有依赖`Eureka Discovery`, `Actuator`, `Web`, `Hystrix`, `Hystrix Dashboard`, `Rest repositories`。
  8. <br />![](img/80e854887e82fb2cf14c4dfd2353d414.jpg#alt=)
  9. -
  10. 主应用程序类`ApiGatewayApplication`,用于启动 Spring Boot 应用程序。
  11. <br />`ApiGatewayApplication.java`
  12. ```java
  13. package com.howtodoinjava.example.apigateway;
  14. import org.springframework.boot.SpringApplication;
  15. import org.springframework.boot.autoconfigure.SpringBootApplication;
  16. import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
  17. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  18. import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
  19. @SpringBootApplication
  20. @EnableEurekaClient
  21. @EnableHystrixDashboard
  22. @EnableCircuitBreaker
  23. public class ApiGatewayApplication {
  24. public static void main(String[] args) {
  25. SpringApplication.run(ApiGatewayApplication.class, args);
  26. }
  27. }


@EnableHystrixDashBoard – 提供 Hystrix 流的仪表板视图。
@EnableCircuitBreaker – 启用断路器实现。

  • 创建一个 REST 控制器类EmployeeController以公开Employee数据。
    EmployeeController.java ```java package com.howtodoinjava.example.apigateway.controller;

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpMethod; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@RestController public class EmployeeController {

  1. @Autowired
  2. RestTemplate restTemplate;
  3. @RequestMapping(value = "/employeeDetails/{employeeid}", method = RequestMethod.GET)
  4. @HystrixCommand(fallbackMethod = "fallbackMethod")
  5. public String getStudents(@PathVariable int employeeid)
  6. {
  7. System.out.println("Getting Employee details for " + employeeid);
  8. String response = restTemplate.exchange("http://employee-service/findEmployeeDetails/{employeeid}",
  9. HttpMethod.GET, null, new ParameterizedTypeReference<String>() {}, employeeid).getBody();
  10. System.out.println("Response Body " + response);
  11. return "Employee Id - " + employeeid + " [ Employee Details " + response+" ]";
  12. }
  13. public String fallbackMethod(int employeeid){
  14. return "Fallback response:: No employee details available temporarily";
  15. }
  16. @Bean
  17. @LoadBalanced
  18. public RestTemplate restTemplate() {
  19. return new RestTemplate();
  20. }

}

  1. -
  2. `src/main/resources`目录中创建`application.yml`
  3. <br />`application.yml`
  4. ```java
  5. server:
  6. port: 8010 #port number
  7. eureka:
  8. instance:
  9. leaseRenewalIntervalInSeconds: 5
  10. leaseExpirationDurationInSeconds: 2
  11. client:
  12. serviceUrl:
  13. defaultZone: http://localhost:8761/eureka/
  14. healthcheck:
  15. enabled: true
  16. lease:
  17. duration: 5
  18. spring:
  19. application:
  20. name: api-gateway
  21. management:
  22. security:
  23. enabled: false
  24. logging:
  25. level:
  26. com.self.sprintboot.learning.apigateway: DEBUG
  • 启动应用程序,可访问http://localhost:8010/employeeDetails/111
    服务监控 – Hystrix,Eureka 管理员和 Spring Boot 管理员 - 图2

5. Hystrix 仪表板视图

  • 通过 Hystrix 仪表板进行监控,请在http://localhost:8010/hystrix打开 Hystrix 仪表板。
    服务监控 – Hystrix,Eureka 管理员和 Spring Boot 管理员 - 图3
    这是主页,需要放置事件流 URL 进行监视。

  • 现在在信息中心中查看 Hystrix 流http://localhost:8010/hystrix.stream
    服务监控 – Hystrix,Eureka 管理员和 Spring Boot 管理员 - 图4
    这提供了所有 Hystrix 命令和线程池的实时信息。

6. Eureka 管理仪表板视图

现在让我们学习如何使用 Eureka 管理控制台视图。

  • Spring boot 初始化器 / Spring 工具套件创建一个 Spring Boot 项目,具有以下依赖 Eureka Server执行器WebSpring Boot 管理员服务器
    服务监控 – Hystrix,Eureka 管理员和 Spring Boot 管理员 - 图5

  • 主应用程序类EurekaServerApplication,用于启动 Spring Boot 应用程序。
    EurekaServerApplication.java ```java package com.howtodoinjava.example.eureka;

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

import de.codecentric.boot.admin.config.EnableAdminServer;

@SpringBootApplication @EnableEurekaServer @EnableAdminServer

public class EurekaServerApplication {

  1. public static void main(String[] args) {
  2. SpringApplication.run(EurekaServerApplication.class, args);
  3. }

}

  1. <br />`@EnableEurekaServer` – 此注解将使此应用程序充当微服务注册表和发现服务器。
  2. <br />`@EnableAdminServer` – 此注解提供 Spring Boot Admin 配置。
  3. -
  4. 在`src/main/resources`目录中创建`application.yml`和`bootstrap.yml`。
  5. -
  6. 使用给定的配置添加`application.yml`。 请注意,对于 Spring boot admin 服务器,提供了一个不同的上下文路径`/admin`,以免与`/eureka`冲突。
  7. <br />`application.yml`
  8. ```java
  9. server:
  10. port: ${PORT:8761}
  11. eureka:
  12. client:
  13. registryFetchIntervalSeconds: 5
  14. registerWithEureka: false
  15. serviceUrl:
  16. defaultZone: ${DISCOVERY_URL:http://localhost:8761}/eureka/
  17. instance:
  18. leaseRenewalIntervalInSeconds: 10
  19. management:
  20. security:
  21. enabled: false
  22. spring:
  23. boot:
  24. admin:
  25. context-path: /admin #A different context path for Spring boot admin server has been provided avoiding conflict with eureka
  • 创建bootstrap.yml并进行配置。
    bootstrap.yml
    1. spring:
    2. application:
    3. name: Eureka-Server
    4. cloud:
    5. config:
    6. uri: ${CONFIG_SERVER_URL:http://localhost:8888}
  • 启动应用程序。 但在此之前,请确保之前启动了上面提到的其余客户端应用程序,以便查看所有已注册的应用程序。 该应用程序可通过http://localhost:8761访问。
    服务监控 – Hystrix,Eureka 管理员和 Spring Boot 管理员 - 图6

7. Spring Boot 管理仪表板视图

  • 要通过 Spring Boot Admin 服务器进行监视,请调用此 URL,该 URL 运行在不同的上下文路径 - http://localhost:8761/admin中。
    服务监控 – Hystrix,Eureka 管理员和 Spring Boot 管理员 - 图7

  • 该管理界面提供应用程序概述,桌面通知,应用程序运行状况检查,日志文件浏览,JMX Bean,线程堆转储等。要查看单个应用程序的运行状况并监视其指标,请单击详细信息按钮。 它将带您到各个应用程序的管理仪表板。
    服务监控 – Hystrix,Eureka 管理员和 Spring Boot 管理员 - 图8

  • 使用仪表板管理日志级别。
    服务监控 – Hystrix,Eureka 管理员和 Spring Boot 管理员 - 图9

  • 使用仪表板管理运行时环境属性。
    服务监控 – Hystrix,Eureka 管理员和 Spring Boot 管理员 - 图10

  • 您也可以使用它来查看 HTTP 跟踪。
    服务监控 – Hystrix,Eureka 管理员和 Spring Boot 管理员 - 图11

从 Spring 运行管理员的角度来看,目前是这样。 请随时添加任何评论/查询。 我们很乐意解决同样的问题。

下载员工服务

下载 API 网关

下载 Eureka 服务器

学习愉快!