Spring Boot Admin integrates custom monitoring alarms

Source: Internet
Author: User
Tags getmessage webhook

Spring Boot Admin Integrated custom Monitoring alarm Preamble

Spring Boot Admin is a community project that can be used to monitor and manage Spring boot applications and provide a UI that can be consulted in detail in official documentation.

Spring Boot Admin itself provides monitoring alarm function, but the default only provides the integration of Hipchat, Slack and other popular foreign communication software, although there are mail notifications, but considering the use of physical examination to determine two times the development of increased nail notification.

This article is based on the latest version of Spring Boot Admin 1.5.7.

Preparatory work
    1. Spring Boot Admin Server, reference document http://codecentric.github.io/spring-boot-admin/1.5.7/#getting-started
    2. Nail customization robot, reference document https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.64ddtm&treeid=257& Articleid=105735&doctype=1
Refer to the source of your own notification

Because the official document does not add the custom notification related documents, so we refer to the Slack notification source SlackNotifier.java .

The source code is long not all pasted, look at the key part:

public class SlackNotifier extends AbstractStatusChangeNotifier
protected void doNotify(ClientApplicationEvent event) throws Exception {    this.restTemplate.postForEntity(this.webhookUrl, this.createMessage(event), Void.class);}

Can see the process is still relatively simple, inherit the Abstractstatuschangenotifier class, implement the Donotify method, when the application state changes will be callback Donotify method.

Implementation of nail-nail notification

Dingtalknotifier.java

public class Dingtalknotifier extends Abstractstatuschangenotifier {private final spelexpressionparser parser = new Sp    Elexpressionparser ();    Private Resttemplate resttemplate = new Resttemplate ();    Private String Webhooktoken;    Private String atmobiles;    Private String Msgtype = "Markdown";    Private String title = "Service Alarm";    Private Expression message;  Public Dingtalknotifier () {this.message = This.parser.parseExpression ("**#{application.name}** (#{application.id})    Is **#{to.status}** ", parsercontext.template_expression); } @Override protected void Donotify (Clientapplicationevent event) {this.restTemplate.postForEntity (this.webh    Ooktoken, This.createmessage (event), Void.class); } private Httpentity<map<string, object>> createmessage (Clientapplicationevent event) {Map<Strin        G, object> Messagejson = new hashmap<> ();        hashmap<string, string> params = new hashmap<> (); Params.put ("text", THis.getmessage (event));        Params.put ("title", This.title);        Messagejson.put ("Dinggroup", This.dinggroup);        Messagejson.put ("Atmobiles", this.atmobiles);        Messagejson.put ("Msgtype", This.msgtype);        Messagejson.put (This.msgtype, params);        Httpheaders headers = new Httpheaders ();        Headers.setcontenttype (Mediatype.application_json_utf8);    return new httpentity<> (Messagejson, headers);        } Private String Getatmobilesstring (string s) {StringBuilder atmobiles = new StringBuilder ();        string[] mobiles = S.split (",");        for (String mobile:mobiles) {atmobiles.append ("@"). Append (mobile);    } return atmobiles.tostring (); } private String GetMessage (Clientapplicationevent event) {return this.atmobiles = = null? this.message.getValu E (event, String.class): This.message.getValue (event, String.class) + "\ n >" + this.getatmobilesstring (    This.atmobiles); } public void SetresttemplatE (resttemplate resttemplate) {this.resttemplate = resttemplate;    } public String Getwebhooktoken () {return webhooktoken;    } public void Setwebhooktoken (String webhooktoken) {this.webhooktoken = Webhooktoken;    } public String Getatmobiles () {return atmobiles;    } public void Setatmobiles (String atmobiles) {this.atmobiles = Atmobiles;    } public String Getmsgtype () {return msgtype;    } public void Setmsgtype (String msgtype) {this.msgtype = Msgtype;    } public Expression GetMessage () {return message; The public void Setmessage (String message) {this.message = this.parser.parseExpression (Message, Parsercontext.tem    Plate_expression);    } public String GetTitle () {return title;    public void Settitle (String title) {this.title = title; }}

The code logic is also relatively simple to explain.

Increase nail Notification Auto-configuration

Dingtalknotifierconfiguration.java

@Configuration@ConditionalOnProperty(        prefix = "spring.boot.admin.notify.dingtalk",        name = {"webhook-token"})@AutoConfigureBefore({NotifierConfiguration.NotifierListenerConfiguration.class, NotifierConfiguration.CompositeNotifierConfiguration.class})public class DingTalkNotifierConfiguration {    public DingTalkNotifierConfiguration() {    }    @Bean    @ConditionalOnMissingBean    @ConfigurationProperties(prefix = "spring.boot.admin.notify.dingtalk")    public DingTalkNotifier dingTalkNotifier() {        return new DingTalkNotifier();    }}

Probably explains the main role of this configuration class:

    1. spring.boot.admin.notify.dingtalk.webhook-tokenThis configuration class takes effect when configured.
    2. The spring.boot.admin.notify.dingtalk next configuration is injected into the DingTalkNotifier generated Bean.
    3. Specifies the time that this configuration will take effect and the condition that the Bean is in effect.

The key is several annotations on classes and beans, but this is not the focus of this article.

Add Related Configuration
spring:  boot:    admin:      notify:        dingtalk:          enabled: true          webhook-token: https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxx

Then when the project status changes, you can receive a message on the spike.

Spring Boot Admin integrates custom monitoring alarms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.