SpringBoot整合Spring?Boot?Admin實(shí)現(xiàn)服務(wù)監(jiān)控的方法
Spring Boot Admin用于管理和監(jiān)控一個(gè)或多個(gè)Spring Boot服務(wù),其分為Server端和Client端,Server端相當(dāng)于一個(gè)注冊中心,Client端通過Http請求向Server端進(jìn)行注冊,也可以結(jié)合Eureka、Nacos等注冊中心實(shí)現(xiàn)服務(wù)注冊。
1. Server端服務(wù)開發(fā)
1.1. 引入核心依賴
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.3.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-web</artifactId> </dependency>
1.2. application.yml配置文件
server: port: 8138 servlet: context-path: /admin spring: application: name: springboot-admin-server security: user: name: admin password: 123456
1.3. Security配置文件
@Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { private String adminSecurityContextPath; public SecurityConfig(AdminServerProperties adminServerProperties) { this.adminSecurityContextPath = adminServerProperties.getContextPath(); } @Override protected void configure(HttpSecurity http) throws Exception { SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler(); successHandler.setTargetUrlParameter("redirectTo"); http.authorizeRequests() .antMatchers(adminSecurityContextPath + "/assets/**").permitAll() .antMatchers(adminSecurityContextPath + "/login").permitAll() .anyRequest().authenticated() .and() .formLogin().loginPage(adminSecurityContextPath + "/login").successHandler(successHandler) .logout().logoutUrl(adminSecurityContextPath + "/logout") .httpBasic() .csrf() .ignoringAntMatchers(adminSecurityContextPath + "/instances", adminSecurityContextPath + "/actuator/**") .disable(); }
1.4. 主啟動(dòng)類
@EnableAdminServer @SpringBootApplication public class AdminServerApplication { public static void main(String[] args) { SpringApplication.run(AdminServerApplication.class, args); } }
2. Client端服務(wù)開發(fā)
2.1. 引入核心依賴
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
2.2. application.yml配置文件
server: port: 8238 spring: application: name: springboot-admin-client boot: admin: client: url: http://localhost:8138/admin instance: prefer-ip: true username: admin password: 123456 management: endpoints: web: exposure: include: '*' endpoint: health: show-details: always shutdown: enabled: true logfile: external-file: ./logs/log_file.log logging: config: classpath:logback-spring.xml level: com.xlhj.boot.admin: debug info: author: caijliu projectName: springboot整合springboot admin version: 1.0
2.3. logback-spring.xml文件
<?xml version="1.0" encoding="UTF-8" ?> <configuration> <property name="log.path" value="./logs"/> <!--彩色日志依賴的渲染類--> <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/> <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" /> <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" /> <property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/> <include resource="org/springframework/boot/logging/logback/console-appender.xml"/> <include resource="org/springframework/boot/logging/logback/file-appender.xml"/> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>${CONSOLE_LOG_PATTERN}</pattern> <charset>UTF-8</charset> </encoder> </appender> <!--配置日志輸出到文件--> <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${log.path}/log_file.log</file> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- 每天日志歸檔路徑以及格式 --> <FileNamePattern>${log.path}/log_file-%d{yyyy-MM-dd}-%i.log</FileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>10MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> <!--日志文件保留天數(shù)--> <maxHistory>15</maxHistory> </rollingPolicy> <logger name="com.xlhj.boot.admin" level="info"/> <root level="info"> <appender-ref ref="console"/> <appender-ref ref="file"/> </root> </configuration>
2.4. 主啟動(dòng)類
@SpringBootApplication public class AdminClientApplication { private static Logger logger = LoggerFactory.getLogger(AdminClientApplication.class); public static void main(String[] args) { SpringApplication.run(AdminClientApplication.class, args); logger.info("服務(wù)啟動(dòng)成功,當(dāng)前時(shí)間為:{}", LocalDateTime.now()); logger.error("服務(wù)啟動(dòng)成功,當(dāng)前時(shí)間為:{}", LocalDateTime.now()); } }
3. 驗(yàn)證
分別啟動(dòng)Server和Client服務(wù),在瀏覽器輸入地址:http://localhost:8138/admin
輸入用戶名和密碼:admin/123456,登陸成功后,可以看到如下頁面
點(diǎn)擊該服務(wù)可以進(jìn)入具體的監(jiān)控頁面,注意需要點(diǎn)擊下圖箭頭所指位置
點(diǎn)擊進(jìn)入服務(wù)后,可以看到如下信息
將滾動(dòng)條往下拉,可以看到內(nèi)存、線程等信息
選擇日志—>日志文件可以查看到服務(wù)的日志信息
點(diǎn)擊日志配置可以動(dòng)態(tài)調(diào)節(jié)每個(gè)包/類的日志級別
選擇應(yīng)用墻選項(xiàng),可以查看到應(yīng)用有哪些服務(wù)被監(jiān)控
4. 配置郵件告警
Spring Boot Admin還可以對其監(jiān)控的服務(wù)提供告警功能,當(dāng)出現(xiàn)重大故障,如服務(wù)宕機(jī)時(shí),可以及時(shí)以郵件方式通知運(yùn)維人員
4.1. 引入核心依賴
在Server服務(wù)中引入郵件依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
4.2. application.yml
在yml配置文件中加入如下配置信息
spring: mail: host: smtp.qq.com username: xxxxx@qq.com password: 授權(quán)碼 boot: admin: notify: mail: to: 收件人郵箱 from: 發(fā)件人郵箱
4.3. 通知配置文件
@Configuration public class NotificationConfig { private InstanceRepository instanceRepository; private ObjectProvider<List<Notifier>> provider; public NotificationConfig(InstanceRepository instanceRepository, ObjectProvider<List<Notifier>> provider) { this.instanceRepository = instanceRepository; this.provider = provider; } @Bean public FilteringNotifier filteringNotifier() { CompositeNotifier compositeNotifier = new CompositeNotifier(this.provider.getIfAvailable(Collections::emptyList)); return new FilteringNotifier(compositeNotifier, this.instanceRepository); } @Bean @Primary public RemindingNotifier remindingNotifier() { RemindingNotifier remindingNotifier = new RemindingNotifier(filteringNotifier(), this.instanceRepository); //配置每隔多久提示 remindingNotifier.setReminderPeriod(Duration.ofMinutes(1)); //配置每隔多久檢查 remindingNotifier.setCheckReminderInverval(Duration.ofSeconds(10)); return remindingNotifier; } }
4.4. 驗(yàn)證
先將Client服務(wù)關(guān)閉,稍等一會(huì)便會(huì)收到宕機(jī)提示郵件
再次將Client服務(wù)啟動(dòng)
到此這篇關(guān)于SpringBoot整合Spring Boot Admin實(shí)現(xiàn)服務(wù)監(jiān)控的文章就介紹到這了,更多相關(guān)Spring Boot Admin服務(wù)監(jiān)控內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot-Admin實(shí)現(xiàn)微服務(wù)監(jiān)控+健康檢查+釘釘告警
- 如何用Springboot Admin監(jiān)控你的微服務(wù)應(yīng)用
- 詳解用Spring Boot Admin來監(jiān)控我們的微服務(wù)
- 使用spring-boot-admin對spring-boot服務(wù)進(jìn)行監(jiān)控的實(shí)現(xiàn)方法
- 詳解Spring boot Admin 使用eureka監(jiān)控服務(wù)
- 詳解Spring Boot Admin監(jiān)控服務(wù)上下線郵件通知
- Spring?boot?admin?服務(wù)監(jiān)控利器詳解
相關(guān)文章
Java中ByteArrayInputStream和ByteArrayOutputStream用法詳解
這篇文章主要介紹了Java中ByteArrayInputStream和ByteArrayOutputStream用法詳解,?ByteArrayInputStream?的內(nèi)部額外的定義了一個(gè)計(jì)數(shù)器,它被用來跟蹤?read()?方法要讀取的下一個(gè)字節(jié)2022-06-06Spring MVC創(chuàng)建項(xiàng)目踩過的bug
這篇文章主要介紹了Spring MVC創(chuàng)建項(xiàng)目踩過的bug,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Java常用數(shù)字工具類 大數(shù)乘法、加法、減法運(yùn)算(2)
這篇文章主要為大家詳細(xì)介紹了Java常用數(shù)字工具類,大數(shù)乘法、加法、減法運(yùn)算,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05springboot如何通過session實(shí)現(xiàn)單點(diǎn)登入詳解
單點(diǎn)登錄(SSO)的定義是在多個(gè)應(yīng)用系統(tǒng)中,用戶只需要登錄一次就可以訪問所有相互信任的應(yīng)用系統(tǒng),下面這篇文章主要給大家介紹了關(guān)于springboot如何通過session實(shí)現(xiàn)單點(diǎn)登入的相關(guān)資料,需要的朋友可以參考下2021-12-12Java圖像之自定義角度旋轉(zhuǎn)(實(shí)例)
這篇文章主要介紹了Java圖像之自定義角度旋轉(zhuǎn)(實(shí)例),需要的朋友可以參考下2017-09-09IDEA不識(shí)別Java文件:文件變橙色&顯示后綴名.java的解決
這篇文章主要介紹了IDEA不識(shí)別Java文件:文件變橙色&顯示后綴名.java的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03