diff --git a/fizz-common/src/main/java/we/config/RedisReactiveConfig.java b/fizz-common/src/main/java/we/config/RedisReactiveConfig.java index b38424c..63e31d6 100644 --- a/fizz-common/src/main/java/we/config/RedisReactiveConfig.java +++ b/fizz-common/src/main/java/we/config/RedisReactiveConfig.java @@ -37,12 +37,12 @@ public abstract class RedisReactiveConfig { protected static final Logger log = LoggerFactory.getLogger(RedisReactiveConfig.class); - // this should not be changed unless there is a truly good reason to do so + // this should not be changed unless there is a truely good reason to do so private static final int ps = Runtime.getRuntime().availableProcessors(); - private static final ClientResources clientResources = DefaultClientResources.builder() - .ioThreadPoolSize(ps) - .computationThreadPoolSize(ps) - .build(); + public static final ClientResources clientResources = DefaultClientResources.builder() + .ioThreadPoolSize(ps) + .computationThreadPoolSize(ps) + .build(); private RedisReactiveProperties redisReactiveProperties; diff --git a/fizz-common/src/main/java/we/util/ReactiveRedisHelper.java b/fizz-common/src/main/java/we/util/ReactiveRedisHelper.java new file mode 100644 index 0000000..5d253f1 --- /dev/null +++ b/fizz-common/src/main/java/we/util/ReactiveRedisHelper.java @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2020 the original author or authors. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package we.util; + +import io.lettuce.core.ClientOptions; +import org.apache.commons.pool2.impl.GenericObjectPoolConfig; +import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory; +import org.springframework.data.redis.connection.RedisStandaloneConfiguration; +import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; +import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration; +import org.springframework.data.redis.core.ReactiveStringRedisTemplate; +import we.config.RedisReactiveConfig; + +/** + * @apiNote just helper, RedisReactiveConfig is best practice + *

+ * + * @author hongqiaowei + */ + +public abstract class ReactiveRedisHelper { + + public static ReactiveRedisConnectionFactory getConnectionFactory(String host, int port, String password, int database) { + RedisStandaloneConfiguration rcs = new RedisStandaloneConfiguration(host, port); + if (password != null) { + rcs.setPassword(password); + } + rcs.setDatabase(database); + + LettucePoolingClientConfiguration ccs = LettucePoolingClientConfiguration.builder() + .clientResources(RedisReactiveConfig.clientResources) + .clientOptions(ClientOptions.builder().publishOnScheduler(true).build()) + .poolConfig(new GenericObjectPoolConfig()) + .build(); + + LettuceConnectionFactory factory = new LettuceConnectionFactory(rcs, ccs); + factory.afterPropertiesSet(); + return factory; + } + + public static ReactiveStringRedisTemplate getStringRedisTemplate(String host, int port, String password, int database) { + ReactiveRedisConnectionFactory connectionFactory = getConnectionFactory(host, port, password, database); + ReactiveStringRedisTemplate template = new ReactiveStringRedisTemplate(connectionFactory); + return template; + } +} diff --git a/fizz-core/src/main/java/we/plugin/FizzPluginFilterChain.java b/fizz-core/src/main/java/we/plugin/FizzPluginFilterChain.java index 2f7c420..cdea449 100644 --- a/fizz-core/src/main/java/we/plugin/FizzPluginFilterChain.java +++ b/fizz-core/src/main/java/we/plugin/FizzPluginFilterChain.java @@ -42,11 +42,11 @@ public final class FizzPluginFilterChain { } public static Mono next(ServerWebExchange exchange) { - Map attris = exchange.getAttributes(); - List pcs = WebUtils.getApiConfig(exchange).pluginConfigs; - Iterator it = (Iterator) attris.get(pluginConfigsIt); + Iterator it = exchange.getAttribute(pluginConfigsIt); if (it == null) { + List pcs = WebUtils.getApiConfig(exchange).pluginConfigs; it = pcs.iterator(); + Map attris = exchange.getAttributes(); attris.put(pluginConfigsIt, it); } if (it.hasNext()) { @@ -70,7 +70,7 @@ public final class FizzPluginFilterChain { } } if (!f && !it.hasNext()) { - WebFilterChain chain = (WebFilterChain) attris.get(WEB_FILTER_CHAIN); + WebFilterChain chain = exchange.getAttribute(WEB_FILTER_CHAIN); m = m.defaultIfEmpty(ReactorUtils.NULL).flatMap( v -> { return chain.filter(exchange); @@ -80,7 +80,7 @@ public final class FizzPluginFilterChain { } return m; } else { - WebFilterChain chain = (WebFilterChain) attris.get(WEB_FILTER_CHAIN); + WebFilterChain chain = exchange.getAttribute(WEB_FILTER_CHAIN); return chain.filter(exchange); } }