-升级Spring Cloud Alibaba 2021.0.1.0,使用 spring.config.import 方式引入nacos配置 -拆分jeecg-boot-starter出来,使用独立项目维护dev
parent
17d42c8d63
commit
bad23add8c
File diff suppressed because one or more lines are too long
@ -1,96 +0,0 @@
|
||||
package org.jeecg.common.util;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
|
||||
/**
|
||||
* 使用Spring自身提供的地址匹配工具匹配URL
|
||||
* @author: jeecg-boot
|
||||
*/
|
||||
public class PathMatcherUtil {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String url = "/sys/dict/loadDictOrderByValue/tree,s2,2";
|
||||
String p = "/sys/dict/loadDictOrderByValue/*";
|
||||
|
||||
System.out.println(PathMatcherUtil.match(p,url));
|
||||
}
|
||||
|
||||
/**
|
||||
* 实际验证路径匹配权限
|
||||
*
|
||||
* @param matchPath 权限url
|
||||
* @param path 访问路径
|
||||
* @return 是否拥有权限
|
||||
*/
|
||||
public static boolean match(String matchPath, String path) {
|
||||
SpringAntMatcher springAntMatcher = new SpringAntMatcher(matchPath, true);
|
||||
return springAntMatcher.matches(path);
|
||||
}
|
||||
|
||||
/**
|
||||
* 实际验证路径匹配权限
|
||||
*
|
||||
* @param list 权限url
|
||||
* @param path 访问路径
|
||||
* @return 是否拥有权限
|
||||
*/
|
||||
public static boolean matches(Collection<String> list, String path) {
|
||||
for (String s : list) {
|
||||
SpringAntMatcher springAntMatcher = new SpringAntMatcher(s, true);
|
||||
if (springAntMatcher.matches(path)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 地址表达式匹配工具
|
||||
*/
|
||||
private static class SpringAntMatcher implements Matcher {
|
||||
private final AntPathMatcher antMatcher;
|
||||
private final String pattern;
|
||||
|
||||
private SpringAntMatcher(String pattern, boolean caseSensitive) {
|
||||
this.pattern = pattern;
|
||||
this.antMatcher = createMatcher(caseSensitive);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(String path) {
|
||||
return this.antMatcher.match(this.pattern, path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> extractUriTemplateVariables(String path) {
|
||||
return this.antMatcher.extractUriTemplateVariables(this.pattern, path);
|
||||
}
|
||||
|
||||
private static AntPathMatcher createMatcher(boolean caseSensitive) {
|
||||
AntPathMatcher matcher = new AntPathMatcher();
|
||||
matcher.setTrimTokens(false);
|
||||
matcher.setCaseSensitive(caseSensitive);
|
||||
return matcher;
|
||||
}
|
||||
}
|
||||
|
||||
private interface Matcher {
|
||||
|
||||
/**
|
||||
* 实际验证路径匹配权限
|
||||
* @param var1
|
||||
* @return
|
||||
*/
|
||||
boolean matches(String var1);
|
||||
|
||||
/**
|
||||
* 提取path中匹配到的部分
|
||||
* @param var1
|
||||
* @return
|
||||
*/
|
||||
Map<String, String> extractUriTemplateVariables(String var1);
|
||||
}
|
||||
}
|
||||
@ -1,25 +1,25 @@
|
||||
package org.jeecg.config.mybatis;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* 多租户 tenant_id存储器
|
||||
* @author: jeecg-boot
|
||||
*/
|
||||
@Slf4j
|
||||
public class TenantContext {
|
||||
private static ThreadLocal<String> currentTenant = new ThreadLocal<>();
|
||||
|
||||
public static void setTenant(String tenant) {
|
||||
log.debug(" setting tenant to " + tenant);
|
||||
currentTenant.set(tenant);
|
||||
}
|
||||
|
||||
public static String getTenant() {
|
||||
return currentTenant.get();
|
||||
}
|
||||
|
||||
public static void clear(){
|
||||
currentTenant.remove();
|
||||
}
|
||||
}
|
||||
//package org.jeecg.config.mybatis;
|
||||
//
|
||||
//import lombok.extern.slf4j.Slf4j;
|
||||
//
|
||||
///**
|
||||
// * 多租户 tenant_id存储器
|
||||
// * @author: jeecg-boot
|
||||
// */
|
||||
//@Slf4j
|
||||
//public class TenantContext {
|
||||
// private static ThreadLocal<String> currentTenant = new ThreadLocal<>();
|
||||
//
|
||||
// public static void setTenant(String tenant) {
|
||||
// log.debug(" setting tenant to " + tenant);
|
||||
// currentTenant.set(tenant);
|
||||
// }
|
||||
//
|
||||
// public static String getTenant() {
|
||||
// return currentTenant.get();
|
||||
// }
|
||||
//
|
||||
// public static void clear(){
|
||||
// currentTenant.remove();
|
||||
// }
|
||||
//}
|
||||
|
||||
@ -1,45 +0,0 @@
|
||||
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<artifactId>jeecg-boot-base</artifactId>
|
||||
<version>3.3.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<description>公共模块</description>
|
||||
<artifactId>jeecg-boot-base-tools</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!--集成springmvc框架 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<!-- Redis -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-pool2</artifactId>
|
||||
</dependency>
|
||||
<!--加载hutool-->
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-crypto</artifactId>
|
||||
</dependency>
|
||||
<!--加载beanutils-->
|
||||
<dependency>
|
||||
<groupId>commons-beanutils</groupId>
|
||||
<artifactId>commons-beanutils</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@ -1,23 +0,0 @@
|
||||
package org.jeecg.common.annotation;
|
||||
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* @Author:zyf
|
||||
* @Date:2019-07-31 10:43
|
||||
* @Description: 消息队列初始化注解
|
||||
**/
|
||||
@Documented
|
||||
@Inherited
|
||||
@Target({ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Component
|
||||
public @interface RabbitComponent {
|
||||
@AliasFor(
|
||||
annotation = Component.class
|
||||
)
|
||||
String value();
|
||||
}
|
||||
@ -1,144 +0,0 @@
|
||||
package org.jeecg.common.base;
|
||||
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
|
||||
import org.apache.commons.beanutils.ConvertUtils;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* BaseMap
|
||||
*
|
||||
* @author: scott
|
||||
* @date: 2020/01/01 16:17
|
||||
*/
|
||||
public class BaseMap extends HashMap<String, Object> {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
||||
public BaseMap() {
|
||||
|
||||
}
|
||||
|
||||
public BaseMap(Map<String, Object> map) {
|
||||
this.putAll(map);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public BaseMap put(String key, Object value) {
|
||||
super.put(key, Optional.ofNullable(value).orElse(""));
|
||||
return this;
|
||||
}
|
||||
|
||||
public BaseMap add(String key, Object value) {
|
||||
super.put(key, Optional.ofNullable(value).orElse(""));
|
||||
return this;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T get(String key) {
|
||||
Object obj = super.get(key);
|
||||
if (ObjectUtil.isNotEmpty(obj)) {
|
||||
return (T) obj;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Boolean getBoolean(String key) {
|
||||
Object obj = super.get(key);
|
||||
if (ObjectUtil.isNotEmpty(obj)) {
|
||||
return Boolean.valueOf(obj.toString());
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public Long getLong(String key) {
|
||||
Object v = get(key);
|
||||
if (ObjectUtil.isNotEmpty(v)) {
|
||||
return new Long(v.toString());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Long[] getLongs(String key) {
|
||||
Object v = get(key);
|
||||
if (ObjectUtil.isNotEmpty(v)) {
|
||||
return (Long[]) v;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Long> getListLong(String key) {
|
||||
List<String> list = get(key);
|
||||
if (ObjectUtil.isNotEmpty(list)) {
|
||||
return list.stream().map(e -> new Long(e)).collect(Collectors.toList());
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Long[] getLongIds(String key) {
|
||||
Object ids = get(key);
|
||||
if (ObjectUtil.isNotEmpty(ids)) {
|
||||
return (Long[]) ConvertUtils.convert(ids.toString().split(","), Long.class);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Integer getInt(String key, Integer def) {
|
||||
Object v = get(key);
|
||||
if (ObjectUtil.isNotEmpty(v)) {
|
||||
return Integer.parseInt(v.toString());
|
||||
} else {
|
||||
return def;
|
||||
}
|
||||
}
|
||||
|
||||
public Integer getInt(String key) {
|
||||
Object v = get(key);
|
||||
if (ObjectUtil.isNotEmpty(v)) {
|
||||
return Integer.parseInt(v.toString());
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public BigDecimal getBigDecimal(String key) {
|
||||
Object v = get(key);
|
||||
if (ObjectUtil.isNotEmpty(v)) {
|
||||
return new BigDecimal(v.toString());
|
||||
}
|
||||
return new BigDecimal("0");
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T get(String key, T def) {
|
||||
Object obj = super.get(key);
|
||||
if (ObjectUtil.isEmpty(obj)) {
|
||||
return def;
|
||||
}
|
||||
return (T) obj;
|
||||
}
|
||||
|
||||
public static BaseMap toBaseMap(Map<String, Object> obj) {
|
||||
BaseMap map = new BaseMap();
|
||||
map.putAll(obj);
|
||||
return map;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -1,27 +0,0 @@
|
||||
package org.jeecg.common.config;
|
||||
|
||||
import org.jeecg.common.util.SpringContextHolder;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
/**
|
||||
* SpringContextHolder注册用
|
||||
*
|
||||
* @author: scott
|
||||
* @date: 2020/01/01 16:00
|
||||
*/
|
||||
@Configuration
|
||||
public class CommonConfig {
|
||||
|
||||
/**
|
||||
* Spring上下文工具配置
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(SpringContextHolder.class)
|
||||
public SpringContextHolder springContextHolder() {
|
||||
SpringContextHolder holder = new SpringContextHolder();
|
||||
return holder;
|
||||
}
|
||||
}
|
||||
@ -1,23 +0,0 @@
|
||||
package org.jeecg.common.constant;
|
||||
|
||||
/**
|
||||
* @Description: GlobalConstants
|
||||
* @author: scott
|
||||
* @date: 2020/01/01 16:01
|
||||
*/
|
||||
public class GlobalConstants {
|
||||
|
||||
/**
|
||||
* 业务处理器beanName传递参数
|
||||
*/
|
||||
public static final String HANDLER_NAME = "handlerName";
|
||||
/**
|
||||
* 路由刷新触发器
|
||||
*/
|
||||
public static final String LODER_ROUDER_HANDLER = "loderRouderHandler";
|
||||
|
||||
/**
|
||||
* redis消息通道名称
|
||||
*/
|
||||
public static final String REDIS_TOPIC_NAME="jeecg_redis_topic";
|
||||
}
|
||||
@ -1,108 +0,0 @@
|
||||
package org.jeecg.common.enums;
|
||||
|
||||
/**
|
||||
* @Description: 异常错误信息定义
|
||||
* @author: zyf
|
||||
* @date: 2022/4/14 10:05
|
||||
*/
|
||||
public enum SentinelErrorInfoEnum {
|
||||
|
||||
/**
|
||||
* 流控异常
|
||||
*/
|
||||
FlowException("访问频繁,请稍候再试"),
|
||||
|
||||
/**
|
||||
* 热点参数异常
|
||||
*/
|
||||
ParamFlowException("热点参数限流"),
|
||||
|
||||
/**
|
||||
* 系统规则限流或降级
|
||||
*/
|
||||
SystemBlockException("系统规则限流或降级"),
|
||||
|
||||
/**
|
||||
* 授权规则不通过
|
||||
*/
|
||||
AuthorityException("授权规则不通过"),
|
||||
|
||||
/**
|
||||
* 授权规则不通过
|
||||
*/
|
||||
UnknownError("未知异常"),
|
||||
|
||||
/**
|
||||
* 服务降级
|
||||
*/
|
||||
DegradeException("服务降级");
|
||||
|
||||
|
||||
/**
|
||||
* 错误信息
|
||||
*/
|
||||
String error;
|
||||
|
||||
/**
|
||||
* 错误代码
|
||||
*/
|
||||
Integer code;
|
||||
|
||||
|
||||
public String getError() {
|
||||
return error;
|
||||
}
|
||||
|
||||
public void setError(String error) {
|
||||
this.error = error;
|
||||
}
|
||||
|
||||
public Integer getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(Integer code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造器
|
||||
*
|
||||
* @param error 错误信息
|
||||
* @param code 错误代码
|
||||
*/
|
||||
SentinelErrorInfoEnum(String error, Integer code) {
|
||||
this.error = error;
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造器
|
||||
*
|
||||
* @param error 错误信息
|
||||
*/
|
||||
SentinelErrorInfoEnum(String error) {
|
||||
this.error = error;
|
||||
this.code = 500;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据异常名称匹配
|
||||
*
|
||||
* @param throwable 异常
|
||||
* @return String 错误信息
|
||||
*/
|
||||
public static SentinelErrorInfoEnum getErrorByException(Throwable throwable) {
|
||||
if(throwable==null){
|
||||
return null;
|
||||
}
|
||||
|
||||
String exceptionClass = throwable.getClass().getSimpleName();
|
||||
for (SentinelErrorInfoEnum e : SentinelErrorInfoEnum.values()) {
|
||||
if (exceptionClass.equals(e.name())) {
|
||||
return e;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -1,15 +0,0 @@
|
||||
package org.jeecg.common.exception;
|
||||
|
||||
/**
|
||||
* @Description: jeecg-cloud自定义异常
|
||||
* @Author: zyf
|
||||
* @Date: 2022-05-30
|
||||
*/
|
||||
public class JeecgCloudException extends RuntimeException {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public JeecgCloudException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,34 +0,0 @@
|
||||
package org.jeecg.common.modules.redis.client;
|
||||
|
||||
import org.jeecg.common.base.BaseMap;
|
||||
import org.jeecg.common.constant.GlobalConstants;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* @Description: redis客户端
|
||||
* @author: scott
|
||||
* @date: 2020/01/01 16:01
|
||||
*/
|
||||
@Configuration
|
||||
public class JeecgRedisClient {
|
||||
|
||||
@Resource
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
|
||||
/**
|
||||
* 发送消息
|
||||
*
|
||||
* @param handlerName
|
||||
* @param params
|
||||
*/
|
||||
public void sendMessage(String handlerName, BaseMap params) {
|
||||
params.put(GlobalConstants.HANDLER_NAME, handlerName);
|
||||
redisTemplate.convertAndSend(GlobalConstants.REDIS_TOPIC_NAME, params);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -1,141 +0,0 @@
|
||||
package org.jeecg.common.modules.redis.config;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
||||
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.jeecg.common.constant.CacheConstant;
|
||||
import org.jeecg.common.constant.GlobalConstants;
|
||||
|
||||
import org.jeecg.common.modules.redis.receiver.RedisReceiver;
|
||||
import org.jeecg.common.modules.redis.writer.JeecgRedisCacheWriter;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.annotation.CachingConfigurerSupport;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.cache.RedisCacheConfiguration;
|
||||
import org.springframework.data.redis.cache.RedisCacheManager;
|
||||
import org.springframework.data.redis.cache.RedisCacheWriter;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.listener.ChannelTopic;
|
||||
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
||||
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
|
||||
import org.springframework.data.redis.serializer.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.Duration;
|
||||
|
||||
import static java.util.Collections.singletonMap;
|
||||
|
||||
/**
|
||||
* 开启缓存支持
|
||||
* @author zyf
|
||||
* @Return:
|
||||
*/
|
||||
@Slf4j
|
||||
@EnableCaching
|
||||
@Configuration
|
||||
public class RedisConfig extends CachingConfigurerSupport {
|
||||
|
||||
@Resource
|
||||
private LettuceConnectionFactory lettuceConnectionFactory;
|
||||
|
||||
/**
|
||||
* RedisTemplate配置
|
||||
* @param lettuceConnectionFactory
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
|
||||
log.info(" --- redis config init --- ");
|
||||
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = jacksonSerializer();
|
||||
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
|
||||
redisTemplate.setConnectionFactory(lettuceConnectionFactory);
|
||||
RedisSerializer<String> stringSerializer = new StringRedisSerializer();
|
||||
|
||||
// key序列化
|
||||
redisTemplate.setKeySerializer(stringSerializer);
|
||||
// value序列化
|
||||
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
|
||||
// Hash key序列化
|
||||
redisTemplate.setHashKeySerializer(stringSerializer);
|
||||
// Hash value序列化
|
||||
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
|
||||
redisTemplate.afterPropertiesSet();
|
||||
return redisTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存配置管理器
|
||||
*
|
||||
* @param factory
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public CacheManager cacheManager(LettuceConnectionFactory factory) {
|
||||
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = jacksonSerializer();
|
||||
// 配置序列化(解决乱码的问题),并且配置缓存默认有效期 6小时
|
||||
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofHours(6));
|
||||
RedisCacheConfiguration redisCacheConfiguration = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
|
||||
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer));
|
||||
//.disableCachingNullValues();
|
||||
|
||||
// 以锁写入的方式创建RedisCacheWriter对象
|
||||
//update-begin-author:taoyan date:20210316 for:注解CacheEvict根据key删除redis支持通配符*
|
||||
RedisCacheWriter writer = new JeecgRedisCacheWriter(factory, Duration.ofMillis(50L));
|
||||
//RedisCacheWriter.lockingRedisCacheWriter(factory);
|
||||
// 创建默认缓存配置对象
|
||||
/* 默认配置,设置缓存有效期 1小时*/
|
||||
//RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofHours(1));
|
||||
// 自定义配置test:demo 的超时时间为 5分钟
|
||||
RedisCacheManager cacheManager = RedisCacheManager.builder(writer).cacheDefaults(redisCacheConfiguration)
|
||||
.withInitialCacheConfigurations(singletonMap(CacheConstant.SYS_DICT_TABLE_CACHE,
|
||||
RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(10)).disableCachingNullValues()
|
||||
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))))
|
||||
.withInitialCacheConfigurations(singletonMap(CacheConstant.TEST_DEMO_CACHE, RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(5)).disableCachingNullValues()))
|
||||
.withInitialCacheConfigurations(singletonMap(CacheConstant.PLUGIN_MALL_RANKING, RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofHours(24)).disableCachingNullValues()))
|
||||
.withInitialCacheConfigurations(singletonMap(CacheConstant.PLUGIN_MALL_PAGE_LIST, RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofHours(24)).disableCachingNullValues()))
|
||||
.transactionAware().build();
|
||||
//update-end-author:taoyan date:20210316 for:注解CacheEvict根据key删除redis支持通配符*
|
||||
return cacheManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* redis 监听配置
|
||||
*
|
||||
* @param redisConnectionFactory redis 配置
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public RedisMessageListenerContainer redisContainer(RedisConnectionFactory redisConnectionFactory, RedisReceiver redisReceiver, MessageListenerAdapter commonListenerAdapter) {
|
||||
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
|
||||
container.setConnectionFactory(redisConnectionFactory);
|
||||
container.addMessageListener(commonListenerAdapter, new ChannelTopic(GlobalConstants.REDIS_TOPIC_NAME));
|
||||
return container;
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
MessageListenerAdapter commonListenerAdapter(RedisReceiver redisReceiver) {
|
||||
MessageListenerAdapter messageListenerAdapter = new MessageListenerAdapter(redisReceiver, "onMessage");
|
||||
messageListenerAdapter.setSerializer(jacksonSerializer());
|
||||
return messageListenerAdapter;
|
||||
}
|
||||
|
||||
private Jackson2JsonRedisSerializer jacksonSerializer() {
|
||||
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
||||
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
|
||||
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
|
||||
return jackson2JsonRedisSerializer;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -1,18 +0,0 @@
|
||||
package org.jeecg.common.modules.redis.listener;
|
||||
|
||||
import org.jeecg.common.base.BaseMap;
|
||||
|
||||
/**
|
||||
* @Description: 自定义消息监听
|
||||
* @author: scott
|
||||
* @date: 2020/01/01 16:02
|
||||
*/
|
||||
public interface JeecgRedisListener {
|
||||
/**
|
||||
* 接受消息
|
||||
*
|
||||
* @param message
|
||||
*/
|
||||
void onMessage(BaseMap message);
|
||||
|
||||
}
|
||||
@ -1,33 +0,0 @@
|
||||
package org.jeecg.common.modules.redis.receiver;
|
||||
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import lombok.Data;
|
||||
import org.jeecg.common.base.BaseMap;
|
||||
import org.jeecg.common.constant.GlobalConstants;
|
||||
import org.jeecg.common.modules.redis.listener.JeecgRedisListener;
|
||||
import org.jeecg.common.util.SpringContextHolder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author zyf
|
||||
*/
|
||||
@Component
|
||||
@Data
|
||||
public class RedisReceiver {
|
||||
|
||||
|
||||
/**
|
||||
* 接受消息并调用业务逻辑处理器
|
||||
*
|
||||
* @param params
|
||||
*/
|
||||
public void onMessage(BaseMap params) {
|
||||
Object handlerName = params.get(GlobalConstants.HANDLER_NAME);
|
||||
JeecgRedisListener messageListener = SpringContextHolder.getHandler(handlerName.toString(), JeecgRedisListener.class);
|
||||
if (ObjectUtil.isNotEmpty(messageListener)) {
|
||||
messageListener.onMessage(params);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,250 +0,0 @@
|
||||
package org.jeecg.common.modules.redis.writer;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.Collections;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.dao.PessimisticLockingFailureException;
|
||||
import org.springframework.data.redis.cache.CacheStatistics;
|
||||
import org.springframework.data.redis.cache.CacheStatisticsCollector;
|
||||
import org.springframework.data.redis.cache.RedisCacheWriter;
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.RedisStringCommands.SetOption;
|
||||
import org.springframework.data.redis.core.types.Expiration;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* 该类参照 DefaultRedisCacheWriter 重写了 remove 方法实现通配符*删除
|
||||
*
|
||||
* @author: scott
|
||||
* @date: 2020/01/01 16:18
|
||||
*/
|
||||
@Slf4j
|
||||
public class JeecgRedisCacheWriter implements RedisCacheWriter {
|
||||
|
||||
private final RedisConnectionFactory connectionFactory;
|
||||
private final Duration sleepTime;
|
||||
|
||||
public JeecgRedisCacheWriter(RedisConnectionFactory connectionFactory) {
|
||||
this(connectionFactory, Duration.ZERO);
|
||||
}
|
||||
|
||||
public JeecgRedisCacheWriter(RedisConnectionFactory connectionFactory, Duration sleepTime) {
|
||||
Assert.notNull(connectionFactory, "ConnectionFactory must not be null!");
|
||||
Assert.notNull(sleepTime, "SleepTime must not be null!");
|
||||
this.connectionFactory = connectionFactory;
|
||||
this.sleepTime = sleepTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void put(String name, byte[] key, byte[] value, @Nullable Duration ttl) {
|
||||
Assert.notNull(name, "Name must not be null!");
|
||||
Assert.notNull(key, "Key must not be null!");
|
||||
Assert.notNull(value, "Value must not be null!");
|
||||
this.execute(name, (connection) -> {
|
||||
if (shouldExpireWithin(ttl)) {
|
||||
connection.set(key, value, Expiration.from(ttl.toMillis(), TimeUnit.MILLISECONDS), SetOption.upsert());
|
||||
} else {
|
||||
connection.set(key, value);
|
||||
}
|
||||
|
||||
return "OK";
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] get(String name, byte[] key) {
|
||||
Assert.notNull(name, "Name must not be null!");
|
||||
Assert.notNull(key, "Key must not be null!");
|
||||
return (byte[])this.execute(name, (connection) -> {
|
||||
return connection.get(key);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] putIfAbsent(String name, byte[] key, byte[] value, @Nullable Duration ttl) {
|
||||
Assert.notNull(name, "Name must not be null!");
|
||||
Assert.notNull(key, "Key must not be null!");
|
||||
Assert.notNull(value, "Value must not be null!");
|
||||
return (byte[])this.execute(name, (connection) -> {
|
||||
if (this.isLockingCacheWriter()) {
|
||||
this.doLock(name, connection);
|
||||
}
|
||||
|
||||
Object var7;
|
||||
try {
|
||||
boolean put;
|
||||
if (shouldExpireWithin(ttl)) {
|
||||
put = connection.set(key, value, Expiration.from(ttl), SetOption.ifAbsent());
|
||||
} else {
|
||||
put = connection.setNX(key, value);
|
||||
}
|
||||
|
||||
if (!put) {
|
||||
byte[] var11 = connection.get(key);
|
||||
return var11;
|
||||
}
|
||||
|
||||
var7 = null;
|
||||
} finally {
|
||||
if (this.isLockingCacheWriter()) {
|
||||
this.doUnlock(name, connection);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return (byte[])var7;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(String name, byte[] key) {
|
||||
Assert.notNull(name, "Name must not be null!");
|
||||
Assert.notNull(key, "Key must not be null!");
|
||||
String keyString = new String(key);
|
||||
log.info("redis remove key:" + keyString);
|
||||
String keyIsAll = "*";
|
||||
if(keyString!=null && keyString.endsWith(keyIsAll)){
|
||||
execute(name, connection -> {
|
||||
// 获取某个前缀所拥有的所有的键,某个前缀开头,后面肯定是*
|
||||
Set<byte[]> keys = connection.keys(key);
|
||||
int delNum = 0;
|
||||
for (byte[] keyByte : keys) {
|
||||
delNum += connection.del(keyByte);
|
||||
}
|
||||
return delNum;
|
||||
});
|
||||
}else{
|
||||
this.execute(name, (connection) -> {
|
||||
return connection.del(new byte[][]{key});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clean(String name, byte[] pattern) {
|
||||
Assert.notNull(name, "Name must not be null!");
|
||||
Assert.notNull(pattern, "Pattern must not be null!");
|
||||
this.execute(name, (connection) -> {
|
||||
boolean wasLocked = false;
|
||||
|
||||
try {
|
||||
if (this.isLockingCacheWriter()) {
|
||||
this.doLock(name, connection);
|
||||
wasLocked = true;
|
||||
}
|
||||
|
||||
byte[][] keys = (byte[][])((Set)Optional.ofNullable(connection.keys(pattern)).orElse(Collections.emptySet())).toArray(new byte[0][]);
|
||||
if (keys.length > 0) {
|
||||
connection.del(keys);
|
||||
}
|
||||
} finally {
|
||||
if (wasLocked && this.isLockingCacheWriter()) {
|
||||
this.doUnlock(name, connection);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return "OK";
|
||||
});
|
||||
}
|
||||
|
||||
void lock(String name) {
|
||||
this.execute(name, (connection) -> {
|
||||
return this.doLock(name, connection);
|
||||
});
|
||||
}
|
||||
|
||||
void unlock(String name) {
|
||||
this.executeLockFree((connection) -> {
|
||||
this.doUnlock(name, connection);
|
||||
});
|
||||
}
|
||||
|
||||
private Boolean doLock(String name, RedisConnection connection) {
|
||||
return connection.setNX(createCacheLockKey(name), new byte[0]);
|
||||
}
|
||||
|
||||
private Long doUnlock(String name, RedisConnection connection) {
|
||||
return connection.del(new byte[][]{createCacheLockKey(name)});
|
||||
}
|
||||
|
||||
boolean doCheckLock(String name, RedisConnection connection) {
|
||||
return connection.exists(createCacheLockKey(name));
|
||||
}
|
||||
|
||||
private boolean isLockingCacheWriter() {
|
||||
return !this.sleepTime.isZero() && !this.sleepTime.isNegative();
|
||||
}
|
||||
|
||||
private <T> T execute(String name, Function<RedisConnection, T> callback) {
|
||||
RedisConnection connection = this.connectionFactory.getConnection();
|
||||
|
||||
try {
|
||||
this.checkAndPotentiallyWaitUntilUnlocked(name, connection);
|
||||
return callback.apply(connection);
|
||||
} finally {
|
||||
connection.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void executeLockFree(Consumer<RedisConnection> callback) {
|
||||
RedisConnection connection = this.connectionFactory.getConnection();
|
||||
|
||||
try {
|
||||
callback.accept(connection);
|
||||
} finally {
|
||||
connection.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void checkAndPotentiallyWaitUntilUnlocked(String name, RedisConnection connection) {
|
||||
if (this.isLockingCacheWriter()) {
|
||||
try {
|
||||
while(this.doCheckLock(name, connection)) {
|
||||
Thread.sleep(this.sleepTime.toMillis());
|
||||
}
|
||||
|
||||
} catch (InterruptedException var4) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new PessimisticLockingFailureException(String.format("Interrupted while waiting to unlock cache %s", name), var4);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean shouldExpireWithin(@Nullable Duration ttl) {
|
||||
return ttl != null && !ttl.isZero() && !ttl.isNegative();
|
||||
}
|
||||
|
||||
private static byte[] createCacheLockKey(String name) {
|
||||
return (name + "~lock").getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
//update-begin-author:zyf date:20220216 for:升级springboot版本到2.4.0+以后需要实现的方法*
|
||||
private final CacheStatisticsCollector statistics = CacheStatisticsCollector.create();
|
||||
@Override
|
||||
public CacheStatistics getCacheStatistics(String cacheName) {
|
||||
return statistics.getCacheStatistics(cacheName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearStatistics(String name) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public RedisCacheWriter withStatisticsCollector(CacheStatisticsCollector cacheStatisticsCollector) {
|
||||
return null;
|
||||
}
|
||||
//update-begin-author:zyf date:20220216 for:升级springboot版本到2.4.0+以后需要实现的方法*
|
||||
}
|
||||
@ -1,5 +0,0 @@
|
||||
server:
|
||||
port: 7002
|
||||
spring:
|
||||
application:
|
||||
name: jeecg-demo
|
||||
@ -1,59 +0,0 @@
|
||||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>jeecg-boot-starter</artifactId>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<version>3.3.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>jeecg-boot-starter-cloud</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<artifactId>jeecg-system-cloud-api</artifactId>
|
||||
</dependency>
|
||||
<!-- Nacos注册中心 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
<!-- Nacos配置中心 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</dependency>
|
||||
<!-- feign -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
<!-- sentinel限流熔断降级 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||
</dependency>
|
||||
<!--sentinel持久化 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-datasource-nacos</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- springboot up 2.6.6 -->
|
||||
<!-- Alibaba Nacos 配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-bootstrap</artifactId>
|
||||
</dependency>
|
||||
<!-- SpringCloud 负载均衡 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||
</dependency>
|
||||
<!-- springboot up 2.6.6 -->
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@ -1,31 +0,0 @@
|
||||
package org.jeecg.starter.cloud.config;
|
||||
|
||||
/**
|
||||
* @Description: 跨域设置 (升级SpringBoot2.6.6)
|
||||
* @author: zyf
|
||||
* @date: 2022/02/21
|
||||
* @version: V1.0
|
||||
*/
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.cors.CorsConfiguration;
|
||||
import org.springframework.web.cors.reactive.CorsWebFilter;
|
||||
import org.springframework.web.util.pattern.PathPatternParser;
|
||||
|
||||
@Configuration
|
||||
public class GwCorsFilter {
|
||||
|
||||
@Bean
|
||||
public CorsWebFilter corsFilter() {
|
||||
CorsConfiguration config = new CorsConfiguration();
|
||||
config.setAllowCredentials(true);
|
||||
config.addAllowedOriginPattern("*");
|
||||
config.addAllowedHeader("*");
|
||||
config.addAllowedMethod("*");
|
||||
config.setMaxAge(18000L);
|
||||
org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource source =
|
||||
new org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource(new PathPatternParser());
|
||||
source.registerCorsConfiguration("/**", config);
|
||||
return new CorsWebFilter(source);
|
||||
}
|
||||
}
|
||||
@ -1,24 +0,0 @@
|
||||
//package org.jeecg.starter.cloud.config;
|
||||
//
|
||||
//import feign.Client;
|
||||
//import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
//import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
|
||||
//import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory;
|
||||
//import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient;
|
||||
//import org.springframework.context.annotation.Bean;
|
||||
//import org.springframework.context.annotation.Configuration;
|
||||
//
|
||||
//@Configuration
|
||||
//public class PersonBeanConfiguration {
|
||||
//
|
||||
// /**
|
||||
// * 创建FeignClient
|
||||
// */
|
||||
// @Bean
|
||||
// @ConditionalOnMissingBean
|
||||
// public Client feignClient(CachingSpringLoadBalancerFactory cachingFactory,
|
||||
// SpringClientFactory clientFactory) {
|
||||
// return new LoadBalancerFeignClient(new Client.Default(null, null),
|
||||
// cachingFactory, clientFactory);
|
||||
// }
|
||||
//}
|
||||
@ -1,6 +0,0 @@
|
||||
//package org.jeecg.starter.cloud.feign;
|
||||
//
|
||||
//public interface IJeecgFeignService {
|
||||
//
|
||||
// <T> T newInstance(Class<T> apiType, String name);
|
||||
//}
|
||||
@ -1,60 +0,0 @@
|
||||
//package org.jeecg.starter.cloud.feign.impl;
|
||||
//
|
||||
//import feign.*;
|
||||
//import feign.codec.Decoder;
|
||||
//import feign.codec.Encoder;
|
||||
//import lombok.extern.slf4j.Slf4j;
|
||||
//import org.jeecg.common.constant.CommonConstant;
|
||||
//import org.jeecg.starter.cloud.feign.IJeecgFeignService;
|
||||
//import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
//import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
//import org.springframework.cloud.openfeign.FeignAutoConfiguration;
|
||||
//import org.springframework.cloud.openfeign.FeignClientsConfiguration;
|
||||
//import org.springframework.context.annotation.Import;
|
||||
//import org.springframework.stereotype.Service;
|
||||
//import org.springframework.web.context.request.RequestContextHolder;
|
||||
//import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
//
|
||||
//import javax.servlet.http.HttpServletRequest;
|
||||
//
|
||||
//@Service
|
||||
//@Slf4j
|
||||
//@ConditionalOnClass(Feign.class)
|
||||
//@AutoConfigureBefore(FeignAutoConfiguration.class)
|
||||
//@Import(FeignClientsConfiguration.class)
|
||||
//public class JeecgFeignService implements IJeecgFeignService {
|
||||
//
|
||||
//
|
||||
// //Feign 原生构造器
|
||||
// Feign.Builder builder;
|
||||
//
|
||||
// //创建构造器
|
||||
// public JeecgFeignService(Decoder decoder, Encoder encoder, Client client, Contract contract) {
|
||||
// this.builder = Feign.builder()
|
||||
// .client(client)
|
||||
// .encoder(encoder)
|
||||
// .decoder(decoder)
|
||||
// .contract(contract);
|
||||
//
|
||||
// builder.requestInterceptor(requestTemplate -> {
|
||||
// ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
// if (null != attributes) {
|
||||
// HttpServletRequest request = attributes.getRequest();
|
||||
// log.info("Feign request: {}", request.getRequestURI());
|
||||
// // 将token信息放入header中
|
||||
// String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
|
||||
// if(token==null){
|
||||
// token = request.getParameter("token");
|
||||
// }
|
||||
// log.info("Feign request token: {}", token);
|
||||
// requestTemplate.header(CommonConstant.X_ACCESS_TOKEN, token);
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
//
|
||||
//
|
||||
// @Override
|
||||
// public <T> T newInstance(Class<T> clientClass, String serviceName) {
|
||||
// return builder.target(clientClass, String.format("http://%s/", serviceName));
|
||||
// }
|
||||
//}
|
||||
@ -1,55 +0,0 @@
|
||||
package org.jeecg.starter.cloud.handler;
|
||||
|
||||
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
|
||||
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
|
||||
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
|
||||
import com.alibaba.csp.sentinel.slots.system.SystemBlockException;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* @Description: 全局Sentinel自定义信息处理(需要启动Sentinel客户端)
|
||||
* @author: zyf
|
||||
* @date: 2022/02/18
|
||||
* @version: V1.0
|
||||
*/
|
||||
@Configuration
|
||||
public class CustomSentinelExceptionHandler implements BlockExceptionHandler {
|
||||
|
||||
@Override
|
||||
public void handle(HttpServletRequest request, HttpServletResponse response, BlockException ex) throws Exception {
|
||||
|
||||
String msg = null;
|
||||
|
||||
if (ex instanceof FlowException) {
|
||||
msg = "访问频繁,请稍候再试";
|
||||
|
||||
} else if (ex instanceof DegradeException) {
|
||||
msg = "系统降级";
|
||||
|
||||
} else if (ex instanceof ParamFlowException) {
|
||||
msg = "热点参数限流";
|
||||
|
||||
} else if (ex instanceof SystemBlockException) {
|
||||
msg = "系统规则限流或降级";
|
||||
|
||||
} else if (ex instanceof AuthorityException) {
|
||||
msg = "授权规则不通过";
|
||||
|
||||
} else {
|
||||
msg = "未知限流降级";
|
||||
}
|
||||
// http状态码
|
||||
response.setStatus(200);
|
||||
response.setCharacterEncoding("utf-8");
|
||||
response.setHeader("Content-Type", "application/json;charset=utf-8");
|
||||
response.setContentType("application/json;charset=utf-8");
|
||||
response.getWriter().write("{\"code\":500,\"message\":"+msg+"}");
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,31 +0,0 @@
|
||||
package org.jeecg.starter.cloud.interceptor;
|
||||
|
||||
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.jeecg.common.util.IpUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* 【示例】sentinel ip和参数授权规则拦截器(黑名单白名单)
|
||||
* 1. 有参数origin的时候走参数拦截规则
|
||||
* 2. 当参数为空时走ip拦截模式
|
||||
*
|
||||
* @author zyf
|
||||
*/
|
||||
@Component
|
||||
public class DefaultRequestOriginParser implements RequestOriginParser {
|
||||
@Override
|
||||
public String parseOrigin(HttpServletRequest request) {
|
||||
//基于请求参数,origin对应授权规则中的流控应用名称,也可通过getHeader传参
|
||||
String origin = request.getParameter("origin");
|
||||
if (StringUtils.isNotEmpty(origin)) {
|
||||
return origin;
|
||||
} else {
|
||||
//当参数为空使用ip拦截模式
|
||||
String ip = IpUtils.getIpAddr(request);
|
||||
return ip;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,21 +0,0 @@
|
||||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>jeecg-boot-starter</artifactId>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<version>3.3.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>jeecg-boot-starter-job</artifactId>
|
||||
<description>jeecg-boot-starter-定时任务</description>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.xuxueli</groupId>
|
||||
<artifactId>xxl-job-core</artifactId>
|
||||
<version>${xxl-job-core.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@ -1,18 +0,0 @@
|
||||
package org.jeecg.boot.starter.job.annotation;
|
||||
|
||||
import org.jeecg.boot.starter.job.config.XxlJobConfiguration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* @author zyf
|
||||
*/
|
||||
@Target({ ElementType.TYPE })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Inherited
|
||||
@Import({ XxlJobConfiguration.class })
|
||||
public @interface EnableXxlJob {
|
||||
|
||||
}
|
||||
@ -1,35 +0,0 @@
|
||||
package org.jeecg.boot.starter.job.prop;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
@Data
|
||||
@ConfigurationProperties(prefix = "jeecg.xxljob")
|
||||
public class XxlJobProperties {
|
||||
|
||||
|
||||
private String adminAddresses;
|
||||
|
||||
|
||||
private String appname;
|
||||
|
||||
|
||||
private String ip;
|
||||
|
||||
|
||||
private int port;
|
||||
|
||||
|
||||
private String accessToken;
|
||||
|
||||
|
||||
private String logPath;
|
||||
|
||||
|
||||
private int logRetentionDays;
|
||||
|
||||
/**
|
||||
* 是否开启xxljob
|
||||
*/
|
||||
private Boolean enable = true;
|
||||
}
|
||||
@ -1,2 +0,0 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
org.jeecg.boot.starter.job.config.XxlJobConfiguration
|
||||
@ -1,41 +0,0 @@
|
||||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>jeecg-boot-starter</artifactId>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<version>3.3.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>jeecg-boot-starter-lock</artifactId>
|
||||
<description>jeecg-boot-starter-分布式锁</description>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<artifactId>jeecg-boot-base-tools</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.redisson</groupId>
|
||||
<artifactId>redisson</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-aop</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@ -1,57 +0,0 @@
|
||||
package org.jeecg.boot.starter.lock.annotation;
|
||||
|
||||
import org.jeecg.boot.starter.lock.enums.LockModel;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* Redisson分布式锁注解
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2020-11-11
|
||||
*/
|
||||
@Target({ElementType.TYPE, ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Inherited
|
||||
public @interface JLock {
|
||||
|
||||
/**
|
||||
* 锁的模式:如果不设置,自动模式,当参数只有一个.使用 REENTRANT 参数多个 MULTIPLE
|
||||
*/
|
||||
LockModel lockModel() default LockModel.AUTO;
|
||||
|
||||
/**
|
||||
* 如果keys有多个,如果不设置,则使用 联锁
|
||||
* @return
|
||||
*/
|
||||
String[] lockKey() default {};
|
||||
|
||||
/**
|
||||
* key的静态常量:当key的spel的值是LIST,数组时使用+号连接将会被spel认为这个变量是个字符串
|
||||
* @return
|
||||
*/
|
||||
String keyConstant() default "";
|
||||
|
||||
|
||||
/**
|
||||
* 锁超时时间,默认30000毫秒
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
long expireSeconds() default 30000L;
|
||||
|
||||
/**
|
||||
* 等待加锁超时时间,默认10000毫秒 -1 则表示一直等待
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
long waitTime() default 10000L;
|
||||
|
||||
/**
|
||||
* 未取到锁时提示信息
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String failMsg() default "获取锁失败,请稍后重试";
|
||||
}
|
||||
@ -1,36 +0,0 @@
|
||||
package org.jeecg.boot.starter.lock.annotation;
|
||||
|
||||
/**
|
||||
* @author zyf
|
||||
*/
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 防止重复提交的注解
|
||||
*
|
||||
* @author 2019年6月18日
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.METHOD})
|
||||
@Documented
|
||||
public @interface JRepeat {
|
||||
|
||||
/**
|
||||
* 超时时间
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
int lockTime();
|
||||
|
||||
|
||||
/**
|
||||
* redis 锁key的
|
||||
*
|
||||
* @return redis 锁key
|
||||
*/
|
||||
String lockKey() default "";
|
||||
|
||||
|
||||
|
||||
}
|
||||
@ -1,67 +0,0 @@
|
||||
package org.jeecg.boot.starter.lock.aspect;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.expression.EvaluationContext;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.ExpressionParser;
|
||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author zyf
|
||||
*/
|
||||
@Slf4j
|
||||
public class BaseAspect {
|
||||
|
||||
/**
|
||||
* 通过spring SpEL 获取参数
|
||||
*
|
||||
* @param key 定义的key值 以#开头 例如:#user
|
||||
* @param parameterNames 形参
|
||||
* @param values 形参值
|
||||
* @param keyConstant key的常亮
|
||||
* @return
|
||||
*/
|
||||
public List<String> getValueBySpEL(String key, String[] parameterNames, Object[] values, String keyConstant) {
|
||||
List<String> keys = new ArrayList<>();
|
||||
if (!key.contains("#")) {
|
||||
String s = "redis:lock:" + key + keyConstant;
|
||||
log.debug("lockKey:" + s);
|
||||
keys.add(s);
|
||||
return keys;
|
||||
}
|
||||
//spel解析器
|
||||
ExpressionParser parser = new SpelExpressionParser();
|
||||
//spel上下文
|
||||
EvaluationContext context = new StandardEvaluationContext();
|
||||
for (int i = 0; i < parameterNames.length; i++) {
|
||||
context.setVariable(parameterNames[i], values[i]);
|
||||
}
|
||||
Expression expression = parser.parseExpression(key);
|
||||
Object value = expression.getValue(context);
|
||||
if (value != null) {
|
||||
if (value instanceof List) {
|
||||
List value1 = (List) value;
|
||||
for (Object o : value1) {
|
||||
addKeys(keys, o, keyConstant);
|
||||
}
|
||||
} else if (value.getClass().isArray()) {
|
||||
Object[] obj = (Object[]) value;
|
||||
for (Object o : obj) {
|
||||
addKeys(keys, o, keyConstant);
|
||||
}
|
||||
} else {
|
||||
addKeys(keys, value, keyConstant);
|
||||
}
|
||||
}
|
||||
log.info("表达式key={},value={}", key, keys);
|
||||
return keys;
|
||||
}
|
||||
|
||||
private void addKeys(List<String> keys, Object o, String keyConstant) {
|
||||
keys.add("redis:lock:" + o.toString() + keyConstant);
|
||||
}
|
||||
}
|
||||
@ -1,36 +0,0 @@
|
||||
package org.jeecg.boot.starter.lock.config;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jeecg.boot.starter.lock.core.RedissonManager;
|
||||
import org.jeecg.boot.starter.lock.prop.RedissonProperties;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
|
||||
/**
|
||||
* Redisson自动化配置
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2020-11-11
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@ConditionalOnClass(RedissonProperties.class)
|
||||
@EnableConfigurationProperties(RedissonProperties.class)
|
||||
public class RedissonConfiguration {
|
||||
|
||||
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(RedissonClient.class)
|
||||
public RedissonClient redissonClient(RedissonProperties redissonProperties) {
|
||||
RedissonManager redissonManager = new RedissonManager(redissonProperties);
|
||||
log.info("RedissonManager初始化完成,当前连接方式:" + redissonProperties.getType() + ",连接地址:" + redissonProperties.getAddress());
|
||||
return redissonManager.getRedisson();
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,21 +0,0 @@
|
||||
package org.jeecg.boot.starter.lock.core.strategy;
|
||||
|
||||
import org.jeecg.boot.starter.lock.prop.RedissonProperties;
|
||||
import org.redisson.config.Config;
|
||||
|
||||
/**
|
||||
* Redisson配置构建接口
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2020-11-11
|
||||
*/
|
||||
public interface RedissonConfigStrategy {
|
||||
|
||||
/**
|
||||
* 根据不同的Redis配置策略创建对应的Config
|
||||
*
|
||||
* @param redissonProperties
|
||||
* @return Config
|
||||
*/
|
||||
Config createRedissonConfig(RedissonProperties redissonProperties);
|
||||
}
|
||||
@ -1,43 +0,0 @@
|
||||
package org.jeecg.boot.starter.lock.core.strategy.impl;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.jeecg.boot.starter.lock.core.strategy.RedissonConfigStrategy;
|
||||
import org.jeecg.boot.starter.lock.prop.RedissonProperties;
|
||||
import org.jeecg.boot.starter.lock.enums.GlobalConstant;
|
||||
import org.redisson.config.Config;
|
||||
|
||||
|
||||
/**
|
||||
* 集群方式Redisson配置
|
||||
* cluster方式至少6个节点(3主3从)
|
||||
* 配置方式:127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2020-11-11
|
||||
*/
|
||||
@Slf4j
|
||||
public class ClusterRedissonConfigStrategyImpl implements RedissonConfigStrategy {
|
||||
|
||||
@Override
|
||||
public Config createRedissonConfig(RedissonProperties redissonProperties) {
|
||||
Config config = new Config();
|
||||
try {
|
||||
String address = redissonProperties.getAddress();
|
||||
String password = redissonProperties.getPassword();
|
||||
String[] addrTokens = address.split(",");
|
||||
// 设置集群(cluster)节点的服务IP和端口
|
||||
for (int i = 0; i < addrTokens.length; i++) {
|
||||
config.useClusterServers().addNodeAddress(GlobalConstant.REDIS_CONNECTION_PREFIX + addrTokens[i]);
|
||||
if (StringUtils.isNotBlank(password)) {
|
||||
config.useClusterServers().setPassword(password);
|
||||
}
|
||||
}
|
||||
log.info("初始化集群方式Config,连接地址:" + address);
|
||||
} catch (Exception e) {
|
||||
log.error("集群Redisson初始化错误", e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
return config;
|
||||
}
|
||||
}
|
||||
@ -1,54 +0,0 @@
|
||||
package org.jeecg.boot.starter.lock.core.strategy.impl;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.jeecg.boot.starter.lock.core.strategy.RedissonConfigStrategy;
|
||||
import org.jeecg.boot.starter.lock.prop.RedissonProperties;
|
||||
import org.jeecg.boot.starter.lock.enums.GlobalConstant;
|
||||
import org.redisson.config.Config;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 主从方式Redisson配置
|
||||
* <p>配置方式: 127.0.0.1:6379(主),127.0.0.1:6380(子),127.0.0.1:6381(子)</p>
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2020-11-11
|
||||
*/
|
||||
@Slf4j
|
||||
public class MasterslaveRedissonConfigStrategyImpl implements RedissonConfigStrategy {
|
||||
|
||||
@Override
|
||||
public Config createRedissonConfig(RedissonProperties redissonProperties) {
|
||||
Config config = new Config();
|
||||
try {
|
||||
String address = redissonProperties.getAddress();
|
||||
String password = redissonProperties.getPassword();
|
||||
int database = redissonProperties.getDatabase();
|
||||
String[] addrTokens = address.split(",");
|
||||
String masterNodeAddr = addrTokens[0];
|
||||
// 设置主节点ip
|
||||
config.useMasterSlaveServers().setMasterAddress(masterNodeAddr);
|
||||
if (StringUtils.isNotBlank(password)) {
|
||||
config.useMasterSlaveServers().setPassword(password);
|
||||
}
|
||||
config.useMasterSlaveServers().setDatabase(database);
|
||||
// 设置从节点,移除第一个节点,默认第一个为主节点
|
||||
List<String> slaveList = new ArrayList<>();
|
||||
for (String addrToken : addrTokens) {
|
||||
slaveList.add(GlobalConstant.REDIS_CONNECTION_PREFIX + addrToken);
|
||||
}
|
||||
slaveList.remove(0);
|
||||
|
||||
config.useMasterSlaveServers().addSlaveAddress((String[]) slaveList.toArray());
|
||||
log.info("初始化主从方式Config,redisAddress:" + address);
|
||||
} catch (Exception e) {
|
||||
log.error("主从Redisson初始化错误", e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
return config;
|
||||
}
|
||||
}
|
||||
@ -1,47 +0,0 @@
|
||||
package org.jeecg.boot.starter.lock.core.strategy.impl;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.jeecg.boot.starter.lock.core.strategy.RedissonConfigStrategy;
|
||||
import org.jeecg.boot.starter.lock.prop.RedissonProperties;
|
||||
import org.jeecg.boot.starter.lock.enums.GlobalConstant;
|
||||
import org.redisson.config.Config;
|
||||
|
||||
|
||||
/**
|
||||
* 哨兵方式Redis连接配置
|
||||
* 比如sentinel.conf里配置为sentinel monitor my-sentinel-name 127.0.0.1 6379 2,那么这里就配置my-sentinel-name
|
||||
* 配置方式:my-sentinel-name,127.0.0.1:26379,127.0.0.1:26389,127.0.0.1:26399
|
||||
* @author zyf
|
||||
* @date 2020-11-11
|
||||
*/
|
||||
@Slf4j
|
||||
public class SentinelRedissonConfigStrategyImpl implements RedissonConfigStrategy {
|
||||
|
||||
@Override
|
||||
public Config createRedissonConfig(RedissonProperties redissonProperties) {
|
||||
Config config = new Config();
|
||||
try {
|
||||
String address = redissonProperties.getAddress();
|
||||
String password = redissonProperties.getPassword();
|
||||
int database = redissonProperties.getDatabase();
|
||||
String[] addrTokens = address.split(",");
|
||||
String sentinelAliasName = addrTokens[0];
|
||||
// 设置redis配置文件sentinel.conf配置的sentinel别名
|
||||
config.useSentinelServers().setMasterName(sentinelAliasName);
|
||||
config.useSentinelServers().setDatabase(database);
|
||||
if (StringUtils.isNotBlank(password)) {
|
||||
config.useSentinelServers().setPassword(password);
|
||||
}
|
||||
// 设置哨兵节点的服务IP和端口
|
||||
for (int i = 1; i < addrTokens.length; i++) {
|
||||
config.useSentinelServers().addSentinelAddress(GlobalConstant.REDIS_CONNECTION_PREFIX+ addrTokens[i]);
|
||||
}
|
||||
log.info("初始化哨兵方式Config,redisAddress:" + address);
|
||||
} catch (Exception e) {
|
||||
log.error("哨兵Redisson初始化错误", e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
return config;
|
||||
}
|
||||
}
|
||||
@ -1,40 +0,0 @@
|
||||
package org.jeecg.boot.starter.lock.core.strategy.impl;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.jeecg.boot.starter.lock.core.strategy.RedissonConfigStrategy;
|
||||
import org.jeecg.boot.starter.lock.prop.RedissonProperties;
|
||||
import org.jeecg.boot.starter.lock.enums.GlobalConstant;
|
||||
import org.redisson.config.Config;
|
||||
|
||||
|
||||
/**
|
||||
* 单机方式Redisson配置
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2020-11-11
|
||||
*/
|
||||
@Slf4j
|
||||
public class StandaloneRedissonConfigStrategyImpl implements RedissonConfigStrategy {
|
||||
|
||||
@Override
|
||||
public Config createRedissonConfig(RedissonProperties redissonProperties) {
|
||||
Config config = new Config();
|
||||
try {
|
||||
String address = redissonProperties.getAddress();
|
||||
String password = redissonProperties.getPassword();
|
||||
int database = redissonProperties.getDatabase();
|
||||
String redisAddr = GlobalConstant.REDIS_CONNECTION_PREFIX + address;
|
||||
config.useSingleServer().setAddress(redisAddr);
|
||||
config.useSingleServer().setDatabase(database);
|
||||
if (StringUtils.isNotBlank(password)) {
|
||||
config.useSingleServer().setPassword(password);
|
||||
}
|
||||
log.info("初始化Redisson单机配置,连接地址:" + address);
|
||||
} catch (Exception e) {
|
||||
log.error("单机Redisson初始化错误", e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
return config;
|
||||
}
|
||||
}
|
||||
@ -1,17 +0,0 @@
|
||||
package org.jeecg.boot.starter.lock.enums;
|
||||
|
||||
/**
|
||||
* 全局常量枚举
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2020-11-11
|
||||
*/
|
||||
|
||||
public interface GlobalConstant {
|
||||
|
||||
/**
|
||||
* Redis地址连接前缀
|
||||
*/
|
||||
String REDIS_CONNECTION_PREFIX = "redis://";
|
||||
|
||||
}
|
||||
@ -1,22 +0,0 @@
|
||||
package org.jeecg.boot.starter.lock.enums;
|
||||
|
||||
/**
|
||||
* 锁的模式
|
||||
* @author jeecg
|
||||
*/
|
||||
public enum LockModel {
|
||||
//可重入锁
|
||||
REENTRANT,
|
||||
//公平锁
|
||||
FAIR,
|
||||
//联锁(可以把一组锁当作一个锁来加锁和释放)
|
||||
MULTIPLE,
|
||||
//红锁
|
||||
REDLOCK,
|
||||
//读锁
|
||||
READ,
|
||||
//写锁
|
||||
WRITE,
|
||||
//自动模式,当参数只有一个.使用 REENTRANT 参数多个 REDLOCK
|
||||
AUTO
|
||||
}
|
||||
@ -1,39 +0,0 @@
|
||||
package org.jeecg.boot.starter.lock.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* Redis连接方式
|
||||
* @author zyf
|
||||
* @date 2020-11-11
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum RedisConnectionType {
|
||||
/**
|
||||
* 单机部署方式(默认)
|
||||
*/
|
||||
STANDALONE("standalone", "单机部署方式"),
|
||||
/**
|
||||
* 哨兵部署方式
|
||||
*/
|
||||
SENTINEL("sentinel", "哨兵部署方式"),
|
||||
/**
|
||||
* 集群部署方式
|
||||
*/
|
||||
CLUSTER("cluster", "集群方式"),
|
||||
/**
|
||||
* 主从部署方式
|
||||
*/
|
||||
MASTERSLAVE("masterslave", "主从部署方式");
|
||||
|
||||
/**
|
||||
* 编码
|
||||
*/
|
||||
private final String code;
|
||||
/**
|
||||
* 名称
|
||||
*/
|
||||
private final String name;
|
||||
}
|
||||
@ -1,4 +0,0 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
org.jeecg.boot.starter.lock.config.RedissonConfiguration
|
||||
|
||||
|
||||
@ -1,59 +0,0 @@
|
||||
package org.jeecg.boot.starter.lock.test;
|
||||
|
||||
import org.jeecg.boot.starter.lock.annotation.JLock;
|
||||
import org.jeecg.boot.starter.lock.annotation.JRepeat;
|
||||
import org.jeecg.boot.starter.lock.annotation.LockConstant;
|
||||
import org.jeecg.boot.starter.lock.client.RedissonLockClient;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@Service
|
||||
public class LockService {
|
||||
|
||||
@Resource
|
||||
private RedissonLockClient redissonLockClient;
|
||||
|
||||
int n = 10;
|
||||
|
||||
/**
|
||||
* 模拟秒杀(注解方式)
|
||||
*/
|
||||
@JLock(lockKey = "#productId", expireSeconds = 5000)
|
||||
public void seckill(String productId) {
|
||||
if (n <= 0) {
|
||||
System.out.println("活动已结束,请下次再来");
|
||||
return;
|
||||
}
|
||||
System.out.println(Thread.currentThread().getName() + ":秒杀到了商品");
|
||||
System.out.println(--n);
|
||||
}
|
||||
|
||||
/**
|
||||
* 模拟秒杀(编程方式)
|
||||
*/
|
||||
public void seckill2(String productId) {
|
||||
redissonLockClient.tryLock(productId, 5000);
|
||||
if (n <= 0) {
|
||||
System.out.println("活动已结束,请下次再来");
|
||||
return;
|
||||
}
|
||||
System.out.println(Thread.currentThread().getName() + ":秒杀到了商品");
|
||||
System.out.println(--n);
|
||||
redissonLockClient.unlock(productId);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 测试重复提交
|
||||
*/
|
||||
@JRepeat(lockKey = "#name", lockTime = 5)
|
||||
public void reSubmit(String name) {
|
||||
try {
|
||||
Thread.sleep(1500);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("提交成功" + name);
|
||||
}
|
||||
}
|
||||
@ -1,67 +0,0 @@
|
||||
package org.jeecg.boot.starter.lock.test;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = LockTestApplication.class)
|
||||
public class LockTest {
|
||||
@Autowired
|
||||
LockService lockService;
|
||||
|
||||
/**
|
||||
* 测试分布式锁(模拟秒杀)
|
||||
*/
|
||||
@Test
|
||||
public void test1() throws Exception {
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(6);
|
||||
IntStream.range(0, 30).forEach(i -> executorService.submit(() -> {
|
||||
try {
|
||||
lockService.seckill("20120508784");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}));
|
||||
executorService.awaitTermination(30, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试分布式锁(模拟秒杀)
|
||||
*/
|
||||
@Test
|
||||
public void test2() throws Exception {
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(6);
|
||||
IntStream.range(0, 30).forEach(i -> executorService.submit(() -> {
|
||||
try {
|
||||
lockService.seckill2("20120508784");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}));
|
||||
executorService.awaitTermination(30, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试分布式锁(模拟重复提交)
|
||||
*/
|
||||
@Test
|
||||
public void test3() throws Exception {
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(6);
|
||||
IntStream.range(0, 20).forEach(i -> executorService.submit(() -> {
|
||||
try {
|
||||
lockService.reSubmit("test");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}));
|
||||
executorService.awaitTermination(30, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
@ -1,15 +0,0 @@
|
||||
package org.jeecg.boot.starter.lock.test;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||
|
||||
@SpringBootApplication(scanBasePackages = "org.jeecg")
|
||||
@EnableAspectJAutoProxy
|
||||
public class LockTestApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(LockTestApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,9 +0,0 @@
|
||||
package org.jeecg.boot.starter.lock.test;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class TestUser {
|
||||
private String userId;
|
||||
private String userName;
|
||||
}
|
||||
@ -1,19 +0,0 @@
|
||||
spring:
|
||||
redis:
|
||||
database: 0
|
||||
host: 127.0.0.1
|
||||
lettuce:
|
||||
pool:
|
||||
max-active: 8 #最大连接数据库连接数,设 0 为没有限制
|
||||
max-idle: 8 #最大等待连接中的数量,设 0 为没有限制
|
||||
max-wait: -1ms #最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
|
||||
min-idle: 0 #最小等待连接中的数量,设 0 为没有限制
|
||||
shutdown-timeout: 100ms
|
||||
password: jeecg
|
||||
port: 6379
|
||||
jeecg :
|
||||
redisson:
|
||||
address: 127.0.0.1:6379
|
||||
password: jeecg
|
||||
type: STANDALONE
|
||||
enabled: true
|
||||
@ -1,21 +0,0 @@
|
||||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>jeecg-boot-starter</artifactId>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<version>3.3.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>jeecg-boot-starter-rabbitmq</artifactId>
|
||||
<description>jeecg-boot-starter-消息队列</description>
|
||||
<dependencies>
|
||||
<!-- 消息总线 rabbitmq -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@ -1,359 +0,0 @@
|
||||
package org.jeecg.boot.starter.rabbitmq.client;
|
||||
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jeecg.boot.starter.rabbitmq.event.EventObj;
|
||||
import org.jeecg.boot.starter.rabbitmq.event.JeecgRemoteApplicationEvent;
|
||||
import org.jeecg.boot.starter.rabbitmq.exchange.DelayExchangeBuilder;
|
||||
import org.jeecg.common.annotation.RabbitComponent;
|
||||
import org.jeecg.common.base.BaseMap;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.amqp.core.*;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.amqp.rabbit.core.RabbitAdmin;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.bus.BusProperties;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.lang.reflect.Method;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* 消息队列客户端
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
public class RabbitMqClient {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(RabbitMqClient.class);
|
||||
|
||||
private final RabbitAdmin rabbitAdmin;
|
||||
|
||||
private final RabbitTemplate rabbitTemplate;
|
||||
|
||||
|
||||
@Resource
|
||||
private SimpleMessageListenerContainer messageListenerContainer;
|
||||
|
||||
@Resource
|
||||
BusProperties busProperties;
|
||||
@Resource
|
||||
private ApplicationEventPublisher publisher;
|
||||
|
||||
|
||||
@Resource
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
|
||||
@Bean
|
||||
public void initQueue() {
|
||||
Map<String, Object> beansWithRqbbitComponentMap = this.applicationContext.getBeansWithAnnotation(RabbitComponent.class);
|
||||
Class<? extends Object> clazz = null;
|
||||
for (Map.Entry<String, Object> entry : beansWithRqbbitComponentMap.entrySet()) {
|
||||
log.info("初始化队列............");
|
||||
//获取到实例对象的class信息
|
||||
clazz = entry.getValue().getClass();
|
||||
Method[] methods = clazz.getMethods();
|
||||
RabbitListener rabbitListener = clazz.getAnnotation(RabbitListener.class);
|
||||
if (ObjectUtil.isNotEmpty(rabbitListener)) {
|
||||
createQueue(rabbitListener);
|
||||
}
|
||||
for (Method method : methods) {
|
||||
RabbitListener methodRabbitListener = method.getAnnotation(RabbitListener.class);
|
||||
if (ObjectUtil.isNotEmpty(methodRabbitListener)) {
|
||||
createQueue(methodRabbitListener);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化队列
|
||||
*
|
||||
* @param rabbitListener
|
||||
*/
|
||||
private void createQueue(RabbitListener rabbitListener) {
|
||||
String[] queues = rabbitListener.queues();
|
||||
DirectExchange directExchange = createExchange(DelayExchangeBuilder.DELAY_EXCHANGE);
|
||||
//创建交换机
|
||||
rabbitAdmin.declareExchange(directExchange);
|
||||
if (ObjectUtil.isNotEmpty(queues)) {
|
||||
for (String queueName : queues) {
|
||||
Properties result = rabbitAdmin.getQueueProperties(queueName);
|
||||
if (ObjectUtil.isEmpty(result)) {
|
||||
Queue queue = new Queue(queueName);
|
||||
addQueue(queue);
|
||||
Binding binding = BindingBuilder.bind(queue).to(directExchange).with(queueName);
|
||||
rabbitAdmin.declareBinding(binding);
|
||||
log.info("创建队列:" + queueName);
|
||||
}else{
|
||||
log.info("已有队列:" + queueName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private Map sentObj = new HashMap<>();
|
||||
|
||||
|
||||
@Autowired
|
||||
public RabbitMqClient(RabbitAdmin rabbitAdmin, RabbitTemplate rabbitTemplate) {
|
||||
this.rabbitAdmin = rabbitAdmin;
|
||||
this.rabbitTemplate = rabbitTemplate;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发送远程事件
|
||||
*
|
||||
* @param handlerName
|
||||
* @param baseMap
|
||||
*/
|
||||
public void publishEvent(String handlerName, BaseMap baseMap) {
|
||||
EventObj eventObj = new EventObj();
|
||||
eventObj.setHandlerName(handlerName);
|
||||
eventObj.setBaseMap(baseMap);
|
||||
publisher.publishEvent(new JeecgRemoteApplicationEvent(eventObj, busProperties.getId()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换Message对象
|
||||
*
|
||||
* @param messageType 返回消息类型 MessageProperties类中常量
|
||||
* @param msg
|
||||
* @return
|
||||
*/
|
||||
public Message getMessage(String messageType, Object msg) {
|
||||
MessageProperties messageProperties = new MessageProperties();
|
||||
messageProperties.setContentType(messageType);
|
||||
Message message = new Message(msg.toString().getBytes(), messageProperties);
|
||||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* 有绑定Key的Exchange发送
|
||||
*
|
||||
* @param routingKey
|
||||
* @param msg
|
||||
*/
|
||||
public void sendMessageToExchange(TopicExchange topicExchange, String routingKey, Object msg) {
|
||||
Message message = getMessage(MessageProperties.CONTENT_TYPE_JSON, msg);
|
||||
rabbitTemplate.send(topicExchange.getName(), routingKey, message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 没有绑定KEY的Exchange发送
|
||||
*
|
||||
* @param exchange
|
||||
* @param msg
|
||||
*/
|
||||
public void sendMessageToExchange(TopicExchange topicExchange, AbstractExchange exchange, String msg) {
|
||||
addExchange(exchange);
|
||||
logger.info("RabbitMQ send " + exchange.getName() + "->" + msg);
|
||||
rabbitTemplate.convertAndSend(topicExchange.getName(), msg);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发送消息
|
||||
*
|
||||
* @param queueName 队列名称
|
||||
* @param params 消息内容map
|
||||
*/
|
||||
public void sendMessage(String queueName, Object params) {
|
||||
log.info("发送消息到mq");
|
||||
try {
|
||||
rabbitTemplate.convertAndSend(DelayExchangeBuilder.DELAY_EXCHANGE, queueName, params, message -> {
|
||||
return message;
|
||||
});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送消息
|
||||
*
|
||||
* @param queueName 队列名称
|
||||
*/
|
||||
public void sendMessage(String queueName) {
|
||||
this.send(queueName, this.sentObj, 0);
|
||||
this.sentObj.clear();
|
||||
}
|
||||
|
||||
|
||||
public RabbitMqClient put(String key, Object value) {
|
||||
this.sentObj.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 延迟发送消息
|
||||
*
|
||||
* @param queueName 队列名称
|
||||
* @param params 消息内容params
|
||||
* @param expiration 延迟时间 单位毫秒
|
||||
*/
|
||||
public void sendMessage(String queueName, Object params, Integer expiration) {
|
||||
this.send(queueName, params, expiration);
|
||||
}
|
||||
|
||||
private void send(String queueName, Object params, Integer expiration) {
|
||||
Queue queue = new Queue(queueName);
|
||||
addQueue(queue);
|
||||
CustomExchange customExchange = DelayExchangeBuilder.buildExchange();
|
||||
rabbitAdmin.declareExchange(customExchange);
|
||||
Binding binding = BindingBuilder.bind(queue).to(customExchange).with(queueName).noargs();
|
||||
rabbitAdmin.declareBinding(binding);
|
||||
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
log.debug("发送时间:" + sf.format(new Date()));
|
||||
rabbitTemplate.convertAndSend(DelayExchangeBuilder.DEFAULT_DELAY_EXCHANGE, queueName, params, message -> {
|
||||
if (expiration != null && expiration > 0) {
|
||||
message.getMessageProperties().setHeader("x-delay", expiration);
|
||||
}
|
||||
return message;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 给queue发送消息
|
||||
*
|
||||
* @param queueName
|
||||
*/
|
||||
public String receiveFromQueue(String queueName) {
|
||||
return receiveFromQueue(DirectExchange.DEFAULT, queueName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 给direct交换机指定queue发送消息
|
||||
*
|
||||
* @param directExchange
|
||||
* @param queueName
|
||||
*/
|
||||
public String receiveFromQueue(DirectExchange directExchange, String queueName) {
|
||||
Queue queue = new Queue(queueName);
|
||||
addQueue(queue);
|
||||
Binding binding = BindingBuilder.bind(queue).to(directExchange).withQueueName();
|
||||
rabbitAdmin.declareBinding(binding);
|
||||
String messages = (String) rabbitTemplate.receiveAndConvert(queueName);
|
||||
System.out.println("Receive:" + messages);
|
||||
return messages;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建Exchange
|
||||
*
|
||||
* @param exchange
|
||||
*/
|
||||
public void addExchange(AbstractExchange exchange) {
|
||||
rabbitAdmin.declareExchange(exchange);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除一个Exchange
|
||||
*
|
||||
* @param exchangeName
|
||||
*/
|
||||
public boolean deleteExchange(String exchangeName) {
|
||||
return rabbitAdmin.deleteExchange(exchangeName);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 声明其名称自动命名的队列。它是用exclusive=true、autoDelete=true和 durable = false
|
||||
*
|
||||
* @return Queue
|
||||
*/
|
||||
public Queue addQueue() {
|
||||
return rabbitAdmin.declareQueue();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个指定的Queue
|
||||
*
|
||||
* @param queue
|
||||
* @return queueName
|
||||
*/
|
||||
public String addQueue(Queue queue) {
|
||||
return rabbitAdmin.declareQueue(queue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除一个队列
|
||||
*
|
||||
* @param queueName the name of the queue.
|
||||
* @param unused true if the queue should be deleted only if not in use.
|
||||
* @param empty true if the queue should be deleted only if empty.
|
||||
*/
|
||||
public void deleteQueue(String queueName, boolean unused, boolean empty) {
|
||||
rabbitAdmin.deleteQueue(queueName, unused, empty);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除一个队列
|
||||
*
|
||||
* @param queueName
|
||||
* @return true if the queue existed and was deleted.
|
||||
*/
|
||||
public boolean deleteQueue(String queueName) {
|
||||
return rabbitAdmin.deleteQueue(queueName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定一个队列到一个匹配型交换器使用一个routingKey
|
||||
*
|
||||
* @param queue
|
||||
* @param exchange
|
||||
* @param routingKey
|
||||
*/
|
||||
public void addBinding(Queue queue, TopicExchange exchange, String routingKey) {
|
||||
Binding binding = BindingBuilder.bind(queue).to(exchange).with(routingKey);
|
||||
rabbitAdmin.declareBinding(binding);
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定一个Exchange到一个匹配型Exchange 使用一个routingKey
|
||||
*
|
||||
* @param exchange
|
||||
* @param topicExchange
|
||||
* @param routingKey
|
||||
*/
|
||||
public void addBinding(Exchange exchange, TopicExchange topicExchange, String routingKey) {
|
||||
Binding binding = BindingBuilder.bind(exchange).to(topicExchange).with(routingKey);
|
||||
rabbitAdmin.declareBinding(binding);
|
||||
}
|
||||
|
||||
/**
|
||||
* 去掉一个binding
|
||||
*
|
||||
* @param binding
|
||||
*/
|
||||
public void removeBinding(Binding binding) {
|
||||
rabbitAdmin.removeBinding(binding);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建交换器
|
||||
*
|
||||
* @param exchangeName
|
||||
* @return
|
||||
*/
|
||||
public DirectExchange createExchange(String exchangeName) {
|
||||
return new DirectExchange(exchangeName, true, false);
|
||||
}
|
||||
}
|
||||
@ -1,67 +0,0 @@
|
||||
package org.jeecg.boot.starter.rabbitmq.config;
|
||||
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.jeecg.boot.starter.rabbitmq.event.JeecgRemoteApplicationEvent;
|
||||
import org.jeecg.common.config.mqtoken.TransmitUserTokenFilter;
|
||||
import org.springframework.amqp.core.AcknowledgeMode;
|
||||
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
|
||||
import org.springframework.amqp.rabbit.core.RabbitAdmin;
|
||||
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
|
||||
import org.springframework.amqp.support.ConsumerTagStrategy;
|
||||
import org.springframework.cloud.bus.jackson.RemoteApplicationEventScan;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 消息队列配置类
|
||||
*
|
||||
* @author zyf
|
||||
*/
|
||||
@Configuration
|
||||
@RemoteApplicationEventScan(basePackageClasses = JeecgRemoteApplicationEvent.class)
|
||||
public class RabbitMqConfig {
|
||||
|
||||
|
||||
@Bean
|
||||
public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
|
||||
RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
|
||||
//设置忽略声明异常
|
||||
rabbitAdmin.setIgnoreDeclarationExceptions(true);
|
||||
return rabbitAdmin;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入获取token过滤器
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public TransmitUserTokenFilter transmitUserInfoFromHttpHeader(){
|
||||
return new TransmitUserTokenFilter();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SimpleMessageListenerContainer messageListenerContainer(ConnectionFactory connectionFactory) {
|
||||
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
|
||||
container.setConnectionFactory(connectionFactory);
|
||||
//手动确认
|
||||
container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
|
||||
//当前的消费者数量
|
||||
container.setConcurrentConsumers(1);
|
||||
//最大的消费者数量
|
||||
container.setMaxConcurrentConsumers(1);
|
||||
//是否重回队列
|
||||
container.setDefaultRequeueRejected(true);
|
||||
|
||||
//消费端的标签策略
|
||||
container.setConsumerTagStrategy(new ConsumerTagStrategy() {
|
||||
@Override
|
||||
public String createConsumerTag(String queue) {
|
||||
return queue + "_" + UUID.randomUUID().toString();
|
||||
}
|
||||
});
|
||||
return container;
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,39 +0,0 @@
|
||||
package org.jeecg.boot.starter.rabbitmq.core;
|
||||
|
||||
import org.springframework.amqp.core.Message;
|
||||
import org.springframework.amqp.core.MessageProperties;
|
||||
import org.springframework.amqp.support.converter.MessageConversionException;
|
||||
import org.springframework.amqp.support.converter.MessageConverter;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class MapMessageConverter implements MessageConverter {
|
||||
@Override
|
||||
public Message toMessage(Object object, MessageProperties messageProperties) throws MessageConversionException {
|
||||
return new Message(object.toString().getBytes(), messageProperties);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object fromMessage(Message message) throws MessageConversionException {
|
||||
String contentType = message.getMessageProperties().getContentType();
|
||||
if (null != contentType && contentType.contains("text")) {
|
||||
return new String(message.getBody());
|
||||
} else {
|
||||
ObjectInputStream objInt = null;
|
||||
try {
|
||||
ByteArrayInputStream byteInt = new ByteArrayInputStream(message.getBody());
|
||||
objInt = new ObjectInputStream(byteInt);
|
||||
//byte[]转map
|
||||
Map map = (HashMap) objInt.readObject();
|
||||
return map;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
}
|
||||
@ -1,28 +0,0 @@
|
||||
package org.jeecg.boot.starter.rabbitmq.event;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
|
||||
import org.jeecg.common.util.SpringContextHolder;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 监听远程事件,并分发消息到业务模块消息处理器
|
||||
*/
|
||||
@Component
|
||||
public class BaseApplicationEvent implements ApplicationListener<JeecgRemoteApplicationEvent> {
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(JeecgRemoteApplicationEvent jeecgRemoteApplicationEvent) {
|
||||
EventObj eventObj = jeecgRemoteApplicationEvent.getEventObj();
|
||||
if (ObjectUtil.isNotEmpty(eventObj)) {
|
||||
//获取业务模块消息处理器
|
||||
JeecgBusEventHandler busEventHandler = SpringContextHolder.getHandler(eventObj.getHandlerName(), JeecgBusEventHandler.class);
|
||||
if (ObjectUtil.isNotEmpty(busEventHandler)) {
|
||||
//通知业务模块
|
||||
busEventHandler.onMessage(eventObj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,21 +0,0 @@
|
||||
package org.jeecg.boot.starter.rabbitmq.event;
|
||||
|
||||
import lombok.Data;
|
||||
import org.jeecg.common.base.BaseMap;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 远程事件数据对象
|
||||
*/
|
||||
@Data
|
||||
public class EventObj implements Serializable {
|
||||
/**
|
||||
* 数据对象
|
||||
*/
|
||||
private BaseMap baseMap;
|
||||
/**
|
||||
* 自定义业务模块消息处理器beanName
|
||||
*/
|
||||
private String handlerName;
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue