From c209a094de25dc289aa2fdc2cf7d81e3829fa7b6 Mon Sep 17 00:00:00 2001 From: Li Date: Wed, 23 Nov 2022 23:36:08 +0800 Subject: [PATCH] =?UTF-8?q?Optimize=EF=BC=88Support=20for=20Consul=20Confi?= =?UTF-8?q?guration=20Center=20to=20dynamically=20adjust=20parameters=20?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ConfigConsulExampleApplication.java | 2 +- .../config/DynamicThreadPoolConfig.java | 41 ------------------- .../consul/controller/TestController.java | 25 ----------- .../src/main/resources/bootstrap.yml | 2 +- .../refresher/ConsulRefresherHandler.java | 24 +++++++---- 5 files changed, 18 insertions(+), 76 deletions(-) delete mode 100644 hippo4j-example/hippo4j-config-consul-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/consul/config/DynamicThreadPoolConfig.java delete mode 100644 hippo4j-example/hippo4j-config-consul-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/consul/controller/TestController.java diff --git a/hippo4j-example/hippo4j-config-consul-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/consul/ConfigConsulExampleApplication.java b/hippo4j-example/hippo4j-config-consul-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/consul/ConfigConsulExampleApplication.java index f91081df..550acd64 100644 --- a/hippo4j-example/hippo4j-config-consul-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/consul/ConfigConsulExampleApplication.java +++ b/hippo4j-example/hippo4j-config-consul-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/consul/ConfigConsulExampleApplication.java @@ -22,7 +22,7 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @EnableDynamicThreadPool -@SpringBootApplication(scanBasePackages = "cn.hippo4j.example.config.consul") +@SpringBootApplication(scanBasePackages = "cn.hippo4j.example") public class ConfigConsulExampleApplication { public static void main(String[] args) { diff --git a/hippo4j-example/hippo4j-config-consul-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/consul/config/DynamicThreadPoolConfig.java b/hippo4j-example/hippo4j-config-consul-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/consul/config/DynamicThreadPoolConfig.java deleted file mode 100644 index 13d57ae4..00000000 --- a/hippo4j-example/hippo4j-config-consul-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/consul/config/DynamicThreadPoolConfig.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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 cn.hippo4j.example.config.consul.config; - -import cn.hippo4j.core.executor.DynamicThreadPool; -import cn.hippo4j.core.executor.support.ThreadPoolBuilder; -import lombok.extern.slf4j.Slf4j; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import java.util.concurrent.ThreadPoolExecutor; - -/** - * Dynamic thread-pool config. - */ -@Slf4j -@Configuration -public class DynamicThreadPoolConfig { - - @Bean - @DynamicThreadPool - public ThreadPoolExecutor messageConsumeDynamicExecutor() { - String threadPoolId = "message-consume"; - return ThreadPoolBuilder.buildDynamicPoolById(threadPoolId); - } -} diff --git a/hippo4j-example/hippo4j-config-consul-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/consul/controller/TestController.java b/hippo4j-example/hippo4j-config-consul-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/consul/controller/TestController.java deleted file mode 100644 index df22d43a..00000000 --- a/hippo4j-example/hippo4j-config-consul-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/consul/controller/TestController.java +++ /dev/null @@ -1,25 +0,0 @@ -package cn.hippo4j.example.config.consul.controller; - -import cn.hippo4j.config.springboot.starter.config.BootstrapConfigProperties; -import lombok.extern.slf4j.Slf4j; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RestController; - -import javax.annotation.Resource; -import java.util.concurrent.ThreadPoolExecutor; - -@Slf4j -@RestController -public class TestController { - - @Resource - private ThreadPoolExecutor messageConsumeDynamicExecutor; - - @GetMapping("/hippo4j-consul/test") - public int test() { - int maximumPoolSize = messageConsumeDynamicExecutor.getMaximumPoolSize(); - int corePoolSize = messageConsumeDynamicExecutor.getCorePoolSize(); - log.info("corePoolSize: {}, maximumPoolSize: {}", corePoolSize, maximumPoolSize); - return corePoolSize + maximumPoolSize; - } -} diff --git a/hippo4j-example/hippo4j-config-consul-spring-boot-starter-example/src/main/resources/bootstrap.yml b/hippo4j-example/hippo4j-config-consul-spring-boot-starter-example/src/main/resources/bootstrap.yml index adcd25ba..12d353de 100644 --- a/hippo4j-example/hippo4j-config-consul-spring-boot-starter-example/src/main/resources/bootstrap.yml +++ b/hippo4j-example/hippo4j-config-consul-spring-boot-starter-example/src/main/resources/bootstrap.yml @@ -15,4 +15,4 @@ spring: format: yaml data-key: hippo4j-consul default-context: application - prefixes: config + prefix: config diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/ConsulRefresherHandler.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/ConsulRefresherHandler.java index 41fe155e..da51cb5a 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/ConsulRefresherHandler.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/ConsulRefresherHandler.java @@ -18,7 +18,9 @@ package cn.hippo4j.config.springboot.starter.refresher; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.env.OriginTrackedMapPropertySource; import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext; import org.springframework.cloud.bootstrap.config.BootstrapPropertySource; import org.springframework.cloud.consul.config.ConsulPropertySource; @@ -45,20 +47,16 @@ public class ConsulRefresherHandler extends AbstractConfigThreadPoolDynamicRefre @EventListener(EnvironmentChangeEvent.class) public void refreshed(EnvironmentChangeEvent event) { - - String[] dataKeys = this.dataKey.split(","); this.dataKey = dataKeys[0]; - AbstractEnvironment environment = (AbstractEnvironment) ((AnnotationConfigServletWebServerApplicationContext) event.getSource()).getEnvironment(); + String activeProfile = Optional.ofNullable(environment.getActiveProfiles().length > 0 ? environment.getActiveProfiles()[0] : null) + .orElseGet(() -> String.valueOf(getApplicationConfigDefaultContext(environment))); List> bootstrapPropertySourceList = environment.getPropertySources().stream() .filter(propertySource -> propertySource instanceof BootstrapPropertySource) .map(propertySource -> (BootstrapPropertySource) propertySource).collect(Collectors.toList()); - Optional> bootstrapPropertySource = bootstrapPropertySourceList.stream() - .filter(source -> source.getName().contains(environment.getActiveProfiles()[0]) - && source.getPropertyNames().length != 0).findFirst(); - + .filter(source -> source.getName().contains(activeProfile) && source.getPropertyNames().length != 0).findFirst(); Map configInfo = new HashMap<>(64); if (bootstrapPropertySource.isPresent()) { ConsulPropertySource consulPropertySource = (ConsulPropertySource) bootstrapPropertySource.get().getDelegate(); @@ -68,10 +66,20 @@ public class ConsulRefresherHandler extends AbstractConfigThreadPoolDynamicRefre } } - dynamicRefresh(configInfo); } + private CharSequence getApplicationConfigDefaultContext(AbstractEnvironment environment) { + return environment.getPropertySources().stream() + .filter(propertySource -> propertySource instanceof OriginTrackedMapPropertySource) + .map(propertySource -> ((Map) propertySource.getSource()).get("spring.cloud.consul.config.default-context")) + .findFirst().orElse(StringUtils.EMPTY); + } + + /** + * TODO consul已实现,暂时不重写 + * @return + */ @Override public String getProperties() { return null;