SpringCloud Tutorial 7, the usage of Spring Boot Admin;SpringCloud系列教程7,Spring Boot Admin的使用

显然,在微服务架构中,我们需要一个监控中心用于监控所有服务的状态,以及服务器资源占用,Spring社区提供了一个解决方案,Spring Boot Admin。
Obviously, we need a monitoring center to monitor the statuses of all services and the occupation of the servers in microservice architectures. Spring Community provides a solution, Spring Boot Admin.
接下来,我们开始建立Spring Boot Admin的服务端。
Then we start to build the server of Spring Boot Admin.
首先,新建一个子模块admin,添加如下依赖。
Firstly, create a child moudle, admin, and add the following dependencies.
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
<dependencies>
        <!-- https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-server -->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>2.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</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-mail</artifactId>
            <version>2.0.3.RELEASE</version>
        </dependency>
    </dependencies>
第二,创建启动类。
Secondly, create the start class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.gerry.springcloudtest;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Configuration;

@SpringBootApplication
@EnableDiscoveryClient
@EnableAdminServer
@EnableAutoConfiguration
@Configuration
public class AdminApplication {
    public static void main(String[] args) {
        SpringApplication.run(AdminApplication.class, args);
    }
}
第三,创建并设置application.yml
Thirdly, create and set the application.yml
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
spring:
  application:
    name: ADMIN-SERVER
  boot:
    admin:
      notify:
        mail:
          from: [email protected]
          to:   [email protected]
          enabled: true
  mail:
    host: smtp.163.com
    port: 25
    username: [email protected]
    password: xxxxxx

server:
  port: 8775  #the port on which the application runs

eureka:
  instance:
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka/

management:
  endpoint:
    health:
      show-details: always
  endpoints:
    web:
      exposure:
        include: "*"
注意,这里我配置了email用来发送通知,你也可以参照文档配置其他方式,具体请查阅http://codecentric.github.io/spring-boot-admin/current
Notice, I configure the email to send notifications, you can also use other options, for more information, please refer to http://codecentric.github.io/spring-boot-admin/current
之后,我们开始建立Spring Boot Admin的客户端。
After this, we start to build the client of Spring Boot Admin.
首先,新建一个子模块adminClient,添加如下依赖。
Firstly, create a child moudle, adminClient, and add the following dependencies.
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
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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


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

        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>2.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.jolokia</groupId>
            <artifactId>jolokia-core</artifactId>
        </dependency>
    </dependencies>
第二,创建启动类。
Secondly, create the start class.
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.gerry.springcloudtest.adminClient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class AdminClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(AdminClientApplication.class, args);
    }
}
第三,创建API口
Thirdly, create the API
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.gerry.springcloudtest.adminClient.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @RequestMapping("/test")
    public Integer test(){
        return 1;
    }
}
第四,创建并设置application.yml
Fourthly, create and set the application.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
spring:
  application:
    name: ADMIN-CLIENT

server:
  port: 8776  #the port on which the application runs

eureka:
  instance:
    prefer-ip-address: true
    health-check-url-path: /actuator/health
    lease-renewal-interval-in-seconds: 10
  client:
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka/
    registry-fetch-interval-seconds: 5

management.endpoints.web.exposure.include: "*"
最后,启动项目,浏览器查看http://localhost:8775/,查看Spring Boot Admin,确认是否使用成功
Finally, run the application, openhttp://localhost:8775/ in the browser to check the Spring Boot Admin, so that you can know whether it is used successfully.
此外,在生产环境中,可以配置账号密码,以及配置允许查看哪部分模块,提高安全性。
In addition, in production environment, we can configure the account and password, and configure the allowance to improve the security.
项目已托管在Github,地址https://github.com/tzry/springclouddemo
The project is hosted on Github, the url is https://github.com/tzry/springclouddemo



Leave a Reply

Your email address will not be published. Required fields are marked *