赞
踩
前面我们讲了actuator,但是Spring Boot Actuator只是提供了一个个的接口,需要我们自行集成到监控程序中。Admin-UI基于actuator实现,能够返回界面展示监控信息
SpringBootAdmin用来管理和监控SpringBoot应用程序,它利用spring-boot-starter-actuator提供的功能,将各个微服务的状态整合到一起,并提供良好的界面查看支持,并且能够动态的修改实例日志级别。SpringBootAdmin分为server端和client端,server端可查看各个微服务的状态,client端将微服务注册到server端。github源码地址:https://github.com/codecentric/spring-boot-admin
- <?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>
-
- <groupId>com.example</groupId>
- <artifactId>admin-ui-server</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>admin-ui-server</name>
- <description>Demo project for Spring Boot</description>
-
- <properties>
- <java.version>1.8</java.version>
- </properties>
-
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.2.1.RELEASE</version>
- </parent>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- <exclusions>
- <exclusion>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-tomcat</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>de.codecentric</groupId>
- <artifactId>spring-boot-admin-server</artifactId>
- <version>2.2.1</version>
- </dependency>
- <dependency>
- <groupId>de.codecentric</groupId>
- <artifactId>spring-boot-admin-server-ui</artifactId>
- <version>2.2.1</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-security</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-jetty</artifactId>
- </dependency>
- </dependencies>
-
-
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
-
- </project>
- server.port=8081
- spring.application.name=admin-server
-
- #监控中心账号密码
- spring.security.user.name=admin
- spring.security.user.password=admin
-
- package com.example.adminuiserver;
-
- 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.csrf.CookieCsrfTokenRepository;
-
- import de.codecentric.boot.admin.server.config.AdminServerProperties;
-
-
-
-
- @Configuration
- public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
-
- private final String contextPath;
-
- public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
- this.contextPath = adminServerProperties.getContextPath();
- }
-
- @Override
- protected void configure(HttpSecurity http) throws Exception {
- // 跨域设置,SpringBootAdmin客户端通过instances注册,见InstancesController
- http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
- .ignoringAntMatchers(contextPath + "/instances");
-
- http.authorizeRequests().antMatchers(contextPath + "/assets/**").permitAll(); // 静态资源
- http.authorizeRequests().anyRequest().authenticated(); // 所有请求必须通过认证
-
- // 整合spring-boot-admin-server-ui
- http.formLogin().loginPage("/login").permitAll();
- http.logout().logoutUrl("/logout").logoutSuccessUrl("/login");
-
- // 启用basic认证,SpringBootAdmin客户端使用的是basic认证
- http.httpBasic();
- }
- }
- package com.example.adminuiserver;
-
- 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.context.annotation.Configuration;
-
- @EnableAdminServer
- @SpringBootApplication
- public class AdminUiServerApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(AdminUiServerApplication.class, args);
- }
- }
-
- <?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>
-
- <groupId>com.example</groupId>
- <artifactId>admin-ui-client</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>admin-ui-client</name>
- <description>Demo project for Spring Boot</description>
-
- <properties>
- <java.version>1.8</java.version>
- </properties>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.0.0.RELEASE</version>
- </parent>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>de.codecentric</groupId>
- <artifactId>spring-boot-admin-starter-client</artifactId>
- <version>2.0.0</version>
- </dependency>
-
- </dependencies>
-
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
-
- </project>
- server.port=8080
- spring.application.name=admin-client
-
-
- management.endpoints.web.exposure.include=*
- management.endpoint.health.show-details=always
- spring.boot.admin.client.url=http://127.0.0.1:8081
- #spring.boot.admin.client.instance.metadata.user.name=admin
- #spring.boot.admin.client.instance.metadata.user.password=admin
-
- spring.boot.admin.client.username=admin
- spring.boot.admin.client.password=admin
- package com.example.adminuiclient;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- @SpringBootApplication
- public class AdminUiClientApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(AdminUiClientApplication.class, args);
- }
-
- }
上面我们配置的账号和密码都是 admin
展示了服务的运行信息
点击应用墙进去后 可以看到各类运行参数信息
SpringBootAdmin提供了多种通知功能,也可以自定义通知提醒。这里我们验证一下邮件通知功能
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-mail</artifactId>
- </dependency>
- spring.mail.host=smtp.qq.com
- spring.mail.username=xxxx@foxmail.com
- spring.mail.password=xxxx
-
- spring.boot.admin.notify.mail.from=xxxx@foxmail.com
- spring.boot.admin.notify.mail.to=yyyy@foxmail.com
当客户端实例停掉后,我们会收到邮件提醒。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。