糖尿病康复,内容丰富有趣,生活中的好帮手!
糖尿病康复 > Spring boot admin 监控配置

Spring boot admin 监控配置

时间:2023-02-02 12:38:51

相关推荐

Spring boot admin 监控配置

1、项目背景

项目开发完成并部署上线,系统正式进入试运行;在试运行期间由于客户服务问题导致部分服务不可用,幸亏系统采用集群架构没有造成系统正常使用,但该问题存在系统风险,问题出现后没被第一时间发现和处理。为了解决该问题,采用简便的Spring boot admin 插件来监控系统正常运行,Spring boot admin 知识监控软件的一种,其他监控软件或者插件大家可自行选择;

2、项目目标

使用Sping boot admin 监控插件,主要是监控系统程序是否存活,如果服务出现问题后,需要发送邮件给运维同事,及时来处理;

3、详细配置

系统分为服务端和客户端,接下来分开两个章节来介绍,并将配置相关信息介绍说明;

3.1、服务端

配置需要使用的jar文件,这里采用maven管理pom.xml配置详见如下:

<?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.4</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>.jack</groupId><artifactId>springadmindemo1</artifactId><version>0.0.1-SNAPSHOT</version><name>springadmindemo1</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-server</artifactId><version>2.6.1</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-server-ui</artifactId><version>2.6.2</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency></dependencies></project>

application.properties文件具体配置

# 配置服务启动的端口号server.port=1801# 配置服务器端访问的账户和密码spring.security.user.name=userspring.security.user.password=123456# 使用的邮箱服务 qq 163等spring.mail.host=# 发送者spring.mail.username=XXXXX@# 授权码spring.mail.password=XXXXX#收件人spring.boot.admin.notify.mail.to=XXXXX@#发件人spring.boot.admin.notify.mail.from=XXXXX@

在Springadmindemo1Application启动类中添加注解@EnableAdminServer

package .jack.springadmindemo1;import de.codecentric.boot.admin.server.config.EnableAdminServer;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@EnableAdminServer@SpringBootApplicationpublic class Springadmindemo1Application {public static void main(String[] args) {SpringApplication.run(Springadmindemo1Application.class, args);}}

关于权限配置看个人需求,相关代码引用网络文件代码,记得创建config文件夹;

package .jack.springadmindemo1.config;import de.codecentric.boot.admin.server.config.AdminServerProperties;import org.springframework.boot.autoconfigure.security.SecurityProperties;import org.springframework.context.annotation.Configuration;import org.springframework.http.HttpMethod;import org.springframework.security.config.Customizer;import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;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;import org.springframework.security.web.csrf.CookieCsrfTokenRepository;import org.springframework.security.web.util.matcher.AntPathRequestMatcher;import java.util.UUID;@Configuration(proxyBeanMethods = false)public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {private final AdminServerProperties adminServer;private final SecurityProperties security;public SecuritySecureConfig(AdminServerProperties adminServer, SecurityProperties security) {this.adminServer = adminServer;this.security = security;}@Overrideprotected void configure(HttpSecurity http) throws Exception {SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();successHandler.setTargetUrlParameter("redirectTo");successHandler.setDefaultTargetUrl(this.adminServer.path("/"));http.authorizeRequests((authorizeRequests) -> authorizeRequests.antMatchers(this.adminServer.path("/assets/**")).permitAll().antMatchers(this.adminServer.path("/actuator/info")).permitAll().antMatchers(this.adminServer.path("/actuator/health")).permitAll().antMatchers(this.adminServer.path("/login")).permitAll().anyRequest().authenticated()).formLogin((formLogin) -> formLogin.loginPage(this.adminServer.path("/login")).successHandler(successHandler).and()).logout((logout) -> logout.logoutUrl(this.adminServer.path("/logout"))).httpBasic(Customizer.withDefaults()).csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()).ignoringRequestMatchers(new AntPathRequestMatcher(this.adminServer.path("/instances"),HttpMethod.POST.toString()),new AntPathRequestMatcher(this.adminServer.path("/instances/*"),HttpMethod.DELETE.toString()),new AntPathRequestMatcher(this.adminServer.path("/actuator/**")))).rememberMe((rememberMe) -> rememberMe.key(UUID.randomUUID().toString()).tokenValiditySeconds(1209600));}// Required to provide UserDetailsService for "remember functionality"@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.inMemoryAuthentication().withUser(security.getUser().getName()).password("{noop}" + security.getUser().getPassword()).roles("USER");}}

3.2、客户端

配置需要使用的jar文件,这里采用maven管理pom.xml配置详见如下:

<?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.4</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>.wuyong</groupId><artifactId>springbootadminclientdemo1</artifactId><version>0.0.1-SNAPSHOT</version><name>springbootadminclientdemo1</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-client</artifactId><version>2.6.1</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency></dependencies></project>

application.properties文件具体配置

# 客户端程序启动端口server.port=1802# 客户端程序启动名称(可自行配置)spring.application.name=Spring Boot Admin Client# 配置监控服务端的地址和端口spring.boot.admin.client.url=http://127.0.0.1:1801/# 配置监控服务端的账户和密码spring.boot.admin.client.username=userspring.boot.admin.client.password=123456# 配置监控访问范围和监控页面显示的具体信息management.endpoints.web.exposure.include=*management.endpoint.health.show-details=always# 系统链接MySQL数据库的配置spring.datasource.driverClassName=com.mysql.cj.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/test_test009?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=truespring.datasource.username=rootspring.datasource.password=123456

Springbootadminclientdemo1Application类型无需配置;

3.3、效果展示

至此客户端和服务端配置完成,进入服务端、客户端的Springbootadmin启动类,点击启动运行,接下里我们看运行效果;

1、 打开浏览器输入http://127.0.0.1:1801/login后直接回,我们可以看到登录页面,输入在管理端配置的账户和密码user/123456后登录

2、进入后可查看监控系统各个指标和信息

点击图标可进入到监控的详情页面,具体详见如下

4、总结

1、在处理中遇到了org.springframework.boot和de.codecentric的jar文件版本问题,大家可根据自己情况引用jar文件

2、文章主要用于分享给大家一同学习,如果有不足和问题请大家指正

如果觉得《Spring boot admin 监控配置》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。