当前位置:   article > 正文

Spring Cloud实战(三)-监控中心_springcloud monitor

springcloud monitor

接着上一篇 Spring Cloud实战(二)-注册中心 现在开始搭建监控中心

一.监控中心monitor-server

1.添加spring-boot-admin版本控制到cloud-action的pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <packaging>pom</packaging>
  6. <modules>
  7. <module>register-server</module>
  8. <module>config-server</module>
  9. <module>monitor-server</module>
  10. <module>api</module>
  11. <module>user-server</module>
  12. <module>order-server</module>
  13. <module>pay-server</module>
  14. <module>manage-server</module>
  15. <module>repository-server</module>
  16. </modules>
  17. <parent>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-parent</artifactId>
  20. <version>2.6.6</version>
  21. <relativePath/> <!-- lookup parent from repository -->
  22. </parent>
  23. <groupId>com.example</groupId>
  24. <artifactId>cloud-action</artifactId>
  25. <version>0.0.1-SNAPSHOT</version>
  26. <name>cloud-action</name>
  27. <description>spring cloud 微服务实战</description>
  28. <properties>
  29. <skipTests>true</skipTests>
  30. <java.version>1.8</java.version>
  31. <spring-cloud.version>2021.0.1</spring-cloud.version>
  32. <spring-boot-admin.version>2.6.3</spring-boot-admin.version>
  33. </properties>
  34. <dependencies>
  35. <dependency>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-starter-web</artifactId>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.springframework.boot</groupId>
  41. <artifactId>spring-boot-starter-actuator</artifactId>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.projectlombok</groupId>
  45. <artifactId>lombok</artifactId>
  46. <scope>provided</scope> <!-- 作用在编译和测试时,同时没有传递性-->
  47. </dependency>
  48. <dependency>
  49. <groupId>org.springframework.boot</groupId>
  50. <artifactId>spring-boot-starter-test</artifactId>
  51. <scope>test</scope>
  52. </dependency>
  53. </dependencies>
  54. <dependencyManagement>
  55. <dependencies>
  56. <dependency>
  57. <groupId>org.springframework.cloud</groupId>
  58. <artifactId>spring-cloud-dependencies</artifactId>
  59. <version>${spring-cloud.version}</version>
  60. <type>pom</type>
  61. <scope>import</scope>
  62. </dependency>
  63. <dependency>
  64. <groupId>de.codecentric</groupId>
  65. <artifactId>spring-boot-admin-dependencies</artifactId>
  66. <version>${spring-boot-admin.version}</version>
  67. <type>pom</type>
  68. <scope>import</scope>
  69. </dependency>
  70. </dependencies>
  71. </dependencyManagement>
  72. <build>
  73. <plugins>
  74. <plugin>
  75. <groupId>org.springframework.boot</groupId>
  76. <artifactId>spring-boot-maven-plugin</artifactId>
  77. </plugin>
  78. </plugins>
  79. </build>
  80. </project>

2.pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>cloud-action</artifactId>
  7. <groupId>com.example</groupId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>monitor-server</artifactId>
  12. <dependencies>
  13. <dependency>
  14. <groupId>de.codecentric</groupId>
  15. <artifactId>spring-boot-admin-starter-server</artifactId>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework.cloud</groupId>
  19. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  20. </dependency>
  21. </dependencies>
  22. </project>

3.application.yml

  1. spring:
  2. application:
  3. name: monitor-server
  4. profiles:
  5. active: single
  6. #监控配置
  7. management:
  8. endpoints:
  9. web:
  10. exposure:
  11. include: "*" #公开所有端点 对于生产,您应该仔细选择要公开的端点.
  12. endpoint:
  13. health:
  14. show-details: always
  15. info:
  16. env:
  17. enabled: true
  18. ---
  19. #单机版
  20. spring:
  21. config:
  22. activate:
  23. on-profile: single
  24. server:
  25. port: 2222 #当前eureka-server服务的端口号
  26. eureka:
  27. instance:
  28. lease-renewal-interval-in-seconds: 30 #服务续约(心跳检测)时间,默认30秒
  29. lease-expiration-duration-in-seconds: 90 #服务剔除时间,默认90秒
  30. health-check-url-path: /actuator/health
  31. metadata-map:
  32. startup: ${random.int} #需要在重启后触发信息和端点更新
  33. client:
  34. service-url:
  35. defaultZone: http://localhost:1111/eureka/ #eureka-client设置eureka-server的地址

4.MonitorApplication.java

  1. package com.example.monitor;
  2. import de.codecentric.boot.admin.server.config.EnableAdminServer;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. /**
  6. * @author 86188
  7. */
  8. @SpringBootApplication
  9. @EnableAdminServer //开启监控服务
  10. public class MonitorApplication {
  11. public static void main(String[] args) {
  12. SpringApplication.run(MonitorApplication.class, args);
  13. }
  14. }

5.测试

java -jar monitor-server-0.0.1-SNAPSHOT.jar

 可以看到监控信息非常详细

特别说明:

实际开发中监控模块完全可以和eureka注册中心放在一块,只需要配置spring.boot.admin.context-path 以便 Spring Boot Admin Server UI 不会与 Eureka 的 UI 冲突就可以了.此处为了凸显微服务的特性,所以单独作为一个服务.

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/101880
推荐阅读
相关标签
  

闽ICP备14008679号