springBoot-企业微信告警

SpringBoot对接企业微信Api,封装Token接口,以及发送文本消息接口,实现自动告警功能

当遇到中文乱码时,如果请求方式没有问题的话,将Message对象转化,new String(message.getBody(), StandardCharsets.UTF_8);
Dto,根据企业微信api官方文档进行编写

public class WxTokenDto {
    private int errcode;
    private String errmsg;
    // 获取到的凭证
    private String access_token;
    // 凭证有效时间,单位:秒
    private int expires_in;
}
@Data
public class WxMessageDto {
    private String errcode;
    private String errmsg;
    private String invaliduser;
    private String invalidparty;
    private String invalidtag;

}
接口
public interface WeChatService {
    /**
     * 获取微信access_token
     * @return
     */
    WxTokenDto getWeChatAccessTokenUrl();

    /**
     * 发送消息
     * @return
     */
    WxMessageDto sendMessage(String token, WxTextMessage message);

}
获取Token实现类
  public WxTokenDto getWeChatAccessTokenUrl() {
        WxTokenDto tokenDto = null;
        StringBuilder builder=new StringBuilder();
        builder.append(accessTokenUrl).append("corpid=").append(id).append("&").append("corpsecret=").append(secret);
        String url = builder.toString();
        log.info("weChat access_token url:" + url);
        try {
            tokenDto = restTemplate.getForObject(url, WxTokenDto.class);
            log.info("WxTokenDto :" +tokenDto);
            if(tokenDto.getErrcode()!=0){
                log.warn("call WX API access_token fail,errmsg: "+tokenDto.getErrmsg());
            }
        } catch (RuntimeException ex) {
            log.warn("getCallWXApiGetAccessToken>>>" + ex,ex);
            throw new WxRetryException(ex.getMessage(),url);
        } catch (Exception e) {
            log.warn("getCallWXApiGetAccessToken>>>" + e, e);
        }
        return tokenDto;
    }
发送消息实现类
    public WxMessageDto sendMessage(String token, WxTextMessage message) {
        StringBuilder builder=new StringBuilder();
        builder.append(sendMessageUrl).append(token);
        String url = builder.toString();
        log.info("weChat sendMessage url:" + url);
        WxMessageDto wxMessageDto =null;
        try{
             HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
            Gson gson=new Gson();
            String json = gson.toJson(message);
            HttpEntity<String> entity = new HttpEntity<String>(json,headers)
            wxMessageDto = restTemplate.postForObject(url, entity, WxMessageDto.class);
            log.info("WxMessageDto :" +wxMessageDto);
        }catch (RuntimeException ex) {
            log.warn("getCallWXApiSendMessage>>>" + ex,ex);
            throw new WxRetryException(ex.getMessage(),url);
        } catch (Exception e) {
            log.warn("getCallWXApiSendMessage>>>" + e, e);
        }
        return wxMessageDto;
    }
    public static void setRestTemplateEncode(RestTemplate restTemplate) {
        if (null == restTemplate || ObjectUtils.isEmpty(restTemplate.getMessageConverters())) {
            return;
        }
        List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
        messageConverters.forEach(messageConverter->{
            if(messageConverter instanceof StringHttpMessageConverter){
                ((StringHttpMessageConverter) messageConverter).setDefaultCharset(StandardCharsets.UTF_8);
            }
        });
    }
    }

告警实现

使用RabbitMQ,在要告警的地方,将消息推进queue中,实现一个监听器,监听到消息,调用获取token接口获取token,再将token传入发送消息接口,实现消息告警

你可能感兴趣的:(java,spring,boot)