Code format optimization

pull/805/head
chen.ma 2 years ago
parent a2f02ad9db
commit e2ece1fda0

@ -22,6 +22,7 @@ import cn.hippo4j.common.model.WebIpAndPortInfo;
import cn.hippo4j.common.toolkit.Assert;
import cn.hippo4j.common.toolkit.StringUtil;
import cn.hippo4j.core.toolkit.inet.InetUtils;
import lombok.NoArgsConstructor;
import org.springframework.boot.web.server.WebServer;
import java.util.Arrays;
@ -29,37 +30,35 @@ import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
/**
* Ip and port Holder
* Ip and port holder.
*/
@NoArgsConstructor(access = lombok.AccessLevel.PRIVATE)
public class WebIpAndPortHolder {
private static boolean support = false;
private static boolean SUPPORT_VERSION = false;
static {
try {
Class.forName("org.springframework.boot.web.server.WebServer");
support = true;
} catch (Exception e) {
SUPPORT_VERSION = true;
} catch (Exception ignored) {
}
}
/**
* Application ip and application post
*/
protected static AtomicReference<WebIpAndPortInfo> webIpAndPort = new AtomicReference<>();
protected static AtomicReference<WebIpAndPortInfo> WEB_IP_AND_PORT = new AtomicReference<>();
public static final String ALL = "*";
protected static final String SEPARATOR = ",";
private WebIpAndPortHolder() {
}
protected static void initIpAndPort() {
if (!support) {
if (!SUPPORT_VERSION) {
return;
}
webIpAndPort.compareAndSet(null, getWebIpAndPortInfo());
WEB_IP_AND_PORT.compareAndSet(null, getWebIpAndPortInfo());
}
private static WebIpAndPortInfo getWebIpAndPortInfo() {
@ -76,19 +75,19 @@ public class WebIpAndPortHolder {
}
/**
* get WebIpAndPortInfo, If it is null, initialize it
* get WebIpAndPortInfo, If it is null, initialize it.
*
* @return WebIpAndPortInfo
* @return Web ip and port info
*/
public static WebIpAndPortInfo getWebIpAndPort() {
if (webIpAndPort.get() == null) {
if (WEB_IP_AND_PORT.get() == null) {
initIpAndPort();
}
return WebIpAndPortHolder.webIpAndPort.get();
return WebIpAndPortHolder.WEB_IP_AND_PORT.get();
}
/**
* Check the new properties and instance IP and port
* Check the new properties and instance IP and port.
*
* @param nodes nodes in properties
* @return Whether it meets the conditions
@ -105,5 +104,4 @@ public class WebIpAndPortHolder {
.filter(Objects::nonNull)
.anyMatch(each -> each.check(webIpAndPort.getIpSegment(), webIpAndPort.getPort()));
}
}

@ -49,7 +49,6 @@
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

@ -7,7 +7,7 @@
<artifactId>hippo4j-example</artifactId>
<version>${revision}</version>
</parent>
<artifactId>hippo4j-config-apollo-spring-boot-1.5-starter-example</artifactId>
<artifactId>hippo4j-config-apollo-spring-boot-1x-starter-example</artifactId>
<properties>
<maven.deploy.skip>true</maven.deploy.skip>
@ -37,15 +37,9 @@
<version>${revision}</version>
</dependency>
<!-- <dependency>
<groupId>cn.hippo4j</groupId>
<artifactId>hippo4j-config-spring-boot-starter</artifactId>
<version>${revision}</version>
</dependency>
-->
<dependency>
<groupId>cn.hippo4j</groupId>
<artifactId>hippo4j-config-spring-boot-1.5-starter</artifactId>
<artifactId>hippo4j-config-spring-boot-1x-starter</artifactId>
<version>${revision}</version>
</dependency>

@ -23,9 +23,9 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDynamicThreadPool
@SpringBootApplication(scanBasePackages = "cn.hippo4j.example.core")
public class ConfigApolloSpringBoot15ExampleApplication {
public class ConfigApolloSpringBoot1xExampleApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApolloSpringBoot15ExampleApplication.class, args);
SpringApplication.run(ConfigApolloSpringBoot1xExampleApplication.class, args);
}
}

@ -21,16 +21,11 @@ spring.dynamic.thread-pool.banner=true
spring.dynamic.thread-pool.check-state-interval=3
spring.dynamic.thread-pool.notify-platforms[0].platform=WECHAT
spring.dynamic.thread-pool.notify-platforms[0].secret-key=ac0426a5-c712-474c-9bff-72b8b8f5caff
spring.dynamic.thread-pool.notify-platforms[1].platform=DING
spring.dynamic.thread-pool.notify-platforms[1].secret-key=56417ebba6a27ca352f0de77a2ae9da66d01f39610b5ee8a6033c60ef9071c55
spring.dynamic.thread-pool.notify-platforms[2].platform=LARK
spring.dynamic.thread-pool.notify-platforms[2].secret-key=2cbf2808-3839-4c26-a04d-fd201dd51f9e
spring.dynamic.thread-pool.notify-platforms[0].token=ac0426a5-c712-474c-9bff-72b8b8f5caff
spring.dynamic.thread-pool.apollo.namespace=application
spring.dynamic.thread-pool.config-file-type=properties
spring.dynamic.thread-pool.executors[0].active-alarm = 80
spring.dynamic.thread-pool.executors[0].alarm = true
spring.dynamic.thread-pool.executors[0].allow-core-thread-time-out = true

@ -7,7 +7,7 @@
<artifactId>hippo4j-example</artifactId>
<version>${revision}</version>
</parent>
<artifactId>hippo4j-config-nacos-spring-boot-1.5-starter-example</artifactId>
<artifactId>hippo4j-config-nacos-spring-boot-1x-starter-example</artifactId>
<properties>
<maven.deploy.skip>true</maven.deploy.skip>
@ -16,7 +16,6 @@
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
@ -47,7 +46,7 @@
<dependency>
<groupId>cn.hippo4j</groupId>
<artifactId>hippo4j-config-spring-boot-1.5-starter</artifactId>
<artifactId>hippo4j-config-spring-boot-1x-starter</artifactId>
<version>${revision}</version>
</dependency>
@ -67,5 +66,4 @@
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>

@ -10,6 +10,10 @@
<artifactId>hippo4j-example</artifactId>
<packaging>pom</packaging>
<properties>
<maven.deploy.skip>true</maven.deploy.skip>
</properties>
<modules>
<module>hippo4j-example-core</module>
<module>hippo4j-spring-boot-starter-example</module>
@ -23,11 +27,7 @@
<module>hippo4j-spring-boot-starter-adapter-spring-cloud-stream-rocketmq-example</module>
<module>hippo4j-spring-boot-starter-adapter-rocketmq-example</module>
<module>hippo4j-config-etcd-spring-boot-starter-example</module>
<module>hippo4j-config-nacos-spring-boot-1.5-starter-example</module>
<module>hippo4j-config-apollo-spring-boot-1.5-starter-example</module>
<module>hippo4j-config-nacos-spring-boot-1x-starter-example</module>
<module>hippo4j-config-apollo-spring-boot-1x-starter-example</module>
</modules>
<properties>
<maven.deploy.skip>true</maven.deploy.skip>
</properties>
</project>

@ -1 +0,0 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.hippo4j.config.springboot15x.starter.config.ConfigHandlerConfiguration

@ -2,15 +2,13 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.hippo4j</groupId>
<artifactId>hippo4j-spring-boot</artifactId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>hippo4j-config-spring-boot-1.5-starter</artifactId>
<artifactId>hippo4j-config-spring-boot-1x-starter</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>

@ -15,22 +15,22 @@
* limitations under the License.
*/
package cn.hippo4j.config.springboot15x.starter.config;
package cn.hippo4j.config.springboot1x.starter.config;
import cn.hippo4j.config.springboot.starter.refresher.BootstrapConfigPropertiesBinderAdapt;
import cn.hippo4j.config.springboot15x.starter.refresher.SpringBoot15BootstrapConfigPropertiesBinderAdapt;
import cn.hippo4j.config.springboot1x.starter.refresher.SpringBoot1xBootstrapConfigPropertiesBinderAdapt;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.bind.RelaxedDataBinder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ConfigHandlerConfiguration {
/**
* Config handler auto configuration.
*/
public class ConfigHandlerAutoConfiguration {
@Bean
@ConditionalOnClass(RelaxedDataBinder.class)
public BootstrapConfigPropertiesBinderAdapt bootstrapConfigPropertiesBinderAdapt() {
return new SpringBoot15BootstrapConfigPropertiesBinderAdapt();
return new SpringBoot1xBootstrapConfigPropertiesBinderAdapt();
}
}

@ -15,7 +15,7 @@
* limitations under the License.
*/
package cn.hippo4j.config.springboot15x.starter.refresher;
package cn.hippo4j.config.springboot1x.starter.refresher;
import cn.hippo4j.config.springboot.starter.config.BootstrapConfigProperties;
import cn.hippo4j.config.springboot.starter.refresher.BootstrapConfigPropertiesBinderAdapt;
@ -40,15 +40,15 @@ import java.util.Set;
/**
* Bootstrap core properties binder adapt.
*/
public class SpringBoot15BootstrapConfigPropertiesBinderAdapt implements ApplicationContextAware, BootstrapConfigPropertiesBinderAdapt {
public class SpringBoot1xBootstrapConfigPropertiesBinderAdapt implements ApplicationContextAware, BootstrapConfigPropertiesBinderAdapt {
private ApplicationContext applicationContext;
/**
* Bootstrap core properties binder.
*
* @param configInfo
* @param bootstrapConfigProperties
* @param configInfo config info
* @param bootstrapConfigProperties bootstrap config properties
* @return
*/
@Override
@ -56,20 +56,17 @@ public class SpringBoot15BootstrapConfigPropertiesBinderAdapt implements Applica
BootstrapConfigProperties bindableCoreProperties = new BootstrapConfigProperties();
RelaxedNames relaxedNames = new RelaxedNames(BootstrapConfigProperties.PREFIX);
Set<String> names = getNames(bindableCoreProperties, relaxedNames);
// 绑定器
Map<String, Object> stringConfigInfo = new HashMap<>(configInfo.size());
configInfo.forEach((key, value) -> stringConfigInfo.put(key.toString(), value));
MapPropertySource test = new MapPropertySource("Hippo4j", stringConfigInfo);
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addFirst(test);
PropertyValues propertyValues = CustomPropertyNamePatternsMatcher.getPropertySourcesPropertyValues(names, propertySources);
RelaxedDataBinder dataBinder = new RelaxedDataBinder(bindableCoreProperties, BootstrapConfigProperties.PREFIX);
dataBinder.setAutoGrowCollectionLimit(Integer.MAX_VALUE);
dataBinder.setIgnoreNestedProperties(false);
dataBinder.setIgnoreInvalidFields(false);
dataBinder.setIgnoreUnknownFields(true);
ResourceEditorRegistrar resourceEditorRegistrar = new ResourceEditorRegistrar(applicationContext, applicationContext.getEnvironment());
resourceEditorRegistrar.registerCustomEditors(dataBinder);
dataBinder.bind(propertyValues);

@ -18,24 +18,30 @@
package org.springframework.boot.bind;
import org.springframework.beans.PropertyValues;
import org.springframework.boot.bind.DefaultPropertyNamePatternsMatcher;
import org.springframework.boot.bind.PropertyNamePatternsMatcher;
import org.springframework.boot.bind.PropertySourcesPropertyValues;
import org.springframework.core.env.MutablePropertySources;
import java.util.Set;
/**
* Custom property name patterns matcher.
*/
public class CustomPropertyNamePatternsMatcher {
private static final char[] EXACT_DELIMITERS = {'_', '.', '['};
/**
* Get property sources property values.
*
* @param names
* @param propertySources
* @return
*/
public static PropertyValues getPropertySourcesPropertyValues(Set<String> names, MutablePropertySources propertySources) {
PropertyNamePatternsMatcher includes = getPropertyNamePatternsMatcher(names);
return new PropertySourcesPropertyValues(propertySources, names, includes, true);
}
private static PropertyNamePatternsMatcher getPropertyNamePatternsMatcher(Set<String> names) {
return new DefaultPropertyNamePatternsMatcher(EXACT_DELIMITERS, true, names);
}
}

@ -0,0 +1 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.hippo4j.config.springboot1x.starter.config.ConfigHandlerAutoConfiguration

@ -118,5 +118,4 @@ public class ConfigHandlerConfiguration {
return new PolarisRefresherHandler(configFileService);
}
}
}

@ -57,9 +57,9 @@ import org.springframework.core.annotation.Order;
@Configuration
@AllArgsConstructor
@ConditionalOnBean(MarkerConfiguration.Marker.class)
@EnableConfigurationProperties(BootstrapConfigProperties.class)
@ConditionalOnProperty(prefix = BootstrapConfigProperties.PREFIX, value = "enable", matchIfMissing = true, havingValue = "true")
@Import({ConfigHandlerConfiguration.class})
@EnableConfigurationProperties(BootstrapConfigProperties.class)
@Import(ConfigHandlerConfiguration.class)
@ImportAutoConfiguration({WebAdapterConfiguration.class, UtilAutoConfiguration.class, MessageConfiguration.class, LocalLogMonitorConfiguration.class, MicrometerMonitorConfiguration.class})
public class DynamicThreadPoolAutoConfiguration {

@ -25,7 +25,6 @@ import cn.hippo4j.config.springboot.starter.config.BootstrapConfigProperties;
import cn.hippo4j.config.springboot.starter.parser.ConfigParserHandler;
import cn.hippo4j.config.springboot.starter.refresher.event.Hippo4jConfigDynamicRefreshEvent;
import cn.hippo4j.core.executor.support.ThreadPoolBuilder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
@ -71,8 +70,8 @@ public abstract class AbstractConfigThreadPoolDynamicRefresh
if (CollectionUtil.isNotEmpty(newValueChangeMap)) {
Optional.ofNullable(configInfo).ifPresent(each -> each.putAll(newValueChangeMap));
}
BootstrapConfigProperties bindableCoreProperties = bootstrapConfigPropertiesBinderAdapt.bootstrapCorePropertiesBinder(configInfo, bootstrapConfigProperties);
ApplicationContextHolder.getInstance().publishEvent(new Hippo4jConfigDynamicRefreshEvent(this, bindableCoreProperties));
BootstrapConfigProperties binderCoreProperties = bootstrapConfigPropertiesBinderAdapt.bootstrapCorePropertiesBinder(configInfo, bootstrapConfigProperties);
ApplicationContextHolder.getInstance().publishEvent(new Hippo4jConfigDynamicRefreshEvent(this, binderCoreProperties));
} catch (Exception ex) {
log.error("Hippo-4J core dynamic refresh failed.", ex);
}

@ -21,7 +21,17 @@ import cn.hippo4j.config.springboot.starter.config.BootstrapConfigProperties;
import java.util.Map;
/**
* Bootstrap config properties binder adapt.
*/
public interface BootstrapConfigPropertiesBinderAdapt {
/**
* Bootstrap core properties binder.
*
* @param configInfo dynamic thread pool configuration
* @param bootstrapConfigProperties bootstrap config properties
* @return
*/
BootstrapConfigProperties bootstrapCorePropertiesBinder(Map<Object, Object> configInfo, BootstrapConfigProperties bootstrapConfigProperties);
}

@ -43,5 +43,4 @@ public class DefaultBootstrapConfigPropertiesBinderAdapt implements BootstrapCon
Binder binder = new Binder(sources);
return binder.bind(BootstrapConfigProperties.PREFIX, Bindable.ofInstance(bootstrapConfigProperties)).get();
}
}

@ -31,9 +31,7 @@ import java.util.Map;
import java.util.Objects;
/**
* @author : wh
* @date : 2022/8/30 17:59
* @description:
* Etcd refresher handler.
*/
@Slf4j
public class EtcdRefresherHandler extends AbstractConfigThreadPoolDynamicRefresh {
@ -57,7 +55,6 @@ public class EtcdRefresherHandler extends AbstractConfigThreadPoolDynamicRefresh
Map<String, String> etcd = bootstrapConfigProperties.getEtcd();
Charset charset = StringUtil.isBlank(etcd.get(CHARSET)) ? StandardCharsets.UTF_8 : Charset.forName(etcd.get(CHARSET));
initClient(etcd, charset);
String key = etcd.get(KEY);
GetResponse getResponse = client.getKVClient().get(ByteSequence.from(key, charset)).get();
KeyValue keyValue = getResponse.getKvs().get(0);
@ -70,8 +67,7 @@ public class EtcdRefresherHandler extends AbstractConfigThreadPoolDynamicRefresh
String key = etcd.get(KEY);
Charset charset = StringUtil.isBlank(etcd.get(CHARSET)) ? StandardCharsets.UTF_8 : Charset.forName(etcd.get(CHARSET));
initClient(etcd, charset);
// todo Currently only supports json
// TODO Currently only supports json
GetResponse getResponse = client.getKVClient().get(ByteSequence.from(key, charset)).get();
KeyValue keyValue = getResponse.getKvs().get(0);
if (Objects.isNull(keyValue)) {
@ -111,7 +107,7 @@ public class EtcdRefresherHandler extends AbstractConfigThreadPoolDynamicRefresh
* @param charset charset
*/
private void initClient(Map<String, String> etcd, Charset charset) {
// todo
// TODO
if (Objects.isNull(client)) {
String user = etcd.get(USER);
String password = etcd.get(PASSWORD);
@ -123,5 +119,4 @@ public class EtcdRefresherHandler extends AbstractConfigThreadPoolDynamicRefresh
.build() : clientBuilder.build();
}
}
}

@ -32,6 +32,7 @@ import java.util.concurrent.Executor;
public class NacosCloudRefresherHandler extends AbstractConfigThreadPoolDynamicRefresh {
static final String DATA_ID = "data-id";
static final String GROUP = "group";
private final NacosConfigManager nacosConfigManager;

@ -17,7 +17,6 @@
package cn.hippo4j.config.springboot.starter.refresher;
import cn.hippo4j.config.springboot.starter.config.BootstrapConfigProperties;
import com.alibaba.cloud.nacos.NacosConfigProperties;
import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.config.ConfigService;
@ -34,6 +33,7 @@ import java.util.concurrent.Executor;
public class NacosRefresherHandler extends AbstractConfigThreadPoolDynamicRefresh {
static final String DATA_ID = "data-id";
static final String GROUP = "group";
@NacosInjected

@ -29,9 +29,7 @@ import java.util.Map;
import java.util.Objects;
/**
*@author : wh
*@date : 2022/10/1 15:24
*@description:
* Polaris refresher handler.
*/
@RequiredArgsConstructor
public class PolarisRefresherHandler extends AbstractConfigThreadPoolDynamicRefresh {
@ -80,5 +78,4 @@ public class PolarisRefresherHandler extends AbstractConfigThreadPoolDynamicRefr
return Objects.equals(POLARIS_FILE_TYPE, "yaml") ? configFileService.getConfigYamlFile(namespace, fileGroup, fileName)
: configFileService.getConfigPropertiesFile(namespace, fileGroup, fileName);
}
}

@ -15,6 +15,6 @@
<module>hippo4j-spring-boot-starter</module>
<module>hippo4j-spring-boot-starter-adapter</module>
<module>hippo4j-spring-boot-starter-monitor</module>
<module>hippo4j-config-spring-boot-1.5-starter</module>
<module>hippo4j-config-spring-boot-1x-starter</module>
</modules>
</project>

Loading…
Cancel
Save