新增redis和redisson
This commit is contained in:
@@ -78,6 +78,16 @@
|
||||
<artifactId>knife4j-spring-boot-starter</artifactId>
|
||||
<version>2.0.9</version>
|
||||
</dependency>
|
||||
<!-- redis -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
<!-- redisson -->
|
||||
<dependency>
|
||||
<groupId>org.redisson</groupId>
|
||||
<artifactId>redisson-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -51,5 +51,19 @@
|
||||
<artifactId>java-jwt</artifactId>
|
||||
<version>3.19.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 测试所需依赖 -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-test</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.hula.common.config;
|
||||
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import org.redisson.Redisson;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.redisson.config.Config;
|
||||
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author nyh
|
||||
*/
|
||||
@Configuration
|
||||
public class RedissonConfig {
|
||||
@Resource
|
||||
private RedisProperties redisProperties;
|
||||
|
||||
@Bean
|
||||
public RedissonClient redissonClient() {
|
||||
Config config = new Config();
|
||||
config.useSingleServer()
|
||||
.setAddress("redis://" + redisProperties.getHost() + ":" + redisProperties.getPort())
|
||||
// .setPassword(redisProperties.getPassword())
|
||||
.setPassword(null)
|
||||
.setDatabase(redisProperties.getDatabase());
|
||||
return Redisson.create(config);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.hula.common.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.scheduling.annotation.AsyncConfigurer;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
|
||||
/**
|
||||
* 自定义线程池配置
|
||||
* @author nyh
|
||||
*/
|
||||
@Configuration
|
||||
@EnableAsync
|
||||
public class ThreadPoolConfig implements AsyncConfigurer {
|
||||
/**
|
||||
* 项目共用线程池
|
||||
*/
|
||||
public static final String HULA_EXECUTOR = "hulaExecutor";
|
||||
/**
|
||||
* websocket通信线程池
|
||||
*/
|
||||
public static final String WS_EXECUTOR = "websocketExecutor";
|
||||
|
||||
@Override
|
||||
public Executor getAsyncExecutor() {
|
||||
return hulaExecutor();
|
||||
}
|
||||
|
||||
@Bean(HULA_EXECUTOR)
|
||||
@Primary
|
||||
public ThreadPoolTaskExecutor hulaExecutor() {
|
||||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
||||
executor.setCorePoolSize(10);
|
||||
executor.setMaxPoolSize(10);
|
||||
executor.setQueueCapacity(200);
|
||||
executor.setThreadNamePrefix("hula-executor-");
|
||||
//满了调用线程执行,认为重要任务
|
||||
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
|
||||
executor.initialize();
|
||||
return executor;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.hula.common.constant;
|
||||
|
||||
/**
|
||||
* 管理项目通用key
|
||||
* @author nyh
|
||||
*/
|
||||
public class RedisKey {
|
||||
private static final String BASE_KEY = "hula:chat";
|
||||
/** 用户token的key */
|
||||
public static final String USER_TOKEN_STRING = "userToken:uid_%d";
|
||||
/** 获取key */
|
||||
public static String getKey(String key, Object... o) {
|
||||
return BASE_KEY + String.format(key, o);
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,24 @@
|
||||
package com.hula.common.user.service.impl;
|
||||
|
||||
import com.hula.common.constant.RedisKey;
|
||||
import com.hula.common.user.service.LoginService;
|
||||
import com.hula.common.utils.JwtUtils;
|
||||
import com.hula.common.utils.RedisUtils;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author nyh
|
||||
*/
|
||||
@Service
|
||||
public class LoginServiceImpl implements LoginService {
|
||||
|
||||
public static final int TOKEN_EXPIRE_DAYS = 3;
|
||||
public static final int TOKEN_RENEWAL_DAYS = 1;
|
||||
@Resource
|
||||
private JwtUtils jwtUtils;
|
||||
|
||||
@@ -20,18 +28,37 @@ public class LoginServiceImpl implements LoginService {
|
||||
}
|
||||
|
||||
@Override
|
||||
@Async
|
||||
public void renewalTokenIfNecessary(String token) {
|
||||
|
||||
Long uid = getValidUid(token);
|
||||
String userTokenKey = getUserTokenKey(uid);
|
||||
Long expireDays = RedisUtils.getExpire(userTokenKey, TimeUnit.DAYS);
|
||||
if (expireDays == -2) {
|
||||
return;
|
||||
}
|
||||
if (expireDays < TOKEN_RENEWAL_DAYS) {
|
||||
RedisUtils.expire(RedisKey.getKey(getUserTokenKey(uid)), TOKEN_EXPIRE_DAYS, TimeUnit.DAYS);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String login(Long uid) {
|
||||
String token = jwtUtils.createToken(uid);
|
||||
return null;
|
||||
RedisUtils.set(RedisKey.getKey(getUserTokenKey(uid)), token, TOKEN_EXPIRE_DAYS, TimeUnit.DAYS);
|
||||
return token;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getValidUid(String token) {
|
||||
return null;
|
||||
Long uid = jwtUtils.getUidOrNull(token);
|
||||
if (Objects.isNull(uid)) {
|
||||
return null;
|
||||
}
|
||||
String oldToken = RedisUtils.get(getUserTokenKey(uid));
|
||||
return Objects.equals(oldToken, token) ? uid : null;
|
||||
}
|
||||
|
||||
private String getUserTokenKey(Long uid) {
|
||||
return RedisKey.getKey(RedisKey.USER_TOKEN_STRING, uid);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package com.hula.common.user.service.impl;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.hula.common.user.service.WXMsgService;
|
||||
import com.hula.common.user.service.adapter.UserAdapter;
|
||||
import com.hula.common.user.dao.UserDao;
|
||||
import com.hula.common.user.domain.entity.User;
|
||||
import com.hula.common.user.service.UserService;
|
||||
import com.hula.common.user.service.WXMsgService;
|
||||
import com.hula.common.user.service.adapter.TextBuilder;
|
||||
import com.hula.common.user.service.adapter.UserAdapter;
|
||||
import com.hula.common.websocket.domain.service.WebSocketService;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.hula.common.utils;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* @author nyh
|
||||
*/
|
||||
public class JsonUtils {
|
||||
private static final ObjectMapper jsonMapper = new ObjectMapper();
|
||||
|
||||
public static <T> T toObj(String str, Class<T> clz) {
|
||||
try {
|
||||
return jsonMapper.readValue(str, clz);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new UnsupportedOperationException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T toObj(String str, TypeReference<T> clz) {
|
||||
try {
|
||||
return jsonMapper.readValue(str, clz);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new UnsupportedOperationException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> List<T> toList(String str, Class<T> clz) {
|
||||
try {
|
||||
return jsonMapper.readValue(str, new TypeReference<List<T>>() {
|
||||
});
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new UnsupportedOperationException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static JsonNode toJsonNode(String str) {
|
||||
try {
|
||||
return jsonMapper.readTree(str);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new UnsupportedOperationException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T nodeToValue(JsonNode node, Class<T> clz) {
|
||||
try {
|
||||
return jsonMapper.treeToValue(node, clz);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new UnsupportedOperationException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String toStr(Object t) {
|
||||
try {
|
||||
return jsonMapper.writeValueAsString(t);
|
||||
} catch (Exception e) {
|
||||
throw new UnsupportedOperationException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
1128
hula-im-service/src/main/java/com/hula/common/utils/RedisUtils.java
Normal file
1128
hula-im-service/src/main/java/com/hula/common/utils/RedisUtils.java
Normal file
File diff suppressed because it is too large
Load Diff
@@ -8,10 +8,9 @@ import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
|
||||
/**
|
||||
* Description: 群成员列表的成员信息
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-03-23
|
||||
* @author nyh
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@@ -20,9 +19,6 @@ import java.util.Date;
|
||||
public class ChatMemberResp {
|
||||
@ApiModelProperty("uid")
|
||||
private Long uid;
|
||||
/**
|
||||
* @see com.abin.mallchat.common.user.domain.enums.ChatActiveStatusEnum
|
||||
*/
|
||||
@ApiModelProperty("在线状态 1在线 2离线")
|
||||
private Integer activeStatus;
|
||||
|
||||
|
||||
@@ -26,9 +26,6 @@ public class WSMemberChange {
|
||||
private Long uid;
|
||||
@ApiModelProperty("变动类型 1加入群组 2移除群组")
|
||||
private Integer changeType;
|
||||
/**
|
||||
* @see com.abin.mallchat.common.user.domain.enums.ChatActiveStatusEnum
|
||||
*/
|
||||
@ApiModelProperty("在线状态 1在线 2离线")
|
||||
private Integer activeStatus;
|
||||
@ApiModelProperty("最后一次上下线时间")
|
||||
|
||||
@@ -8,10 +8,9 @@ import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* Description:
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-03-19
|
||||
* @author nyh
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@@ -26,16 +25,10 @@ public class WSMsgMark {
|
||||
private Long uid;
|
||||
@ApiModelProperty("消息id")
|
||||
private Long msgId;
|
||||
/**
|
||||
* @see com.abin.mallchat.common.chat.domain.enums.MessageMarkTypeEnum
|
||||
*/
|
||||
@ApiModelProperty("标记类型 1点赞 2举报")
|
||||
private Integer markType;
|
||||
@ApiModelProperty("被标记的数量")
|
||||
private Integer markCount;
|
||||
/**
|
||||
* @see com.abin.mallchat.common.chat.domain.enums.MessageMarkActTypeEnum
|
||||
*/
|
||||
@ApiModelProperty("动作类型 1确认 2取消")
|
||||
private Integer actType;
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ HuLa-IM:
|
||||
LOGGING_LEVEL: info # 日志打印类型
|
||||
REDIS:
|
||||
PAW: null #redis密码
|
||||
PORT: 6379 # redis端口号
|
||||
WX: # 微信配置
|
||||
CALLBACK: https://468ov9ww8878.vicp.fun
|
||||
APPID: wxb28a0fd6d1cae6ba
|
||||
|
||||
@@ -34,24 +34,16 @@ spring:
|
||||
# Redis服务器地址
|
||||
host: ${HuLa-IM.HOST}
|
||||
# Redis服务器连接端口
|
||||
port: 6379
|
||||
port: ${HuLa-IM.REDIS.PORT}
|
||||
# Redis服务器连接密码(默认为空)
|
||||
password: ${HuLa-IM.REDIS.PAW}
|
||||
# 连接超时时间
|
||||
timeout: 10s
|
||||
lettuce:
|
||||
pool:
|
||||
# 连接池最大连接数
|
||||
max-active: 8
|
||||
# 连接池最大阻塞等待时间(使用负值表示没有限制)
|
||||
max-wait: -1ms
|
||||
# 连接池中的最大空闲连接
|
||||
max-idle: 8
|
||||
# 连接池中的最小空闲连接
|
||||
min-idle: 0
|
||||
timeout: 1800000
|
||||
|
||||
# jackson配置
|
||||
jackson:
|
||||
serialization:
|
||||
write-dates-as-timestamps: true # 序列化
|
||||
date-format: yyyy-MM-dd HH:mm:ss # 日期格式化
|
||||
|
||||
|
||||
|
||||
12
hula-im-service/src/main/resources/banner.txt
Normal file
12
hula-im-service/src/main/resources/banner.txt
Normal file
@@ -0,0 +1,12 @@
|
||||
${AnsiColor.CYAN} ██╗ ██╗██╗ ██╗${AnsiColor.BLUE} ██╗ █████╗
|
||||
${AnsiColor.CYAN} ██║ ██║██║ ██║${AnsiColor.BLUE} ██║ ██╔══██╗
|
||||
${AnsiColor.CYAN} ███████║██║ ██║${AnsiColor.BLUE} ██║ ███████║
|
||||
${AnsiColor.CYAN} ██╔══██║██║ ██║${AnsiColor.BLUE} ██║ ██╔══██║
|
||||
${AnsiColor.CYAN} ██║ ██║╚██████╔╝${AnsiColor.BLUE} ███████╗██║ ██║
|
||||
${AnsiColor.CYAN} ╚═╝ ╚═╝ ╚═════╝ ${AnsiColor.BLUE} ╚══════╝╚═╝ ╚═╝
|
||||
|
||||
${AnsiColor.BLUE}::HuLa 版本:: ${AnsiColor.MAGENTA}(${version})
|
||||
${AnsiColor.CYAN}::Spring Boot 版本:: ${AnsiColor.MAGENTA}(${spring-boot.version})
|
||||
${AnsiColor.GREEN}::Server Port 端口:: ${AnsiColor.YELLOW}(${AnsiColor.YELLOW}${server.port}${AnsiColor.YELLOW})
|
||||
${AnsiColor.YELLOW}::JDK 版本:: ${AnsiColor.MAGENTA}(${java.version})
|
||||
${AnsiColor.WHITE}
|
||||
38
hula-im-service/src/test/java/com/hula/common/DaoTest.java
Normal file
38
hula-im-service/src/test/java/com/hula/common/DaoTest.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package com.hula.common;
|
||||
|
||||
import com.hula.HuLaImCommonStarterApplication;
|
||||
import com.hula.common.user.service.LoginService;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.redisson.api.RLock;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
|
||||
@SpringBootTest(classes = HuLaImCommonStarterApplication.class)
|
||||
@RunWith(SpringRunner.class)
|
||||
@Slf4j
|
||||
public class DaoTest {
|
||||
@Resource
|
||||
private RedissonClient redissonClient;
|
||||
@Resource
|
||||
private LoginService loginService;
|
||||
|
||||
@Test
|
||||
public void redis() {
|
||||
RLock lock = redissonClient.getLock("123");
|
||||
lock.lock();
|
||||
System.out.println("触发");
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getUserToken() {
|
||||
String s = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOjExMDAwLCJjcmVhdGVUaW1lIjoxNzE0NDkwNjc5fQ.Gu9iLk37OlKMVHsDnzvu6C9-tzt_5WRo9A11DkH9QlY";
|
||||
Long validUid = loginService.getValidUid(s);
|
||||
System.out.println(validUid);
|
||||
}
|
||||
}
|
||||
21
pom.xml
21
pom.xml
@@ -13,16 +13,10 @@
|
||||
<module>hula-im-framework</module>
|
||||
</modules>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.2.1</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<revision>v1.0.0-alpha</revision>
|
||||
<java.version>21</java.version>
|
||||
<spring-boot.version>3.2.1</spring-boot.version>
|
||||
<hutool.version>5.8.24</hutool.version>
|
||||
<springfox-swagger.version>3.0.0</springfox-swagger.version>
|
||||
<swagger-models.version>1.6.0</swagger-models.version>
|
||||
@@ -39,7 +33,7 @@
|
||||
<mybatis-plus-generator.version>3.4.1</mybatis-plus-generator.version>
|
||||
<jsoup.version>1.15.3</jsoup.version>
|
||||
<okhttp.version>4.8.1</okhttp.version>
|
||||
<redisson-spring-boot-starter.version>3.17.1</redisson-spring-boot-starter.version>
|
||||
<redisson-spring-boot-starter.version>3.24.1</redisson-spring-boot-starter.version>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
@@ -50,7 +44,15 @@
|
||||
<artifactId>hula-im-common-starter</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringBoot -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-dependencies</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<!-- okhttp3 -->
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
@@ -160,6 +162,7 @@
|
||||
<configuration>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
<!--suppress UnresolvedMavenProperty -->
|
||||
<encoding>${project.build.sourceEncoding}</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
Reference in New Issue
Block a user