Skip to content

Commit

Permalink
Merge pull request #414 from dromara/dev
Browse files Browse the repository at this point in the history
[ISSUE #395] Simplify notifiy Webhook and [ISSUE #400] low version dubbo bug
  • Loading branch information
yanhom1314 authored Mar 8, 2024
2 parents 00b5291 + b86639b commit efd70c3
Show file tree
Hide file tree
Showing 15 changed files with 359 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,24 @@

import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.Version;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.store.DataStore;
import org.apache.dubbo.common.threadpool.manager.DefaultExecutorRepository;
import org.apache.dubbo.common.threadpool.manager.ExecutorRepository;
import org.apache.dubbo.common.threadpool.support.eager.EagerThreadPoolExecutor;
import org.apache.dubbo.config.spring.context.event.ServiceBeanExportedEvent;
import org.apache.dubbo.remoting.transport.dispatcher.WrappedChannelHandler;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.dromara.dynamictp.adapter.common.AbstractDtpAdapter;
import org.dromara.dynamictp.common.properties.DtpProperties;
import org.dromara.dynamictp.common.spring.ApplicationContextHolder;
import org.dromara.dynamictp.common.util.ReflectionUtil;
import org.dromara.dynamictp.core.support.ThreadPoolExecutorProxy;
import org.dromara.dynamictp.jvmti.JVMTI;
import org.springframework.context.ApplicationEvent;

import java.util.Map;
Expand All @@ -42,6 +46,9 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;

import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER_SIDE;
import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY;

/**
* ApacheDubboDtpAdapter related
*
Expand All @@ -56,6 +63,8 @@ public class ApacheDubboDtpAdapter extends AbstractDtpAdapter {

private static final String EXECUTOR_SERVICE_COMPONENT_KEY = ExecutorService.class.getName();

private static final String EXECUTOR_FIELD = "executor";

@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ServiceBeanExportedEvent) {
Expand Down Expand Up @@ -85,16 +94,26 @@ protected void initialize() {
String currVersion = Version.getVersion();
if (DubboVersion.compare(DubboVersion.VERSION_2_7_5, currVersion) > 0) {
// 当前dubbo版本 < 2.7.5
DataStore dataStore = ExtensionLoader.getExtensionLoader(DataStore.class).getDefaultExtension();
if (Objects.isNull(dataStore)) {
return;
}
Map<String, Object> executorMap = dataStore.get(EXECUTOR_SERVICE_COMPONENT_KEY);
if (MapUtils.isNotEmpty(executorMap)) {
executorMap.forEach((k, v) -> {
ThreadPoolExecutor proxy = getProxy((ThreadPoolExecutor) v);
dataStore.put(EXECUTOR_SERVICE_COMPONENT_KEY, k, proxy);
putAndFinalize(genTpName(k), (ExecutorService) v, proxy);
val handlers = JVMTI.getInstances(WrappedChannelHandler.class);
if (CollectionUtils.isNotEmpty(handlers)) {
DataStore dataStore = ExtensionLoader.getExtensionLoader(DataStore.class).getDefaultExtension();
handlers.forEach(handler -> {
//获取WrappedChannelHandler中的原始线程池
val originExecutor = ReflectionUtil.getFieldValue(EXECUTOR_FIELD, handler);
if (originExecutor instanceof ThreadPoolExecutor) {
URL url = handler.getUrl();
//低版本跳过消费者线程池配置
if (!CONSUMER_SIDE.equalsIgnoreCase(url.getParameter(SIDE_KEY))) {
String port = String.valueOf(url.getPort());
String tpName = genTpName(port);
//增强原始线程池,替换为动态线程池代理
enhanceOriginExecutor(tpName, (ThreadPoolExecutor) originExecutor, EXECUTOR_FIELD, handler);
//获取增强后的新动态线程池
Object newExexutor = ReflectionUtil.getFieldValue(EXECUTOR_FIELD, handler);
//替换dataStore中的线程池
dataStore.put(EXECUTOR_SERVICE_COMPONENT_KEY, port, newExexutor);
}
}
});
}
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ public final class DingNotifyConst {

private DingNotifyConst() { }

public static final String DING_WEBHOOK = "https://oapi.dingtalk.com/robot/send?access_token=";
public static final String DING_WEBHOOK = "https://oapi.dingtalk.com/robot/send";

public static final String ACCESS_TOKEN_PARAM = "access_token";

public static final String TIMESTAMP_PARAM = "timestamp";

public static final String SIGN_PARAM = "sign";

public static final String WARNING_COLOR = "#EA9F00";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private LarkNotifyConst() { }
/**
* lark bot url
*/
public static final String LARK_WEBHOOK = "https://open.feishu.cn/open-apis/bot/v2/hook/";
public static final String LARK_WEBHOOK = "https://open.feishu.cn/open-apis/bot/v2/hook";

/**
* lark at format. openid
Expand All @@ -56,9 +56,9 @@ private LarkNotifyConst() { }

public static final String COMMENT_COLOR = "";

public static final String SIGN_REPLACE = "{";
public static final String SIGN_REPLACE = "\\{";

public static final String SIGN_PARAM = SIGN_REPLACE + "\"timestamp\": \"%s\",\"sign\": \"%s\",";
public static final String SIGN_PARAM_PREFIX = "{\"timestamp\": \"%s\",\"sign\": \"%s\",";

/**
* lark alarm json str
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public final class WechatNotifyConst {

private WechatNotifyConst() { }

public static final String WECHAT_WEB_HOOK = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=";
public static final String WECHAT_WEB_HOOK = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send";

public static final String KEY_PARAM = "key";

public static final String WARNING_COLOR = "warning";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ public class NotifyPlatform {
private String secret;

/**
* webHook, may be null.
* webhook, may be null.
*/
private String webHook;
private String webhook;

/**
* Receivers, split by ,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.dromara.dynamictp.common.notifier;

import cn.hutool.core.net.url.UrlBuilder;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
Expand Down Expand Up @@ -73,24 +74,29 @@ protected String buildMsgBody(NotifyPlatform platform, String content) {

@Override
protected String buildUrl(NotifyPlatform platform) {
String webHook = Optional.ofNullable(platform.getWebHook()).orElse(DingNotifyConst.DING_WEBHOOK);
return getTargetUrl(platform.getSecret(), platform.getUrlKey(), webHook);
String webhook = Optional.ofNullable(platform.getWebhook()).orElse(DingNotifyConst.DING_WEBHOOK);
return getTargetUrl(platform.getSecret(), platform.getUrlKey(), webhook);
}

/**
* Build target url.
*
* @param secret secret
* @param accessToken accessToken
* @param webHook webHook
* @param webhook webhook
* @return url
*/
private String getTargetUrl(String secret, String accessToken, String webHook) {
if (StringUtils.isBlank(secret)) {
return webHook + accessToken;
private String getTargetUrl(String secret, String accessToken, String webhook) {
UrlBuilder builder = UrlBuilder.of(webhook);
if (StringUtils.isNotBlank(accessToken) && StringUtils.isBlank(builder.getQuery().get(DingNotifyConst.ACCESS_TOKEN_PARAM))) {
builder.addQuery(DingNotifyConst.ACCESS_TOKEN_PARAM, accessToken);
}
long timestamp = System.currentTimeMillis();
String sign = DingSignUtil.dingSign(secret, timestamp);
return webHook + accessToken + "&timestamp=" + timestamp + "&sign=" + sign;
if (StringUtils.isNotBlank(secret)) {
long timestamp = System.currentTimeMillis();
builder.addQuery(DingNotifyConst.TIMESTAMP_PARAM, timestamp);
builder.addQuery(DingNotifyConst.SIGN_PARAM, DingSignUtil.dingSign(secret, timestamp));
}
return builder.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.dromara.dynamictp.common.notifier;

import cn.hutool.core.net.url.UrlBuilder;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.commons.codec.binary.Base64;
Expand All @@ -30,9 +31,11 @@
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import static org.dromara.dynamictp.common.constant.LarkNotifyConst.SIGN_PARAM;
import static org.dromara.dynamictp.common.constant.LarkNotifyConst.SIGN_PARAM_PREFIX;
import static org.dromara.dynamictp.common.constant.LarkNotifyConst.SIGN_REPLACE;

/**
Expand Down Expand Up @@ -85,7 +88,7 @@ protected String buildMsgBody(NotifyPlatform platform, String content) {
try {
val secondsTimestamp = System.currentTimeMillis() / 1000;
val sign = genSign(platform.getSecret(), secondsTimestamp);
content = content.replace(SIGN_REPLACE, String.format(SIGN_PARAM, secondsTimestamp, sign));
content = content.replaceFirst(SIGN_REPLACE, String.format(SIGN_PARAM_PREFIX, secondsTimestamp, sign));
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
log.error("DynamicTp notify, lark generate signature failed...", e);
}
Expand All @@ -94,7 +97,15 @@ protected String buildMsgBody(NotifyPlatform platform, String content) {

@Override
protected String buildUrl(NotifyPlatform platform) {
String webHook = Optional.ofNullable(platform.getWebHook()).orElse(LarkNotifyConst.LARK_WEBHOOK);
return webHook + platform.getUrlKey();
if (StringUtils.isBlank(platform.getUrlKey())) {
return platform.getWebhook();
}
UrlBuilder builder = UrlBuilder.of(Optional.ofNullable(platform.getWebhook()).orElse(LarkNotifyConst.LARK_WEBHOOK));
List<String> segments = builder.getPath().getSegments();
if (StringUtils.isNotBlank(platform.getUrlKey()) && !Objects.equals(platform.getUrlKey(), segments.get(segments.size() - 1))) {
builder.addPath(platform.getUrlKey());
}
return builder.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

package org.dromara.dynamictp.common.notifier;

import cn.hutool.core.net.url.UrlBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.dromara.dynamictp.common.constant.WechatNotifyConst;
import org.dromara.dynamictp.common.em.NotifyPlatformEnum;
import org.dromara.dynamictp.common.entity.MarkdownReq;
Expand Down Expand Up @@ -52,7 +54,14 @@ protected String buildMsgBody(NotifyPlatform platform, String content) {

@Override
protected String buildUrl(NotifyPlatform platform) {
String webHook = Optional.ofNullable(platform.getWebHook()).orElse(WechatNotifyConst.WECHAT_WEB_HOOK);
return webHook + platform.getUrlKey();
if (StringUtils.isBlank(platform.getUrlKey())) {
return platform.getWebhook();
}
UrlBuilder builder = UrlBuilder.of(Optional.ofNullable(platform.getWebhook()).orElse(WechatNotifyConst.WECHAT_WEB_HOOK));
if (StringUtils.isNotBlank(platform.getUrlKey()) && StringUtils.isBlank(builder.getQuery().get(WechatNotifyConst.KEY_PARAM))) {
builder.addQuery(WechatNotifyConst.KEY_PARAM, platform.getUrlKey());
}
return builder.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

package org.dromara.dynamictp.extension.notify.yunzhijia;

import cn.hutool.core.net.url.UrlBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.dromara.dynamictp.common.entity.NotifyPlatform;
import org.dromara.dynamictp.common.util.JsonUtil;
import org.dromara.dynamictp.common.notifier.AbstractHttpNotifier;
import org.dromara.dynamictp.common.util.JsonUtil;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -50,7 +52,16 @@ protected String buildMsgBody(NotifyPlatform platform, String content) {

@Override
protected String buildUrl(NotifyPlatform platform) {
String webHook = Optional.ofNullable(platform.getWebHook()).orElse(YunZhiJiaNotifyConst.WEB_HOOK);
return webHook + platform.getUrlKey();
if (StringUtils.isBlank(platform.getUrlKey())) {
return platform.getWebhook();
}
UrlBuilder builder = UrlBuilder.of(Optional.ofNullable(platform.getWebhook()).orElse(YunZhiJiaNotifyConst.WEB_HOOK));
if (StringUtils.isBlank(builder.getQuery().get(YunZhiJiaNotifyConst.YZJ_TYPE_PARAM))) {
builder.addQuery(YunZhiJiaNotifyConst.YZJ_TYPE_PARAM, 0);
}
if (StringUtils.isNotBlank(platform.getUrlKey()) && StringUtils.isBlank(builder.getQuery().get(YunZhiJiaNotifyConst.YZJ_TOKEN_PARAM))) {
builder.addQuery(YunZhiJiaNotifyConst.YZJ_TOKEN_PARAM, platform.getUrlKey());
}
return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ public final class YunZhiJiaNotifyConst {
private YunZhiJiaNotifyConst() {
}

public static final String WEB_HOOK = "https://www.yunzhijia.com/gateway/robot/webhook/send?yzjtype=0&yzjtoken=";
public static final String WEB_HOOK = "https://www.yunzhijia.com/gateway/robot/webhook/send";

public static final String YZJ_TYPE_PARAM = "yzjtype";

public static final String YZJ_TOKEN_PARAM = "yzjtoken";

public static final String WARNING_COLOR = "warning";

Expand Down
4 changes: 4 additions & 0 deletions test/test-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,9 @@
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>org.dromara.dynamictp</groupId>
<artifactId>dynamic-tp-extension-notify-yunzhijia</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.dynamictp.test.common.notifier;

import org.dromara.dynamictp.common.entity.NotifyPlatform;
import org.dromara.dynamictp.common.notifier.DingNotifier;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
* @author <a href = "mailto:kamtohung@gmail.com">KamTo Hung</a>
*/
public class DingNotifierTest {

@Test
public void buildUrlWithWebhook() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
NotifyPlatform notifyPlatform = new NotifyPlatform();
notifyPlatform.setWebhook("https://oapi.dingtalk.com/robot/send?access_token=123");
// notifyPlatform.setUrlKey("123");
// notifyPlatform.setSecret("456");
DingNotifier dingNotifier = new DingNotifier();
Method privateMethod = DingNotifier.class.getDeclaredMethod("getTargetUrl", String.class, String.class, String.class);
privateMethod.setAccessible(true);
String result = (String) privateMethod.invoke(dingNotifier, notifyPlatform.getSecret(), notifyPlatform.getUrlKey(), notifyPlatform.getWebhook());
Assertions.assertEquals("https://oapi.dingtalk.com/robot/send?access_token=123", result);
}

@Test
public void buildUrlWithUrlKey() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
NotifyPlatform notifyPlatform = new NotifyPlatform();
notifyPlatform.setWebhook("https://oapi.dingtalk.com/robot/send?access_token=123");
notifyPlatform.setUrlKey("123");
// notifyPlatform.setSecret("456");
DingNotifier dingNotifier = new DingNotifier();
Method privateMethod = DingNotifier.class.getDeclaredMethod("getTargetUrl", String.class, String.class, String.class);
privateMethod.setAccessible(true);
String result = (String) privateMethod.invoke(dingNotifier, notifyPlatform.getSecret(), notifyPlatform.getUrlKey(), notifyPlatform.getWebhook());
Assertions.assertEquals("https://oapi.dingtalk.com/robot/send?access_token=123", result);
}


@Test
public void buildUrlWithSecret() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
NotifyPlatform notifyPlatform = new NotifyPlatform();
notifyPlatform.setWebhook("https://oapi.dingtalk.com/robot/send?access_token=123");
// notifyPlatform.setUrlKey("123");
notifyPlatform.setSecret("456");
DingNotifier dingNotifier = new DingNotifier();
Method privateMethod = DingNotifier.class.getDeclaredMethod("getTargetUrl", String.class, String.class, String.class);
privateMethod.setAccessible(true);
String result = (String) privateMethod.invoke(dingNotifier, notifyPlatform.getSecret(), notifyPlatform.getUrlKey(), notifyPlatform.getWebhook());
System.out.println(result);
Assertions.assertTrue(result.contains("timestamp="));
Assertions.assertTrue(result.contains("sign="));
}

}
Loading

0 comments on commit efd70c3

Please sign in to comment.