一、创建Spring Cloud Admin项目
1.pom文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath />
</parent>
<groupId>slan-shop</groupId>
<artifactId>server-admin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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-server</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2.配置文件
server.port=8005
spring.application.name=server-admin
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
3.启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
@EnableAutoConfiguration
@EnableAdminServer
public class SpringBootAdminApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAdminApplication.class, args);
}
}
4.在需要监控的项目添加配置
4.1.配置文件
spring.boot.admin.client.url=http://localhost:8005
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=ALWAYS
4.2 在需要监控的项目里添加pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
二、测试
访问地址http://localhost:8005/,端口是自己配置文件里配置的
通过上图可以看到注册上来的服务,我配置了4个项目,所以这里就有4个服务。选择一个服务可以看到服务内的详细信息,如下图:
三、添加邮件提醒
1.添加pom依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2.添加配置:
spring.mail.host=smtp.qq.com
spring.mail.username=303138741@qq.com
#注意这里的password不是邮箱密码,而是邮箱授权码
spring.mail.password=wsytydsenaipbjdg
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
# 发件邮箱
spring.boot.admin.notify.mail.from=303138741@qq.com
# 收件邮箱
spring.boot.admin.notify.mail.to=2389166591@qq.com
3.测试:
1.停掉一个服务或新服务上线都会发邮件,如下图是一个服务下线后发送的一封邮件提醒: