客户端启动时检查参数是否为空.

pull/12/head
chen.ma 3 years ago
parent 483e3dd636
commit bbb10ee4c9

@ -1,13 +1,10 @@
package cn.hippo4j.starter.config;
import cn.hippo4j.starter.alarm.NotifyConfig;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.List;
/**
* Bootstrap properties.
*
@ -42,14 +39,4 @@ public class BootstrapProperties {
*/
private boolean banner = true;
/**
* Alarm interval
*/
private Long alarmInterval;
/**
* notifys
*/
private List<NotifyConfig> notifys;
}

@ -1,11 +1,11 @@
package cn.hippo4j.starter.config;
import cn.hutool.core.util.StrUtil;
import cn.hippo4j.common.model.InstanceInfo;
import cn.hippo4j.common.toolkit.ContentUtil;
import cn.hippo4j.starter.core.DiscoveryClient;
import cn.hippo4j.starter.remote.HttpAgent;
import cn.hippo4j.starter.toolkit.inet.InetUtils;
import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.context.annotation.Bean;
@ -34,14 +34,18 @@ public class DiscoveryConfig {
@Bean
@SneakyThrows
public InstanceInfo instanceConfig() {
String namespace = properties.getNamespace();
String itemId = properties.getItemId();
String applicationName = environment.getProperty("spring.application.name");
InstanceInfo instanceInfo = new InstanceInfo();
instanceInfo.setInstanceId(getDefaultInstanceId(environment, inetUtils))
.setIpApplicationName(getIpApplicationName(environment, inetUtils))
.setHostName(InetAddress.getLocalHost().getHostAddress())
.setGroupKey(properties.getItemId() + "+" + properties.getNamespace())
.setAppName(environment.getProperty("spring.application.name"))
.setGroupKey(itemId + "+" + namespace)
.setAppName(applicationName)
.setClientBasePath(environment.getProperty("server.servlet.context-path"))
.setGroupKey(ContentUtil.getGroupKey(properties.getItemId(), properties.getNamespace()));
.setGroupKey(ContentUtil.getGroupKey(itemId, namespace));
String callBackUrl = new StringBuilder().append(instanceInfo.getHostName()).append(":")
.append(environment.getProperty("server.port")).append(instanceInfo.getClientBasePath())

@ -1,13 +1,15 @@
package cn.hippo4j.starter.config;
import cn.hippo4j.common.model.InstanceInfo;
import cn.hippo4j.starter.alarm.*;
import cn.hippo4j.starter.alarm.AlarmControlHandler;
import cn.hippo4j.starter.alarm.BaseSendMessageService;
import cn.hippo4j.starter.alarm.SendMessageHandler;
import cn.hippo4j.starter.alarm.SendMessageService;
import cn.hippo4j.starter.alarm.ding.DingSendMessageHandler;
import cn.hippo4j.starter.alarm.lark.LarkSendMessageHandler;
import cn.hippo4j.starter.alarm.wechat.WeChatSendMessageHandler;
import cn.hippo4j.starter.remote.HttpAgent;
import lombok.AllArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.env.ConfigurableEnvironment;
@ -27,6 +29,8 @@ public class MessageAlarmConfig {
private ConfigurableEnvironment environment;
public static final String ACTIVE_DEFAULT = "unknown";
public static final String SEND_MESSAGE_BEAN_NAME = "hippo4JSendMessageService";
@DependsOn("hippo4JApplicationContextHolder")
@ -37,19 +41,19 @@ public class MessageAlarmConfig {
@Bean
public SendMessageHandler dingSendMessageHandler() {
String active = environment.getProperty("spring.profiles.active", Strings.EMPTY);
String active = environment.getProperty("spring.profiles.active", ACTIVE_DEFAULT);
return new DingSendMessageHandler(active, instanceInfo);
}
@Bean
public SendMessageHandler larkSendMessageHandler() {
String active = environment.getProperty("spring.profiles.active", Strings.EMPTY);
String active = environment.getProperty("spring.profiles.active", ACTIVE_DEFAULT);
return new LarkSendMessageHandler(active, instanceInfo);
}
@Bean
public SendMessageHandler weChatSendMessageHandler() {
String active = environment.getProperty("spring.profiles.active", Strings.EMPTY);
String active = environment.getProperty("spring.profiles.active", ACTIVE_DEFAULT);
return new WeChatSendMessageHandler(active, instanceInfo);
}

@ -0,0 +1,19 @@
package cn.hippo4j.starter.core;
import org.springframework.boot.diagnostics.AbstractFailureAnalyzer;
import org.springframework.boot.diagnostics.FailureAnalysis;
/**
* Config empty analyzer.
*
* @author chen.ma
* @date 2021/11/28 21:59
*/
public class ConfigEmptyAnalyzer extends AbstractFailureAnalyzer<ConfigEmptyException> {
@Override
protected FailureAnalysis analyze(Throwable rootFailure, ConfigEmptyException cause) {
return new FailureAnalysis(cause.getDescription(), cause.getAction(), cause);
}
}

@ -0,0 +1,20 @@
package cn.hippo4j.starter.core;
import lombok.AllArgsConstructor;
import lombok.Data;
/**
* Config empty exception.
*
* @author chen.ma
* @date 2021/11/28 21:58
*/
@Data
@AllArgsConstructor
public class ConfigEmptyException extends RuntimeException {
private String description;
private String action;
}

@ -0,0 +1,62 @@
package cn.hippo4j.starter.enable;
import cn.hippo4j.starter.config.BootstrapProperties;
import cn.hippo4j.starter.core.ConfigEmptyException;
import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
/**
* Before check configuration.
*
* @author chen.ma
* @date 2021/11/28 22:44
*/
@AllArgsConstructor
@Configuration(proxyBeanMethods = false)
public class BeforeCheckConfiguration {
@Bean
public BeforeCheckConfiguration.BeforeCheck dynamicThreadPoolBeforeCheckBean(BootstrapProperties properties, ConfigurableEnvironment environment) {
String namespace = properties.getNamespace();
if (StrUtil.isBlank(namespace)) {
throw new ConfigEmptyException(
"Web server failed to start. The dynamic thread pool namespace is empty.",
"Please check whether the [spring.dynamic.thread-pool.namespace] configuration is empty or an empty string."
);
}
String itemId = properties.getItemId();
if (StrUtil.isBlank(itemId)) {
throw new ConfigEmptyException(
"Web server failed to start. The dynamic thread pool item id is empty.",
"Please check whether the [spring.dynamic.thread-pool.item-id] configuration is empty or an empty string."
);
}
String serverAddr = properties.getServerAddr();
if (StrUtil.isBlank(serverAddr)) {
throw new ConfigEmptyException(
"Web server failed to start. The dynamic thread pool server addr is empty.",
"Please check whether the [spring.dynamic.thread-pool.server-addr] configuration is empty or an empty string."
);
}
String applicationName = environment.getProperty("spring.application.name");
if (StrUtil.isBlank(applicationName)) {
throw new ConfigEmptyException(
"Web server failed to start. The dynamic thread pool application name is empty.",
"Please check whether the [spring.application.name] configuration is empty or an empty string."
);
}
return new BeforeCheckConfiguration.BeforeCheck();
}
public class BeforeCheck {
}
}

@ -13,7 +13,7 @@ import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(MarkerConfiguration.class)
@Import({BeforeCheckConfiguration.class, MarkerConfiguration.class})
public @interface EnableDynamicThreadPool {
}

@ -58,9 +58,6 @@ public class ServerListManager {
}
Iterator<String> iterator() {
if (serverUrls.isEmpty()) {
log.error("[iterator-serverlist] No server address defined!");
}
return new ServerAddressIterator(serverUrls);
}

@ -1,2 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.hippo4j.starter.config.DynamicThreadPoolAutoConfiguration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.hippo4j.starter.config.DynamicThreadPoolAutoConfiguration
org.springframework.boot.diagnostics.FailureAnalyzer=cn.hippo4j.starter.core.ConfigEmptyAnalyzer

Loading…
Cancel
Save