diff --git a/dependencies/pom.xml b/dependencies/pom.xml index af91cef387..e2f89950f0 100644 --- a/dependencies/pom.xml +++ b/dependencies/pom.xml @@ -34,6 +34,7 @@ 6.8.17 9.0.55 5.1.0 + 3.43.0 2.2.6.RELEASE 3.1.0 2.2.9.RELEASE @@ -216,6 +217,11 @@ spring-cloud-starter-alibaba-nacos-config ${spring-cloud-starter-alibaba-nacos-config.version} + + org.redisson + redisson-spring-boot-starter + ${redisson.version} + \ No newline at end of file diff --git a/starters/threadpool/adapter/all/pom.xml b/starters/threadpool/adapter/all/pom.xml index ceff5197b3..4ceb5c526f 100644 --- a/starters/threadpool/adapter/all/pom.xml +++ b/starters/threadpool/adapter/all/pom.xml @@ -40,5 +40,10 @@ hippo4j-threadpool-spring-boot-starter-adapter-hystrix ${project.version} + + cn.hippo4j + hippo4j-threadpool-adapter-redisson + ${project.version} + diff --git a/starters/threadpool/adapter/pom.xml b/starters/threadpool/adapter/pom.xml index 292cb30846..cbf8501358 100644 --- a/starters/threadpool/adapter/pom.xml +++ b/starters/threadpool/adapter/pom.xml @@ -19,6 +19,7 @@ rocketmq kafka hystrix + redisson stream-rocketmq stream-rabbitmq web diff --git a/starters/threadpool/adapter/redisson/pom.xml b/starters/threadpool/adapter/redisson/pom.xml new file mode 100644 index 0000000000..47559bd986 --- /dev/null +++ b/starters/threadpool/adapter/redisson/pom.xml @@ -0,0 +1,19 @@ + + +4.0.0 + + cn.hippo4j + hippo4j-threadpool-spring-boot-starter-adapter + ${revision} + +hippo4j-threadpool-spring-boot-starter-adapter-redisson + + + + cn.hippo4j + hippo4j-threadpool-adapter-redisson + ${project.version} + + + diff --git a/starters/threadpool/adapter/redisson/src/main/java/cn/hippo4j/springboot/starter/adapter/redisson/RedissonAdapterAutoConfiguration.java b/starters/threadpool/adapter/redisson/src/main/java/cn/hippo4j/springboot/starter/adapter/redisson/RedissonAdapterAutoConfiguration.java new file mode 100644 index 0000000000..ef23e3e728 --- /dev/null +++ b/starters/threadpool/adapter/redisson/src/main/java/cn/hippo4j/springboot/starter/adapter/redisson/RedissonAdapterAutoConfiguration.java @@ -0,0 +1,45 @@ +/* + * 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.springboot.starter.adapter.redisson; + +import cn.hippo4j.core.config.ApplicationContextHolder; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import cn.hippo4j.adapter.redisson.RedissonThreadPoolAdapter; + +/** + * Redisson adapter auto configuration. + */ +@Configuration +public class RedissonAdapterAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + public ApplicationContextHolder simpleApplicationContextHolder() { + return new ApplicationContextHolder(); + } + + @Bean + @ConditionalOnProperty(name = "spring.redis.host") + public RedissonThreadPoolAdapter redissonThreadPoolAdapter(ApplicationContextHolder applicationContextHolder) { + return new RedissonThreadPoolAdapter(); + } + +} diff --git a/starters/threadpool/adapter/redisson/src/main/resources/META-INF/spring.factories b/starters/threadpool/adapter/redisson/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000000..abf9f2085e --- /dev/null +++ b/starters/threadpool/adapter/redisson/src/main/resources/META-INF/spring.factories @@ -0,0 +1 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.hippo4j.springboot.starter.adapter.redisson.RedissonAdapterAutoConfiguration \ No newline at end of file diff --git a/starters/threadpool/adapter/redisson/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/starters/threadpool/adapter/redisson/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 0000000000..9dd3fdceab --- /dev/null +++ b/starters/threadpool/adapter/redisson/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +cn.hippo4j.springboot.starter.adapter.rabbitmq.RedissonAdapterAutoConfiguration \ No newline at end of file diff --git a/threadpool/adapter/pom.xml b/threadpool/adapter/pom.xml index d980bce1db..9a36872511 100644 --- a/threadpool/adapter/pom.xml +++ b/threadpool/adapter/pom.xml @@ -21,6 +21,7 @@ hystrix stream-rocketmq stream-rabbitmq + redisson web diff --git a/threadpool/adapter/redisson/pom.xml b/threadpool/adapter/redisson/pom.xml new file mode 100644 index 0000000000..e9b56439b7 --- /dev/null +++ b/threadpool/adapter/redisson/pom.xml @@ -0,0 +1,33 @@ + + +4.0.0 + + cn.hippo4j + hippo4j-threadpool-adapter + ${revision} + +hippo4j-threadpool-adapter-redisson + + + + cn.hippo4j + hippo4j-threadpool-adapter-base + ${project.version} + + + org.redisson + redisson-spring-boot-starter + true + + + io.micrometer + micrometer-registry-prometheus + + + + org.springframework.boot + spring-boot-starter-actuator + + + diff --git a/threadpool/adapter/redisson/src/main/java/cn/hippo4j/adapter/redisson/RedissonThreadPoolAdapter.java b/threadpool/adapter/redisson/src/main/java/cn/hippo4j/adapter/redisson/RedissonThreadPoolAdapter.java new file mode 100644 index 0000000000..0e9257cc59 --- /dev/null +++ b/threadpool/adapter/redisson/src/main/java/cn/hippo4j/adapter/redisson/RedissonThreadPoolAdapter.java @@ -0,0 +1,104 @@ +/* + * 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.adapter.redisson; + +import cn.hippo4j.adapter.base.ThreadPoolAdapter; +import cn.hippo4j.adapter.base.ThreadPoolAdapterParameter; +import cn.hippo4j.common.model.ThreadPoolAdapterState; +import cn.hippo4j.common.toolkit.ThreadPoolExecutorUtil; +import cn.hippo4j.core.config.ApplicationContextHolder; +import lombok.extern.slf4j.Slf4j; +import org.redisson.Redisson; +import org.redisson.api.RedissonClient; +import org.springframework.boot.context.event.ApplicationStartedEvent; +import org.springframework.context.ApplicationListener; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ThreadPoolExecutor; + +import static cn.hippo4j.common.constant.ChangeThreadPoolConstants.CHANGE_DELIMITER; + +/** + * Redisson thread-pool adapter. + */ +@Slf4j +public class RedissonThreadPoolAdapter implements ThreadPoolAdapter, ApplicationListener { + + private final Map redissonExecutors = new HashMap<>(); + + @Override + public String mark() { + return "redisson"; + } + + @Override + public ThreadPoolAdapterState getThreadPoolState(String identify) { + ThreadPoolAdapterState result = new ThreadPoolAdapterState(); + ThreadPoolExecutor redissonExecutor = redissonExecutors.get(identify); + if (redissonExecutor != null) { + result.setThreadPoolKey(identify); + result.setCoreSize(redissonExecutor.getCorePoolSize()); + result.setMaximumSize(redissonExecutor.getMaximumPoolSize()); + return result; + } + log.warn("[{}] Redisson thread pool not found.", identify); + return result; + } + + @Override + public List getThreadPoolStates() { + List adapterStateList = new ArrayList<>(); + redissonExecutors.forEach( + (key, val) -> adapterStateList.add(getThreadPoolState(key))); + return adapterStateList; + } + + @Override + public boolean updateThreadPool(ThreadPoolAdapterParameter threadPoolAdapterParameter) { + String threadPoolKey = threadPoolAdapterParameter.getThreadPoolKey(); + ThreadPoolExecutor redissonExecutor = redissonExecutors.get(threadPoolKey); + if (redissonExecutor != null) { + int originalCoreSize = redissonExecutor.getCorePoolSize(); + int originalMaximumPoolSize = redissonExecutor.getMaximumPoolSize(); + ThreadPoolExecutorUtil.safeSetPoolSize(redissonExecutor, threadPoolAdapterParameter.getCorePoolSize(), threadPoolAdapterParameter.getMaximumPoolSize()); + log.info("[{}] Redisson thread pool parameter change. coreSize: {}, maximumSize: {}", + threadPoolKey, + String.format(CHANGE_DELIMITER, originalCoreSize, redissonExecutor.getCorePoolSize()), + String.format(CHANGE_DELIMITER, originalMaximumPoolSize, redissonExecutor.getMaximumPoolSize())); + return true; + } + log.warn("[{}] Redisson thread pool not found.", threadPoolKey); + return false; + } + + @Override + public void onApplicationEvent(ApplicationStartedEvent applicationStartedEvent) { + Map redissonClientMap = ApplicationContextHolder.getBeansOfType(RedissonClient.class); + try { + redissonClientMap.forEach((name, redissonClient) -> { + ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) ((Redisson) redissonClient).getServiceManager().getExecutor(); + redissonExecutors.put(name, threadPoolExecutor); + }); + } catch (Exception e) { + log.error("Failed to get Redisson thread pool.", e); + } + } +}