From 796872022c8773ea0f9fccae34b89f3cf898bfd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E5=86=B7?= Date: Tue, 25 Jun 2019 18:05:36 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=B7=BB=E5=8A=A0=E6=96=B0?= =?UTF-8?q?=E7=89=B9=E6=80=A7=E3=80=82=20=E6=89=A9=E5=B1=95ribbon=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81IP=E6=9C=89=E9=99=90=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker-compose.yml | 2 +- .../pigx/auth/endpoint/PigxTokenEndpoint.java | 14 ++-- pigx-common/pigx-common-bom/pom.xml | 7 ++ .../data/cache/RedisTemplateConfig.java | 7 +- pigx-common/pigx-common-gateway/pom.xml | 5 ++ .../DynamicRouteAutoConfiguration.java | 1 + .../MicaRibbonRuleAutoConfiguration.java | 55 ++++++++++++ .../predicate/DiscoveryEnabledPredicate.java | 49 +++++++++++ .../predicate/MetadataAwarePredicate.java | 56 +++++++++++++ .../gateway/rule/DiscoveryEnabledRule.java | 76 +++++++++++++++++ .../gateway/rule/MetadataAwareRule.java | 83 +++++++++++++++++++ .../support/PigxRibbonRuleProperties.java | 50 +++++++++++ .../properties/SequenceRedisProperties.java | 2 +- .../SequenceSnowflakeProperties.java | 2 +- pigx-gateway/src/main/resources/bootstrap.yml | 1 + .../admin/config/DynamicRouteInitRunner.java | 8 +- .../admin/service/impl/MobileServiceImpl.java | 6 +- .../service/impl/SysRouteConfServiceImpl.java | 8 +- .../pigx/mp/config/WxMpConfiguration.java | 4 +- pom.xml | 2 +- 20 files changed, 412 insertions(+), 26 deletions(-) create mode 100755 pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/configuration/MicaRibbonRuleAutoConfiguration.java create mode 100755 pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/predicate/DiscoveryEnabledPredicate.java create mode 100755 pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/predicate/MetadataAwarePredicate.java create mode 100755 pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/rule/DiscoveryEnabledRule.java create mode 100755 pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/rule/MetadataAwareRule.java create mode 100755 pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/support/PigxRibbonRuleProperties.java diff --git a/docker-compose.yml b/docker-compose.yml index 8bfd8b417..d5f93eae4 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,4 @@ -# 使用说明 V3.1.0 +# 使用说明 V3.2.0 # 1. 使用docker-compose 宿主机不需要配置host来发现 # 2. 无需修改源码,根目录 docker-compose up 即可 # 3. 静静等待服务启动 diff --git a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/endpoint/PigxTokenEndpoint.java b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/endpoint/PigxTokenEndpoint.java index 0be02c86e..4de06ef49 100644 --- a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/endpoint/PigxTokenEndpoint.java +++ b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/endpoint/PigxTokenEndpoint.java @@ -66,7 +66,7 @@ import java.util.Map; public class PigxTokenEndpoint { private static final String PIGX_OAUTH_ACCESS = SecurityConstants.PIGX_PREFIX + SecurityConstants.OAUTH_PREFIX + "auth_to_access:"; private final ClientDetailsService clientDetailsService; - private final RedisTemplate pigxRedisTemplate; + private final RedisTemplate redisTemplate; private final TokenStore tokenStore; private final CacheManager cacheManager; @@ -167,19 +167,19 @@ public class PigxTokenEndpoint { List pages = findKeysForPage(key, MapUtil.getInt(params, PaginationConstants.CURRENT) , MapUtil.getInt(params, PaginationConstants.SIZE)); - pigxRedisTemplate.setKeySerializer(new StringRedisSerializer()); - pigxRedisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); + redisTemplate.setKeySerializer(new StringRedisSerializer()); + redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); Page result = new Page(MapUtil.getInt(params, PaginationConstants.CURRENT), MapUtil.getInt(params, PaginationConstants.SIZE)); - result.setRecords(pigxRedisTemplate.opsForValue().multiGet(pages)); - result.setTotal(Long.valueOf(pigxRedisTemplate.keys(key).size())); + result.setRecords(redisTemplate.opsForValue().multiGet(pages)); + result.setTotal(Long.valueOf(redisTemplate.keys(key).size())); return R.ok(result); } private List findKeysForPage(String patternKey, int pageNum, int pageSize) { ScanOptions options = ScanOptions.scanOptions().match(patternKey).build(); - RedisSerializer redisSerializer = (RedisSerializer) pigxRedisTemplate.getKeySerializer(); - Cursor cursor = (Cursor) pigxRedisTemplate.executeWithStickyConnection(redisConnection -> new ConvertingCursor<>(redisConnection.scan(options), redisSerializer::deserialize)); + RedisSerializer redisSerializer = (RedisSerializer) redisTemplate.getKeySerializer(); + Cursor cursor = (Cursor) redisTemplate.executeWithStickyConnection(redisConnection -> new ConvertingCursor<>(redisConnection.scan(options), redisSerializer::deserialize)); List result = new ArrayList<>(); int tmpIndex = 0; int startIndex = (pageNum - 1) * pageSize; diff --git a/pigx-common/pigx-common-bom/pom.xml b/pigx-common/pigx-common-bom/pom.xml index 398c4171d..5f7cca2ef 100644 --- a/pigx-common/pigx-common-bom/pom.xml +++ b/pigx-common/pigx-common-bom/pom.xml @@ -22,6 +22,7 @@ 8.0.16 1.5.22 3.3.0 + 1.1.3 @@ -132,6 +133,12 @@ weixin-java-common ${mp.weixin.version} + + + net.dreamlu + mica-plus-ribbon + ${mica.version} + diff --git a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/cache/RedisTemplateConfig.java b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/cache/RedisTemplateConfig.java index 9cb199ae2..eb49aee9d 100644 --- a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/cache/RedisTemplateConfig.java +++ b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/cache/RedisTemplateConfig.java @@ -18,7 +18,9 @@ package com.pig4cloud.pigx.common.data.cache; import lombok.AllArgsConstructor; +import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -34,13 +36,14 @@ import org.springframework.data.redis.serializer.StringRedisSerializer; */ @EnableCaching @Configuration -@ConditionalOnBean(RedisConnectionFactory.class) @AllArgsConstructor +@AutoConfigureBefore(RedisAutoConfiguration.class) +@ConditionalOnBean(RedisConnectionFactory.class) public class RedisTemplateConfig { private final RedisConnectionFactory redisConnectionFactory; @Bean - public RedisTemplate pigxRedisTemplate() { + public RedisTemplate redisTemplate() { RedisTemplate redisTemplate = new RedisTemplate<>(); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); diff --git a/pigx-common/pigx-common-gateway/pom.xml b/pigx-common/pigx-common-gateway/pom.xml index e5910a701..6ea0a5b5a 100644 --- a/pigx-common/pigx-common-gateway/pom.xml +++ b/pigx-common/pigx-common-gateway/pom.xml @@ -45,5 +45,10 @@ org.springframework.boot spring-boot-starter-data-redis-reactive + + org.springframework.cloud + spring-cloud-alibaba-nacos-discovery + compile + diff --git a/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/configuration/DynamicRouteAutoConfiguration.java b/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/configuration/DynamicRouteAutoConfiguration.java index ee192e5ad..80a3a9865 100644 --- a/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/configuration/DynamicRouteAutoConfiguration.java +++ b/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/configuration/DynamicRouteAutoConfiguration.java @@ -42,4 +42,5 @@ public class DynamicRouteAutoConfiguration { public PropertiesRouteDefinitionLocator propertiesRouteDefinitionLocator() { return new PropertiesRouteDefinitionLocator(new GatewayProperties()); } + } diff --git a/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/configuration/MicaRibbonRuleAutoConfiguration.java b/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/configuration/MicaRibbonRuleAutoConfiguration.java new file mode 100755 index 000000000..8133a9f61 --- /dev/null +++ b/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/configuration/MicaRibbonRuleAutoConfiguration.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.common.gateway.configuration; + +import com.pig4cloud.pigx.common.gateway.rule.DiscoveryEnabledRule; +import com.pig4cloud.pigx.common.gateway.rule.MetadataAwareRule; +import com.pig4cloud.pigx.common.gateway.support.MicaRibbonRuleProperties; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.boot.autoconfigure.AutoConfigureBefore; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.cloud.alibaba.nacos.ribbon.NacosServer; +import org.springframework.cloud.netflix.ribbon.RibbonClientConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Scope; + +/** + * Mica ribbon rule auto configuration. + * + * @author L.cm + */ +@Configuration +@RequiredArgsConstructor +@ConditionalOnClass(NacosServer.class) +@AutoConfigureBefore(RibbonClientConfiguration.class) +@EnableConfigurationProperties(MicaRibbonRuleProperties.class) +@ConditionalOnProperty(value = "mica.ribbon.rule.enabled", matchIfMissing = true) +public class MicaRibbonRuleAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public DiscoveryEnabledRule metadataAwareRule() { + return new MetadataAwareRule(); + } +} diff --git a/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/predicate/DiscoveryEnabledPredicate.java b/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/predicate/DiscoveryEnabledPredicate.java new file mode 100755 index 000000000..25f2a1430 --- /dev/null +++ b/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/predicate/DiscoveryEnabledPredicate.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.common.gateway.predicate; + +import com.netflix.loadbalancer.AbstractServerPredicate; +import com.netflix.loadbalancer.PredicateKey; +import org.springframework.cloud.alibaba.nacos.ribbon.NacosServer; +import org.springframework.lang.Nullable; + +/** + * 过滤服务 + * + * @author L.cm + */ +public abstract class DiscoveryEnabledPredicate extends AbstractServerPredicate { + + /** + * {@inheritDoc} + */ + @Override + public boolean apply(@Nullable PredicateKey input) { + return input != null + && input.getServer() instanceof NacosServer + && apply((NacosServer) input.getServer()); + } + + /** + * Returns whether the specific {@link NacosServer} matches this predicate. + * + * @param server the discovered server + * @return whether the server matches the predicate + */ + protected abstract boolean apply(NacosServer server); +} diff --git a/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/predicate/MetadataAwarePredicate.java b/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/predicate/MetadataAwarePredicate.java new file mode 100755 index 000000000..f102b1419 --- /dev/null +++ b/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/predicate/MetadataAwarePredicate.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.common.gateway.predicate; + +import cn.hutool.core.util.StrUtil; +import com.pig4cloud.pigx.common.core.util.SpringContextHolder; +import com.pig4cloud.pigx.common.gateway.support.MicaRibbonRuleProperties; +import org.springframework.cloud.alibaba.nacos.ribbon.NacosServer; + +import java.util.Map; + +/** + * 基于 Metadata 的服务筛选 + * + * @author L.cm + */ +public class MetadataAwarePredicate extends DiscoveryEnabledPredicate { + + @Override + protected boolean apply(NacosServer server) { + final Map metadata = server.getMetadata(); + + // 获取配置 + MicaRibbonRuleProperties properties = SpringContextHolder.getBean(MicaRibbonRuleProperties.class); + // 服务里的配置 + String localTag = properties.getTag(); + + if (StrUtil.isBlank(localTag)) { + return true; + } + + // 本地有 tag,服务没有,返回 false + String metaDataTag = metadata.get("tag"); + if (StrUtil.isBlank(metaDataTag)) { + return false; + } + + return metaDataTag.equals(localTag); + } + +} diff --git a/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/rule/DiscoveryEnabledRule.java b/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/rule/DiscoveryEnabledRule.java new file mode 100755 index 000000000..70ce94576 --- /dev/null +++ b/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/rule/DiscoveryEnabledRule.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.common.gateway.rule; + +import com.google.common.base.Optional; +import com.netflix.loadbalancer.*; +import com.pig4cloud.pigx.common.gateway.predicate.DiscoveryEnabledPredicate; +import org.springframework.util.Assert; + +import java.util.List; + +/** + * ribbon 路由规则 + * + * @author L.cm + */ +public abstract class DiscoveryEnabledRule extends PredicateBasedRule { + private final CompositePredicate predicate; + + public DiscoveryEnabledRule(DiscoveryEnabledPredicate discoveryEnabledPredicate) { + Assert.notNull(discoveryEnabledPredicate, "Parameter 'discoveryEnabledPredicate' can't be null"); + this.predicate = createCompositePredicate(discoveryEnabledPredicate, new AvailabilityPredicate(this, null)); + } + + /** + * {@inheritDoc} + */ + @Override + public AbstractServerPredicate getPredicate() { + return predicate; + } + + @Override + public Server choose(Object key) { + ILoadBalancer lb = getLoadBalancer(); + + List allServers = lb.getAllServers(); + // 过滤服务列表 + List serverList = filterServers(allServers); + + Optional server = getPredicate().chooseRoundRobinAfterFiltering(serverList, key); + if (server.isPresent()) { + return server.get(); + } else { + return null; + } + } + + /** + * 过滤服务 + * + * @param serverList 服务列表 + * @return 服务列表 + */ + public abstract List filterServers(List serverList); + + private CompositePredicate createCompositePredicate(DiscoveryEnabledPredicate discoveryEnabledPredicate, AvailabilityPredicate availabilityPredicate) { + return CompositePredicate.withPredicates(discoveryEnabledPredicate, availabilityPredicate) + .build(); + } +} diff --git a/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/rule/MetadataAwareRule.java b/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/rule/MetadataAwareRule.java new file mode 100755 index 000000000..edaf61c94 --- /dev/null +++ b/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/rule/MetadataAwareRule.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.common.gateway.rule; + +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.system.HostInfo; +import cn.hutool.system.SystemUtil; +import com.netflix.loadbalancer.Server; +import com.pig4cloud.pigx.common.core.util.SpringContextHolder; +import com.pig4cloud.pigx.common.gateway.predicate.DiscoveryEnabledPredicate; +import com.pig4cloud.pigx.common.gateway.predicate.MetadataAwarePredicate; +import com.pig4cloud.pigx.common.gateway.support.MicaRibbonRuleProperties; +import org.springframework.util.PatternMatchUtils; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * ribbon 路由规则器 + * + * @author dream.lu + */ +public class MetadataAwareRule extends DiscoveryEnabledRule { + + public MetadataAwareRule() { + this(new MetadataAwarePredicate()); + } + + public MetadataAwareRule(DiscoveryEnabledPredicate predicate) { + super(predicate); + } + + @Override + public List filterServers(List serverList) { + MicaRibbonRuleProperties ribbonProperties = SpringContextHolder.getBean(MicaRibbonRuleProperties.class); + List priorIpPattern = ribbonProperties.getPriorIpPattern(); + + // 1. 查找是否有本机 ip + HostInfo hostInfo = SystemUtil.getHostInfo(); + String hostIp = hostInfo.getAddress(); + + // 优先的 ip 规则 + boolean hasPriorIpPattern = !priorIpPattern.isEmpty(); + String[] priorIpPatterns = priorIpPattern.toArray(new String[0]); + + List priorServerList = new ArrayList<>(); + for (Server server : serverList) { + String host = server.getHost(); + // 2. 优先本地 ip 的服务 + if (ObjectUtil.equal(hostIp, host)) { + return Collections.singletonList(server); + } + // 3. 优先的 ip 服务 + if (hasPriorIpPattern && PatternMatchUtils.simpleMatch(priorIpPatterns, host)) { + priorServerList.add(server); + } + } + + // 4. 如果优先的有数据直接返回 + if (!priorServerList.isEmpty()) { + return priorServerList; + } + + return Collections.unmodifiableList(serverList); + } + +} diff --git a/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/support/PigxRibbonRuleProperties.java b/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/support/PigxRibbonRuleProperties.java new file mode 100755 index 000000000..e2435f7b7 --- /dev/null +++ b/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/support/PigxRibbonRuleProperties.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.common.gateway.support; + +import lombok.Getter; +import lombok.Setter; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.cloud.context.config.annotation.RefreshScope; + +import java.util.ArrayList; +import java.util.List; + +/** + * Ribbon 配置 + * + * @author L.cm + */ +@Getter +@Setter +@RefreshScope +@ConfigurationProperties("ribbon.rule") +public class PigxRibbonRuleProperties { + /** + * 是否开启,默认:true + */ + private Boolean enabled = Boolean.TRUE; + /** + * 服务的tag,用于灰度,匹配:nacos.discovery.metadata.tag + */ + private String tag; + /** + * 优先的ip列表,支持通配符,例如:10.20.0.8*、10.20.0.* + */ + private List priorIpPattern = new ArrayList<>(); +} diff --git a/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/common/sequence/properties/SequenceRedisProperties.java b/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/common/sequence/properties/SequenceRedisProperties.java index d6e0473f9..c6d6e9e5a 100644 --- a/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/common/sequence/properties/SequenceRedisProperties.java +++ b/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/common/sequence/properties/SequenceRedisProperties.java @@ -17,7 +17,7 @@ import org.springframework.stereotype.Component; */ @Data @Component -@ConfigurationProperties(prefix = "xsequence.redis") +@ConfigurationProperties(prefix = "pigx.xsequence.redis") public class SequenceRedisProperties extends BaseSequenceProperties { } \ No newline at end of file diff --git a/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/common/sequence/properties/SequenceSnowflakeProperties.java b/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/common/sequence/properties/SequenceSnowflakeProperties.java index f7927cb36..e12b5e62a 100644 --- a/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/common/sequence/properties/SequenceSnowflakeProperties.java +++ b/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/common/sequence/properties/SequenceSnowflakeProperties.java @@ -12,7 +12,7 @@ import org.springframework.stereotype.Component; */ @Data @Component -@ConfigurationProperties(prefix = "xsequence.snowflake") +@ConfigurationProperties(prefix = "pigx.xsequence.snowflake") public class SequenceSnowflakeProperties extends BaseSequenceProperties { /** diff --git a/pigx-gateway/src/main/resources/bootstrap.yml b/pigx-gateway/src/main/resources/bootstrap.yml index e4817417f..dff850d8c 100644 --- a/pigx-gateway/src/main/resources/bootstrap.yml +++ b/pigx-gateway/src/main/resources/bootstrap.yml @@ -8,6 +8,7 @@ spring: nacos: discovery: server-addr: ${NACOS-HOST:pigx-register}:${NACOS-PORT:8848} + network-interface: ppp0 config: server-addr: ${spring.cloud.nacos.discovery.server-addr} file-extension: yml diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/config/DynamicRouteInitRunner.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/config/DynamicRouteInitRunner.java index 5c8f29e16..69e345dcb 100644 --- a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/config/DynamicRouteInitRunner.java +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/config/DynamicRouteInitRunner.java @@ -50,14 +50,14 @@ import java.net.URI; @Configuration @AllArgsConstructor public class DynamicRouteInitRunner { - private final RedisTemplate pigxRedisTemplate; + private final RedisTemplate redisTemplate; private final SysRouteConfService routeConfService; @Async @Order @EventListener({WebServerInitializedEvent.class, DynamicRouteInitEvent.class}) public void initRoute() { - Boolean result = pigxRedisTemplate.delete(CacheConstants.ROUTE_KEY); + Boolean result = redisTemplate.delete(CacheConstants.ROUTE_KEY); log.info("初始化网关路由 {} ", result); routeConfService.routes().forEach(route -> { @@ -73,8 +73,8 @@ public class DynamicRouteInitRunner { vo.setPredicates(predicateObj.toList(PredicateDefinition.class)); log.info("加载路由ID:{},{}", route.getRouteId(), vo); - pigxRedisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(RouteDefinitionVo.class)); - pigxRedisTemplate.opsForHash().put(CacheConstants.ROUTE_KEY, route.getRouteId(), vo); + redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(RouteDefinitionVo.class)); + redisTemplate.opsForHash().put(CacheConstants.ROUTE_KEY, route.getRouteId(), vo); }); log.debug("初始化网关路由结束 "); } diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/MobileServiceImpl.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/MobileServiceImpl.java index e0dc1dbc8..cdac975a6 100644 --- a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/MobileServiceImpl.java +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/MobileServiceImpl.java @@ -45,7 +45,7 @@ import java.util.concurrent.TimeUnit; @Service @AllArgsConstructor public class MobileServiceImpl implements MobileService { - private final RedisTemplate pigxRedisTemplate; + private final RedisTemplate redisTemplate; private final SysUserMapper userMapper; @@ -67,7 +67,7 @@ public class MobileServiceImpl implements MobileService { return R.ok(Boolean.FALSE, "手机号未注册"); } - Object codeObj = pigxRedisTemplate.opsForValue().get(CommonConstants.DEFAULT_CODE_KEY + mobile); + Object codeObj = redisTemplate.opsForValue().get(CommonConstants.DEFAULT_CODE_KEY + mobile); if (codeObj != null) { log.info("手机号验证码未过期:{},{}", mobile, codeObj); @@ -76,7 +76,7 @@ public class MobileServiceImpl implements MobileService { String code = RandomUtil.randomNumbers(Integer.parseInt(SecurityConstants.CODE_SIZE)); log.debug("手机号生成验证码成功:{},{}", mobile, code); - pigxRedisTemplate.opsForValue().set( + redisTemplate.opsForValue().set( CommonConstants.DEFAULT_CODE_KEY + LoginTypeEnum.SMS.getType() + "@" + mobile , code, SecurityConstants.CODE_TIME, TimeUnit.SECONDS); return R.ok(Boolean.TRUE, code); diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysRouteConfServiceImpl.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysRouteConfServiceImpl.java index 904d013aa..d73e26ffe 100755 --- a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysRouteConfServiceImpl.java +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysRouteConfServiceImpl.java @@ -57,7 +57,7 @@ import java.util.stream.Collectors; @AllArgsConstructor @Service("sysRouteConfService") public class SysRouteConfServiceImpl extends ServiceImpl implements SysRouteConfService { - private final RedisTemplate pigxRedisTemplate; + private final RedisTemplate redisTemplate; private final ApplicationEventPublisher applicationEventPublisher; @@ -84,7 +84,7 @@ public class SysRouteConfServiceImpl extends ServiceImpl updateRoutes(JSONArray routes) { // 清空Redis 缓存 - Boolean result = pigxRedisTemplate.delete(CacheConstants.ROUTE_KEY); + Boolean result = redisTemplate.delete(CacheConstants.ROUTE_KEY); log.info("清空网关路由 {} ", result); // 遍历修改的routes,保存到Redis @@ -131,8 +131,8 @@ public class SysRouteConfServiceImpl extends ServiceImpl(RouteDefinitionVo.class)); - pigxRedisTemplate.opsForHash().put(CacheConstants.ROUTE_KEY, vo.getId(), vo); + redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(RouteDefinitionVo.class)); + redisTemplate.opsForHash().put(CacheConstants.ROUTE_KEY, vo.getId(), vo); routeDefinitionVoList.add(vo); }); diff --git a/pigx-visual/pigx-mp-manager/src/main/java/com/pig4cloud/pigx/mp/config/WxMpConfiguration.java b/pigx-visual/pigx-mp-manager/src/main/java/com/pig4cloud/pigx/mp/config/WxMpConfiguration.java index 6211b502a..409d936a6 100755 --- a/pigx-visual/pigx-mp-manager/src/main/java/com/pig4cloud/pigx/mp/config/WxMpConfiguration.java +++ b/pigx-visual/pigx-mp-manager/src/main/java/com/pig4cloud/pigx/mp/config/WxMpConfiguration.java @@ -55,7 +55,7 @@ public class WxMpConfiguration { , LocationHandler locationHandler, MenuHandler menuHandler , MsgHandler msgHandler, UnsubscribeHandler unsubscribeHandler , SubscribeHandler subscribeHandler, ScanHandler scanHandler - , RedisTemplate pigxRedisTemplate) { + , RedisTemplate redisTemplate) { this.accountService = accountService; this.logHandler = logHandler; this.nullHandler = nullHandler; @@ -67,7 +67,7 @@ public class WxMpConfiguration { this.unsubscribeHandler = unsubscribeHandler; this.subscribeHandler = subscribeHandler; this.scanHandler = scanHandler; - this.redisTemplate = pigxRedisTemplate; + this.redisTemplate = redisTemplate; } @PostConstruct diff --git a/pom.xml b/pom.xml index 4395ab56d..b6296f3ed 100644 --- a/pom.xml +++ b/pom.xml @@ -51,7 +51,7 @@ 1.8 2.1.5 1.1.2-beta5 - 4.5.12 + 4.5.13 0.0.9 2.9.2 1.9.4