Source code address: github.com/muxiaonong/…

The Admin profile

Official documents:What is Spring Boot Admin?

SpringBootAdmin is a community project to manage and monitor SpringBoot microservices, providing monitoring information to servers using client-side registration or Eureka service discovery. Note that the server provides the UI. The actual monitoring information is provided by the client using SpringBootAdmin. Through the interface, you can access the monitoring information required by the entire microservices, such as service health check information, CPU, memory, operating system information, and so on

This article uses SpringBoot 2.3.3.RELEASE, SpringCloud Hoxton.SR6, SpringBoot Admin 2.2.3, and eureka for the service registry

SpringCloud uses SpringBoot Admin

1.1 Create a SpringBoot project named admin-test and import the following dependencies

<! -- Admin service --> <dependency> <groupId>de. Codecentric </groupId> <artifactId> Spring-boot-admin-starter-server </artifactId>  <version>2.21.</version> </dependency> <! <dependency> <groupId> <artifactId> Spring-boot-admin-server-ui </artifactId> <version>2.21.</version>
  </dependency>
Copy the code

1.2 start the class

@SpringBootApplication
@EnableAdminServer
public class AdminTestApplication {

    public static void main(String[] args) { SpringApplication.run(AdminTestApplication.class, args); }}Copy the code

1.3 Configuration File

spring.application.name=admin-test

management.endpoints.jmx.exposure.include=*
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

# spring cloud access&secret config
alibaba.cloud.access-key=****
alibaba.cloud.secret-key=****
Copy the code

1.4 Starting the Project

Enter the project address:http://localhost:8080/applications

2. Configure email notification

2.1 the pom

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
Copy the code

2.2 Email Configuration

Spring.mail.host=smtp.qq.com spring. Mail. The username = pure QQ number spring. Mail. Spring. The password = password mail. The properties. The mail. The SMPT. Auth =true
spring.mail.properties.mail.smpt.starttls.enable=true
spring.mail.properties.mail.smpt.starttls.required=true# receipt mail spring. The boot. Admin. Notify. Mail. To = XXXX@qq. Com # to send a mail spring. The boot. Admin. Notify. Mail. The from = XXXX@qq.com
Copy the code

2.3 QQ Mailbox Settings

Find your own QQ mailbox

QQ mailbox “Settings” account “red box to obtain the authorization code

When we took the consumer service offline,

The next thing we know, we get an email notification that the service is down

Three, send pin group notification

Find the group Settings inside the group “Intelligent Group Assistant” add robots Note: The custom keywords must match the project keywords

Get the Webhook into the project, which you’ll use later

Start the class:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;

@SpringBootApplication
@EnableAdminServer
public class AdminApplication {

	public static void main(String[] args) {
		SpringApplication.run(AdminApplication.class, args);
	}
	   @Bean
	    public DingDingNotifier dingDingNotifier(InstanceRepository repository) {
	        return newDingDingNotifier(repository); }}Copy the code

Notice:

import java.util.Map;

import com.alibaba.fastjson.JSONObject;

import de.codecentric.boot.admin.server.domain.entities.Instance;
import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
import de.codecentric.boot.admin.server.notify.AbstractStatusChangeNotifier;
import reactor.core.publisher.Mono;

public class DingDingNotifier extends AbstractStatusChangeNotifier  {
	public DingDingNotifier(InstanceRepository repository) {
        super(repository);
    }
    @Override
    protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
        String serviceName = instance.getRegistration().getName();
        String serviceUrl = instance.getRegistration().getServiceUrl();
        String status = instance.getStatusInfo().getStatus();
        Map<String, Object> details = instance.getStatusInfo().getDetails();
        StringBuilder str = new StringBuilder();
        str.append("Service Alert: [" + serviceName + "】");
        str.append("[Address]" + serviceUrl);
        str.append("State" + status);
        str.append("[Details]" + JSONObject.toJSONString(details));
        returnMono.fromRunnable(() -> { DingDingMessageUtil.sendTextMessage(str.toString()); }); }}Copy the code

Send utility class

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.alibaba.fastjson.JSONObject;

public class DingDingMessageUtil {
	public static String access_token = "Token";
    public static void sendTextMessage(String msg) {
        try {
            Message message = new Message();
            message.setMsgtype("text");
            message.setText(new MessageInfo(msg));
            URL url = new URL("https://oapi.dingtalk.com/robot/send?access_token=" + access_token);
            // Establish an HTTP connection
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Charset"."UTF-8");
            conn.setRequestProperty("Content-Type"."application/Json; charset=UTF-8");
            conn.connect();
            OutputStream out = conn.getOutputStream();
            String textMessage = JSONObject.toJSONString(message);
            byte[] data = textMessage.getBytes();
            out.write(data);
            out.flush();
            out.close();
            InputStream in = conn.getInputStream();
            byte[] data1 = new byte[in.available()];
            in.read(data1);
            System.out.println(new String(data1));
        } catch(Exception e) { e.printStackTrace(); }}}Copy the code

The message classes:

public class Message {
	private String msgtype;
    private MessageInfo text;
    public String getMsgtype(a) {
        return msgtype;
    }
    public void setMsgtype(String msgtype) {
        this.msgtype = msgtype;
    }
    public MessageInfo getText(a) {
        return text;
    }
    public void setText(MessageInfo text) {
        this.text = text; }}Copy the code
public class MessageInfo {
    private String content;
    public MessageInfo(String content) {
        this.content = content;
    }
    public String getContent(a) {
        return content;
    }
    public void setContent(String content) {
        this.content = content; }}Copy the code

When we take a service offline, we can see a notification of a message sent by a Spike group

Also, when we start the service, a message notifies us that the service is started

Four summarizes

The above is our actual application of admin health examination, in the enterprise + nailing group tend to have SMS notification and email, interested friends can go to try, or quite good play, another is WeChat notice, in the service number template message interested friends can go to study by oneself, everybody refuels ~