Spring Boot Admin是一个开源社区项目,用于管理和监控SpringBoot应用程序。 应用程序作为Spring Boot Admin Client向为Spring Boot Admin Server注册(通过HTTP)或使用SpringCloud注册中心(例如Eureka,Consul,Zookeeper)发现。 UI是的AngularJs应用程序,展示Spring Boot Admin Client的Actuator端点上的一些监控。
常见的功能或者监控如下:
搭建版本:
parent的pom.xml文件:
1.8
Finchley.SR2
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
1、搭建SpringBoot Admin Server端(admin-server)
添加相关依赖:
de.codecentric
spring-boot-admin-starter-server
2.1.0
org.springframework.boot
spring-boot-starter-web
在启动类添加注解@EnableAdminServer,开启AdminServer的功能:
package com.example.demo.admin;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@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: 8081
2、搭建SpringBoot Admin Client端(admin-client)
添加相关依赖:
de.codecentric
spring-boot-admin-starter-client
2.1.0
org.springframework.boot
spring-boot-starter-web
在application.yml配置应用名和端口信息,以及向admin-server注册的地址为http://localhost:8081,最后暴露自己的actuator的所有端口信息:
server:
port: 8082
spring:
application:
name: admin-client
boot:
admin:
client:
url: http://localhost:8081
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: '*'
3、分别启动admin-server和admin-client,浏览器访问:http://localhost:8081
点击wallboard可以查看admin-client具体的信息,比如内存状态信息:
查看spring bean的情况:
SpringBoot和SpringCloud的版本和上面的一样,没有变化。
1、搭建注册中心(eureka-server)
添加eureka依赖:
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
在application.yml配置端口信息,以及defaultZone和防止自注册,系统暴露eureka-server的actuator的所有端口:
server:
port: 8001
spring:
application:
name: eureka-server
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka
register-with-eureka: false
fetch-registry: false
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: '*'
启动类添加注解 @EnableEurekaServer,启动Eureka Server:
package com.example.demo.admin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
2、搭建SpringBoot Admin Server端(eureka-admin-server)
添加依赖:
de.codecentric
spring-boot-admin-starter-server
2.1.0
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-security
org.springframework.boot
spring-boot-starter-mail
在application.yml配置:
server:
port: 8002
spring:
application:
name: eureka-admin-server
security:
user:
name: "admin"
password: "admin"
mail:
host: smtp.163.com
username: [email protected]
password: admin #此处的密码是授权码
boot:
admin:
notify:
mail:
to: [email protected]
eureka:
client:
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8001}/eureka/
registry-fetch-interval-seconds: 5
instance:
health-check-url-path: /actuator/health
lease-renewal-interval-in-seconds: 10
metadata-map:
user.name: ${spring.security.user.name}
user.password: ${spring.security.user.password}
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
新建配置类SecuritySecureConfig继承WebSecurityConfigurerAdapter:
package com.example.demo.admin.config;
import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
/**
* 路径:com.example.demo.admin.config
* 类名:
* 功能:《用一句描述一下》
* 备注:
* 创建人:typ
* 创建时间:2019/1/11 11:05
* 修改人:
* 修改备注:
* 修改时间:
*/
@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter{
private final String adminContextPath;
public SecuritySecureConfig(AdminServerProperties adminServerProperties){
this.adminContextPath = adminServerProperties.getContextPath();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
http.authorizeRequests()
.antMatchers(adminContextPath + "/assets/**").permitAll()
.antMatchers(adminContextPath + "/login").permitAll()
.anyRequest()
.authenticated()
.and()
.formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler )
.and()
.logout().logoutUrl(adminContextPath+"/logout")
.and()
.httpBasic()
.and()
.csrf()
.disable();
}
}
启动类添加@EnableAdminServer注解,开启admin server的功能,加上@EnableDiscoveryClient注解开启eurke client的功能:
package com.example.demo.admin;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableAdminServer
@EnableDiscoveryClient
public class EurekaAdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaAdminServerApplication.class, args);
}
}
3、搭建SpringBoot Admin Client端(eureka-admin-client)
添加依赖:
由于2.1.0采用webflux,引入webflux的起步依赖,引入eureka-client的起步依赖,并引用actuator的起步依赖。
org.springframework.boot
spring-boot-starter-webflux
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-actuator
在application.yml配置应用名、端口、向注册中心注册的地址,以及暴露actuator的所有端口:
server:
port: 8003
spring:
application:
name: eureka-admin-client
eureka:
instance:
health-check-url-path: /actuator/health
lease-renewal-interval-in-seconds: 10
client:
registry-fetch-interval-seconds: 5
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8001}/eureka/
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
在启动类加上@EnableDiscoveryClie注解,开启DiscoveryClient的功能:
package com.example.demo.admin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaAdminClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaAdminClientApplication.class, args);
}
}
启动工程,在浏览器上访问:http://localhost:8002/,登录输入配置好的用户名和密码(admin/admin),如下显示:
输入配置的用户名和密码,登录:
查看注册中信息,浏览器访问:http://localhost:8001/
源码下载:
https://download.csdn.net/download/typ1805/10910479
欢迎关注