1. 创建Spring Boot Admin Server
1.1. pom依赖
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
1.2. 开启 AdminServer
@SpringBootApplication
@EnableAdminServer
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run( AdminServerApplication.class, args );
}
}
在工程的配置文件application.yml中配置程序名和程序的端口,代码如下:
spring:
application:
name: admin-server
server:
port: 8769
这样Admin Server就创建好了。
2. 创建Spring Boot Admin Client
2.1. client pom依赖
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
在工程的配置文件application.yml中配置应用名和端口信息,
- 以及向admin-server注册的地址为http://localhost:8769,最后暴露自己的actuator的所有端口信息,
具体配置如下:
2.2. client yaml配置文件
spring:
application:
name: admin-client
boot:
admin:
client:
url: http://localhost:8769
server:
port: 8768
management:
endpoints:
web:
exposure:
include: '*' #所有端口信息
endpoint:
health:
show-details: ALWAYS
在工程的启动文件如下:
@SpringBootApplication
public class AdminClientApplication {
public static void main(String[] args) {
SpringApplication.run( AdminClientApplication.class, args );
}
一次启动两个工程,在浏览器上输入localhost:8769 ,浏览器显示的界面如下:
3. Spring boot Admin结合eureka注册中心使用
3.1. 搭建eureka server
3.1.1. pom依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
3.1.2. 配置文件
spring:
application:
name: eureka-server
#安全验证配置
security:
user:
name: root
password: 123456
#端口配置
server:
port: 7001
eureka:
name: root
password: 123456
client:
region: zsy
availability-zones:
zsy: zsy-zone1,zsy-zone2
#表示是否将自己注册进EurekaServer默认为true
#如果建高可用的系统,需要设置为true
fetch-registry: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
register-with-eureka: true
service-url:
defaultZone: http://${eureka.name}:${eureka.password}@127.0.0.1:7002/eureka/,http://${eureka.name}:${eureka.password}@127.0.0.1:7001/eureka/
instance:
hostname: zone1
#服务续约
# Eureka客户端向服务端发送心跳的时间间隔,单位为秒(默认是30秒)
lease-renewal-interval-in-seconds: 10
# Eureka服务端在收到最后一次心跳后等待时间上限,单位为秒(默认是90秒),超时将剔除服务
lease-expiration-duration-in-seconds: 20
# 访问路径可以显示IP地址
prefer-ip-address: true
# 主机名:应用名:ip:端口
instance-id: ${spring.cloud.client.hostname}:${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
server:
# 关闭自我保护机制,保证不可用服务被及时剔除,默认打开
enable-self-preservation: false
# # 失效剔除,单位是毫秒,默认为90秒
# eviction-interval-timer-in-ms: 20000
management:
endpoints:
web:
exposure:
include: '*' #所有端口信息
endpoint:
health:
show-details: ALWAYS
3.1.3. 启动类
在工程的启动文件EurekaServerApplication加上@EnableEurekaServer注解开启Eureka Server.
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run( EurekaServerApplication.class, args );
}
}
3.1.4. 配置类放开actuator端口
@EnableWebSecurity
public class SecurityConf extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable(); //关闭跨域
http.authorizeRequests() //认证请求
.antMatchers("/actuator/**").permitAll();
super.configure(http);
}
}
3.2. 搭建admin-server
3.2.1. pom依赖
<artifactId>spring-boot-admin-starter-server</artifactId>
<artifactId>spring-boot-starter-web</artifactId>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
3.2.2. admin server的 yml
spring:
application:
name: admin
security:
user:
name: root
password: 123456
server:
port: 8000
eureka:
name: root
password: 123456
client:
region: zsy
availability-zones:
zsy: zsy-zone2
#表示是否将自己注册进EurekaServer默认为true
#如果建高可用的系统,需要设置为true
fetch-registry: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
register-with-eureka: true
service-url:
defaultZone: http://${eureka.name}:${eureka.password}@127.0.0.1:7002/eureka/,http://${eureka.name}:${eureka.password}@127.0.0.1:7001/eureka/
instance:
metadata-map:
user.name: ${spring.security.user.name}
user.password: ${spring.security.user.password}
management:
endpoints:
web:
exposure:
include: "*" #配置这个将监控所有的信息
endpoint:
health:
show-details: ALWAYS
3.2.3. 启动类
@SpringBootApplication
@EnableAdminServer
@EnableEurekaClient
public class AdminApplication {
public static void main(String[] args) {
SpringApplication.run(AdminApplication.class, args);
}
}
3.2.4. security 配置类
@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
private final String adminContextPath;
//注入 adminServerProperties
public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
this.adminContextPath = adminServerProperties.getContextPath();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
http.authorizeRequests() //assets 和 login 同意
.antMatchers(adminContextPath + "/assets/**").permitAll()
.antMatchers(adminContextPath + "/login").permitAll()
.antMatchers(adminContextPath + "/actuator/**").permitAll()
.anyRequest().authenticated() //其他都需要登录
.and() //登录成功
.formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
.logout().logoutUrl(adminContextPath + "/logout").and()
.httpBasic().and()
.csrf().disable(); //csrf禁用
// @formatter:on
}
}
4. 集成邮箱报警功能
在spring boot admin中,也可以集成邮箱报警功能,比如服务不健康了、下线了,都可以给指定邮箱发送邮件。集成非常简单,只需要改造下admin-server即可:
在admin-server工程Pom文件,加上mail的起步依赖,代码如下:
4.1. 引入pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
在配置文件application.yml文件中,需要配置邮件相关的配置,如下:
4.2. yaml配置
spring.mail.host: smtp.163.com
spring.mail.username: miles02
spring.mail.password:
spring.boot.admin.notify.mail.to: 124746406@qq.com
做完以上配置后,当我们已注册的客户端的状态从 UP 变为 OFFLINE 或其他状态,服务端就会自动将电子邮件发送到上面配置的地址。