pull/1595/merge
Haotian Ma 7 months ago committed by GitHub
commit 1e536b71ce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -34,6 +34,7 @@
<elasticsearch.version>6.8.17</elasticsearch.version>
<tomcat-embed-core.version>9.0.55</tomcat-embed-core.version>
<curator-framework.version>5.1.0</curator-framework.version>
<redisson.version>3.43.0</redisson.version>
<spring-cloud-starter-stream-rocketmq.version>2.2.6.RELEASE</spring-cloud-starter-stream-rocketmq.version>
<spring-cloud-starter-stream-rabbitmq.version>3.1.0</spring-cloud-starter-stream-rabbitmq.version>
<spring-cloud-starter-netflix-hystrix.version>2.2.9.RELEASE</spring-cloud-starter-netflix-hystrix.version>
@ -216,6 +217,11 @@
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>${spring-cloud-starter-alibaba-nacos-config.version}</version>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>${redisson.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>

@ -40,5 +40,10 @@
<artifactId>hippo4j-threadpool-spring-boot-starter-adapter-hystrix</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>cn.hippo4j</groupId>
<artifactId>hippo4j-threadpool-adapter-redisson</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>

@ -19,6 +19,7 @@
<module>rocketmq</module>
<module>kafka</module>
<module>hystrix</module>
<module>redisson</module>
<module>stream-rocketmq</module>
<module>stream-rabbitmq</module>
<module>web</module>

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.hippo4j</groupId>
<artifactId>hippo4j-threadpool-spring-boot-starter-adapter</artifactId>
<version>${revision}</version>
</parent>
<artifactId>hippo4j-threadpool-spring-boot-starter-adapter-redisson</artifactId>
<dependencies>
<dependency>
<groupId>cn.hippo4j</groupId>
<artifactId>hippo4j-threadpool-adapter-redisson</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>

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

@ -0,0 +1 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.hippo4j.springboot.starter.adapter.redisson.RedissonAdapterAutoConfiguration

@ -21,6 +21,7 @@
<module>hystrix</module>
<module>stream-rocketmq</module>
<module>stream-rabbitmq</module>
<module>redisson</module>
<module>web</module>
</modules>
</project>

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.hippo4j</groupId>
<artifactId>hippo4j-threadpool-adapter</artifactId>
<version>${revision}</version>
</parent>
<artifactId>hippo4j-threadpool-adapter-redisson</artifactId>
<dependencies>
<dependency>
<groupId>cn.hippo4j</groupId>
<artifactId>hippo4j-threadpool-adapter-base</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>

@ -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<ApplicationStartedEvent> {
private final Map<String, ThreadPoolExecutor> 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<ThreadPoolAdapterState> getThreadPoolStates() {
List<ThreadPoolAdapterState> 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<String, RedissonClient> 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);
}
}
}
Loading…
Cancel
Save