Optimize(Support for Consul Configuration Center to dynamically adjust parameters )

pull/1007/head
Li 3 years ago
parent 70cd583577
commit c209a094de

@ -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) {

@ -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);
}
}

@ -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;
}
}

@ -15,4 +15,4 @@ spring:
format: yaml
data-key: hippo4j-consul
default-context: application
prefixes: config
prefix: config

@ -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<BootstrapPropertySource<?>> bootstrapPropertySourceList = environment.getPropertySources().stream()
.filter(propertySource -> propertySource instanceof BootstrapPropertySource)
.map(propertySource -> (BootstrapPropertySource<?>) propertySource).collect(Collectors.toList());
Optional<BootstrapPropertySource<?>> 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<Object, Object> 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<String, CharSequence>) propertySource.getSource()).get("spring.cloud.consul.config.default-context"))
.findFirst().orElse(StringUtils.EMPTY);
}
/**
* TODO consul
* @return
*/
@Override
public String getProperties() {
return null;

Loading…
Cancel
Save