目录
整体图
springboot版本:
springbootadmin版本:
整合了日志、钉钉推送、
admin服务端
pom.xml
application.yml
java相关类
启动类
admin增加账号密码,security相关类
钉钉预警相关类,几个类放到一起
验证:http://localhost:9100
client客户端
pom.xml
application.yml
钉钉机器人webhook
参考:ruoyi的文档
2.2.1.RELEASE
2.2.1
de.codecentric
spring-boot-admin-starter-server
${spring-boot-admin.version}
org.springframework.boot
spring-boot-starter-security
org.springframework.boot
spring-boot-starter-web
org.apache.httpcomponents
httpclient
4.5.13
spring:
application:
name: homelife-monitor
security:
user:
name: admin
password: admin123
boot:
admin:
ui:
title: 员工APP
# 开发环境配置
server:
# 服务器的HTTP端口,默认为8080
port: 9100
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
logfile:
enabled: true
health:
show-details: always
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableAdminServer
@SpringBootApplication
public class MonitorApp
{
public static void main(String[] args)
{
SpringApplication.run(MonitorApp.class, args);
System.out.println("(♥◠‿◠)ノ゙ 监控中心启动成功 ლ(´ڡ`ლ)゙ \n" +
" .-------. ____ __ \n" +
" | _ _ \\ \\ \\ / / \n" +
" | ( ' ) | \\ _. / ' \n" +
" |(_ o _) / _( )_ .' \n" +
" | (_,_).' __ ___(_ o _)' \n" +
" | |\\ \\ | || |(_,_)' \n" +
" | | \\ `' /| `-' / \n" +
" | | \\ / \\ / \n" +
" ''-' `'-' `-..-' ");
}
}
/**
* 监控权限配置
*
* @author ruoyi
*/
@Configuration
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter
{
private final String adminContextPath;
public WebSecurityConfigurer(AdminServerProperties adminServerProperties)
{
this.adminContextPath = adminServerProperties.getContextPath();
}
@Override
protected void configure(HttpSecurity http) throws Exception
{
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(adminContextPath + "/");
http
.headers().frameOptions().disable()
.and().authorizeRequests()
.antMatchers(adminContextPath + "/assets/**"
, adminContextPath + "/login"
, adminContextPath + "/actuator/**"
, adminContextPath + "/instances/**"
).permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage(adminContextPath + "/login")
.successHandler(successHandler).and()
.logout().logoutUrl(adminContextPath + "/logout")
.and()
.httpBasic().and()
.csrf()
.disable();
}
}
//通知回调类
import java.util.Map;
@Component
public class DingDingNotifier extends AbstractStatusChangeNotifier {
public DingDingNotifier(InstanceRepository repository) {
super(repository);
}
@Override
protected Mono doNotify(InstanceEvent event, Instance instance) {
String serviceName = instance.getRegistration().getName();
String serviceUrl = instance.getRegistration().getServiceUrl();
String getStatus = instance.getStatusInfo().getStatus();
Map details = instance.getStatusInfo().getDetails();
StringBuilder str = new StringBuilder();
/**\n前后必须两个空格**/
str.append("- **应用名称** : 【" + serviceName + "】 \n ");
// str.append(" 监控报警 : 【" + serviceName + "】 ");
str.append("- **服务地址** :" + serviceUrl + " \n ");
str.append("- **应用状态** :" + getStatus + " \n ");
str.append("- **应用详情** :" + JSONObject.toJSONString(details));
Markdown m = new Markdown();
m.setTitle("后台监控");
m.setText(str.toString());
Robot robot = new Robot();
robot.setMarkdown(m);
return Mono.fromRunnable(() -> {
// DingDingMessageUtil.sendTextMessage(str.toString());
RobotSendUtils.send(robot, "https://oapi.dingtalk.com/robot/send?access_token=de4b9f59f517ed887dc966a549c5a59dc7b4f27edc1d960040");
});
}
}
// httpclient发送类和实体信息
public class RobotSendUtils {
public static void send(Robot robot, String WEBHOOK){
//创建HttpClient对象
HttpClient httpClient = HttpClients.createDefault();
// 创建请求方法的实例
HttpPost httpPost = new HttpPost(WEBHOOK);
//请求参数
httpPost.addHeader("Content-Type", "application/json; charset=utf-8");
HttpEntity httpEntity = new StringEntity(JSON.toJSONString(robot),"utf-8");
httpPost.setEntity(httpEntity);
HttpResponse response;
String result;
try {
response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
result = EntityUtils.toString(response.getEntity(), "utf-8");
System.out.println(result);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
-------------
import lombok.Data;
@Data
public class Robot {
private String msgtype = "markdown";//默认markdown
private Markdown markdown;//信息
private At at;//@的人
}
----------------
import lombok.Data;
@Data
public class Markdown {
private String title;//标题
private String text;//内容
}
----------------
import java.util.List;
public class At {
private List atMobiles;//@的人
private Boolean isAtAll = false;//默认不@all
}
de.codecentric
spring-boot-admin-starter-client
${spring-boot-admin.version}
org.springframework.boot
spring-boot-starter-actuator
spring:
application:
name: admin-client
boot:
admin:
client:
url: http://localhost:9100
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
logfile:
enabled: true
external-file: ${user.home}/homelife-start/logs/application.log
health:
show-details: always
这个可以取百度一下
http://doc.ruoyi.vip/ruoyi-cloud/cloud/monitor.html#%E6%95%B4%E5%90%88admin-ui
https://blog.csdn.net/ron03129596/article/details/109127704?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_title~default-4.control&spm=1001.2101.3001.4242