diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..2ebb2942b7ba22583553dd2f38b8975d79fb79e3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,5 @@ +FROM openjdk:21 +EXPOSE 9190 +ADD hula-im-service-v1.5.0.jar app.jar +RUN sh -c 'touch /app.jar' +ENTRYPOINT ["java","-jar","-Djava.security.egd=file:/dev/./urandom","/app.jar","--spring.profiles.active=prod"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000000000000000000000000000000000000..cb9604185f1b95fafd2b458b98d7f1625fca3bf9 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,108 @@ +version: '3.8' + +services: + mysql: + image: mysql:8.0 + ports: + - "3306:3306" + volumes: + - /home/mysql/data:/var/lib/mysql + environment: #指定root密码 + - MYSQL_ROOT_PASSWORD=123456 + + redis: + image: redis:latest + ports: + - "6379:6379" + volumes: + - /home/redis/myredis/data:/data + - /home/redis/myredis/myredis.conf:/etc/redis/redis.conf + + + rocketmq-namesrv: + image: foxiswho/rocketmq:4.8.0 + container_name: rocketmq-namesrv + restart: always + ports: + - 9876:9876 + volumes: + - ./namesrv/logs:/home/rocketmq/logs + - ./namesrv/store:/home/rocketmq/store + environment: + JAVA_OPT_EXT: "-Duser.home=/home/rocketmq -Xms128M -Xmx128M -Xmn128m" + command: [ "sh","mqnamesrv" ] + networks: + rocketmq_net: + aliases: + - rocketmq-namesrv + + + rocketmq-broker: + image: foxiswho/rocketmq:4.8.0 + container_name: rocketmq-broker + restart: always + ports: + - 10909:10909 + - 10911:10911 + volumes: + - ./broker/logs:/home/rocketmq/logs + - ./broker/store:/home/rocketmq/store + - ./broker/conf/plain_acl.yml:/home/rocketmq/rocketmq-4.8.0/conf/plain_acl.yml + - ./broker/conf/broker.conf:/etc/rocketmq/broker.conf + environment: + JAVA_OPT_EXT: "-Duser.home=/home/rocketmq -Xms128m -Xmx128m -Xmn128m" + command: [ "sh","mqbroker","-c","/etc/rocketmq/broker.conf" ] + depends_on: + - rocketmq-namesrv + networks: + rocketmq_net: + aliases: + - rocketmq-broker + + + rocketmq-console: + image: iamverygood/rocketmq-console:4.7.1 + container_name: rocketmq-console + restart: always + ports: + - 8180:8080 + volumes: + - ./console/data:/tmp/rocketmq-console/data + environment: + JAVA_OPTS: "-Drocketmq.namesrv.addr=rocketmq-namesrv:9876 -Dcom.rocketmq.sendMessageWithVIPChannel=false -Drocketmq.config.loginRequired=true -Drocketmq.config.aclEnabled=true -Drocketmq.config.accessKey=mallchat -Drocketmq.config.secretKey=12345678" + depends_on: + - rocketmq-namesrv + networks: + rocketmq_net: + aliases: + - rocketmq-console + + minio: + image: "quay.io/minio/minio:RELEASE.2022-08-02T23-59-16Z" + container_name: minio + ports: + - "9000:9000" # api 端口 + - "9001:9001" # 控制台端口 + environment: + MINIO_ROOT_USER: admin # 管理后台用户名 + MINIO_ROOT_PASSWORD: 12345678 # 管理后台密码,最小8个字符 + MINIO_COMPRESS: "off" # 开启压缩 on 开启 off 关闭 + MINIO_COMPRESS_EXTENSIONS: "" # 扩展名 .pdf,.doc 为空 所有类型均压缩 + MINIO_COMPRESS_MIME_TYPES: "" # mime 类型 application/pdf 为空 所有类型均压缩 + volumes: + - /data/minio/data:/data/ # 映射当前目录下的data目录至容器内/data目录 + - /data/minio/config:/root/.minio/ # 映射配置目录 + command: server --address ':9000' --console-address ':9001' /data # 指定容器中的目录 /data + privileged: true + + hula-im-server: + image: hula-im-server:v1.5.0 + ports: + - "9190:9190" + depends_on: + - mysql + - redis + - rocketmq-namesrv + - rocketmq-broker + - rocketmq-console + - minio \ No newline at end of file diff --git a/hula-im-service/src/main/java/com/hula/core/chat/domain/entity/GroupMember.java b/hula-im-service/src/main/java/com/hula/core/chat/domain/entity/GroupMember.java index 078b0a44f35273185fb5240247765fa42aecbc12..d2ab71b8a4f00f2fe21eda1c6f0ae6ed4a5d8066 100644 --- a/hula-im-service/src/main/java/com/hula/core/chat/domain/entity/GroupMember.java +++ b/hula-im-service/src/main/java/com/hula/core/chat/domain/entity/GroupMember.java @@ -46,8 +46,6 @@ public class GroupMember implements Serializable { /** * 成员角色1群主(可撤回,可移除,可解散) 2管理员(可撤回,可移除) 3普通成员 - * - * @see com.abin.mallchat.common.chat.domain.enums.GroupRoleEnum */ @TableField("role") private Integer role; diff --git a/hula-im-service/src/main/java/com/hula/core/chat/domain/entity/MessageMark.java b/hula-im-service/src/main/java/com/hula/core/chat/domain/entity/MessageMark.java index 6d808766164beaa2bb17b4806c762dd07f092c3a..79699807911253154076646be5fff84a47ca865d 100644 --- a/hula-im-service/src/main/java/com/hula/core/chat/domain/entity/MessageMark.java +++ b/hula-im-service/src/main/java/com/hula/core/chat/domain/entity/MessageMark.java @@ -46,8 +46,6 @@ public class MessageMark implements Serializable { /** * 标记类型 1点赞 2举报 - * - * @see com.abin.mallchat.common.chat.domain.enums.MessageMarkTypeEnum */ @TableField("type") private Integer type; diff --git a/hula-im-service/src/main/java/com/hula/core/chat/service/impl/RoomAppServiceImpl.java b/hula-im-service/src/main/java/com/hula/core/chat/service/impl/RoomAppServiceImpl.java index 0f37c1a40aaa33186bf46c63cd6334c959624caa..0c59c396d11377970664c851b7688b5702ceb720 100644 --- a/hula-im-service/src/main/java/com/hula/core/chat/service/impl/RoomAppServiceImpl.java +++ b/hula-im-service/src/main/java/com/hula/core/chat/service/impl/RoomAppServiceImpl.java @@ -314,7 +314,12 @@ public class RoomAppServiceImpl implements RoomAppService { Message message = msgMap.get(room.getLastMsgId()); if (Objects.nonNull(message)) { AbstractMsgHandler strategyNoNull = MsgHandlerFactory.getStrategyNoNull(message.getType()); - resp.setText(lastMsgUidMap.get(message.getFromUid()).getName() + ":" + strategyNoNull.showContactMsg(message)); + // 判断是群聊还是单聊 + if (Objects.equals(roomBaseInfo.getType(), RoomTypeEnum.GROUP.getType())) { + resp.setText(lastMsgUidMap.get(message.getFromUid()).getName() + ":" + strategyNoNull.showContactMsg(message)); + } else { + resp.setText(strategyNoNull.showContactMsg(message)); + } } resp.setUnreadCount(unReadCountMap.getOrDefault(room.getRoomId(), 0)); return resp; diff --git a/hula-im-service/src/main/java/com/hula/core/user/service/impl/LoginServiceImpl.java b/hula-im-service/src/main/java/com/hula/core/user/service/impl/LoginServiceImpl.java index daf8b5c92adc32428301af4849652e021d430412..6f8d9ef81778b1d3cd2bf8940ed1fb091bfc6c24 100644 --- a/hula-im-service/src/main/java/com/hula/core/user/service/impl/LoginServiceImpl.java +++ b/hula-im-service/src/main/java/com/hula/core/user/service/impl/LoginServiceImpl.java @@ -1,5 +1,6 @@ package com.hula.core.user.service.impl; +import cn.hutool.core.util.StrUtil; import com.hula.common.constant.RedisKey; import com.hula.common.utils.JwtUtils; import com.hula.common.utils.RedisUtils; @@ -17,49 +18,61 @@ import java.util.concurrent.TimeUnit; @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; + //token过期时间 + private static final Integer TOKEN_EXPIRE_DAYS = 5; + //token续期时间 + private static final Integer TOKEN_RENEWAL_DAYS = 2; + /** + * 校验token是不是有效 + */ @Override public boolean verify(String token) { - return false; + Long uid = jwtUtils.getUidOrNull(token); + if (Objects.isNull(uid)) { + return false; + } + String key = RedisKey.getKey(RedisKey.USER_TOKEN_STRING, uid); + String realToken = RedisUtils.getStr(key); + return Objects.equals(token, realToken);//有可能token失效了,需要校验是不是和最新token一致 } - @Override @Async + @Override public void renewalTokenIfNecessary(String token) { Long uid = jwtUtils.getUidOrNull(token); if (Objects.isNull(uid)) { return; } String key = RedisKey.getKey(RedisKey.USER_TOKEN_STRING, uid); - Long expireDays = RedisUtils.getExpire(key, TimeUnit.DAYS); - if (expireDays == -2) { + long expireDays = RedisUtils.getExpire(key, TimeUnit.DAYS); + if (expireDays == -2) {//不存在的key return; } - if (expireDays < TOKEN_RENEWAL_DAYS) { + if (expireDays < TOKEN_RENEWAL_DAYS) {//小于一天的token帮忙续期 RedisUtils.expire(key, TOKEN_EXPIRE_DAYS, TimeUnit.DAYS); } } @Override public String login(Long uid) { - String token = jwtUtils.createToken(uid); String key = RedisKey.getKey(RedisKey.USER_TOKEN_STRING, uid); - RedisUtils.set(key, token, TOKEN_EXPIRE_DAYS, TimeUnit.DAYS); + String token = RedisUtils.getStr(key); + if (StrUtil.isNotBlank(token)) { + return token; + } + //获取用户token + token = jwtUtils.createToken(uid); + RedisUtils.set(key, token, TOKEN_EXPIRE_DAYS, TimeUnit.DAYS);//token过期用redis中心化控制,初期采用5天过期,剩1天自动续期的方案。后续可以用双token实现 return token; } @Override public Long getValidUid(String token) { - Long uid = jwtUtils.getUidOrNull(token); - if (Objects.isNull(uid)) { - return null; - } - String key = RedisKey.getKey(RedisKey.USER_TOKEN_STRING, uid); - String oldToken = RedisUtils.getStr(key); - return Objects.equals(oldToken, token) ? uid : null; + boolean verify = verify(token); + return verify ? jwtUtils.getUidOrNull(token) : null; } } diff --git a/hula-im-service/src/main/java/com/hula/core/websocket/NettyWebSocketServer.java b/hula-im-service/src/main/java/com/hula/core/websocket/NettyWebSocketServer.java index 0b42e05202d9081178d281f267a93a8d615c88e2..b050679a2a28a4106f6af8be4a9d6dc3c80ecaa9 100644 --- a/hula-im-service/src/main/java/com/hula/core/websocket/NettyWebSocketServer.java +++ b/hula-im-service/src/main/java/com/hula/core/websocket/NettyWebSocketServer.java @@ -14,6 +14,7 @@ import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import io.netty.handler.stream.ChunkedWriteHandler; +import io.netty.handler.timeout.IdleStateHandler; import io.netty.util.NettyRuntime; import io.netty.util.concurrent.Future; import jakarta.annotation.PostConstruct; @@ -67,7 +68,7 @@ public class NettyWebSocketServer { protected void initChannel(SocketChannel socketChannel) throws Exception { ChannelPipeline pipeline = socketChannel.pipeline(); //30秒客户端没有向服务器发送心跳则关闭连接 -// pipeline.addLast(new IdleStateHandler(30, 0, 0)); + pipeline.addLast(new IdleStateHandler(30, 0, 0)); // 因为使用http协议,所以需要使用http的编码器,解码器 pipeline.addLast(new HttpServerCodec()); // 以块方式写,添加 chunkedWriter 处理器 diff --git a/hula-im-service/src/main/java/com/hula/core/websocket/NettyWebSocketServerHandler.java b/hula-im-service/src/main/java/com/hula/core/websocket/NettyWebSocketServerHandler.java index 84bb52a35ddd650a59e904f846b742294359c0ff..c1ecc6864edf4f54e370148a253502621f09c7cb 100644 --- a/hula-im-service/src/main/java/com/hula/core/websocket/NettyWebSocketServerHandler.java +++ b/hula-im-service/src/main/java/com/hula/core/websocket/NettyWebSocketServerHandler.java @@ -58,10 +58,6 @@ public class NettyWebSocketServerHandler extends SimpleChannelInboundHandler