spring-cloud开发Greenwich版:服务状态监控可视化(十六)

上文中的被监控端微服务,我们引入了spring-boot-admin-starter-client依赖,这样一来,貌似对我们的微服务有某种侵入性;在实际应用中,我们的微服务都是注册到eureka server服务器,因此监控服务端spring-boot-admin可以从注册服务器获取我们的微服务列表,而不用每一个监控端微服务都引入监控服务端的url信息,也不用引入spring-boot-admin-starter-client依赖(当然需要引入spring-boot-starter-actuator依赖)

改造微服务ralab-admin

  • Add the Maven spring-cloud-starter-netflix-eureka-client denpendencys
  • Add @EnableEurekaClient to the application class
  • Config eureka server info to application.yml file
    其pom.xml文件如下:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    <?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.8.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ralab</groupId>
    <artifactId>ralab-admin</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ralab-admin</name>
    <description>Demo project for Spring Boot</description>

    <properties>
    <java.version>1.8</java.version>
    <spring-boot-admin.version>2.1.5</spring-boot-admin.version>
    <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
    </properties>

    <dependencies>
    <dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
    </dependency>

    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>
    </dependencies>

    <dependencyManagement>
    <dependencies>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>${spring-cloud.version}</version>
    <type>pom</type>
    <scope>import</scope>
    </dependency>
    <dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-dependencies</artifactId>
    <version>${spring-boot-admin.version}</version>
    <type>pom</type>
    <scope>import</scope>
    </dependency>
    </dependencies>
    </dependencyManagement>

    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    </plugins>
    </build>

    </project>

RalabAdminApplication文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.ralab.ralabadmin;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableAdminServer
@EnableEurekaClient
public class RalabAdminApplication {

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

}

application.yml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
server:
port: 8080
spring:
application:
name: service-admin

eureka:
instance:
# ip-address: 127.0.0.1
prefer-ip-address: true # 补充
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/

然后运行eureka server服务器,运行被监控微服务,运行上面的监控微服务,打开http://127.0.0.1:8080

记得被监控的微服务加入依赖

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

添加配置

1
2
3
4
5
6
7
8
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always

坚持原创技术分享,您的支持是我前进的动力!