Bladeren bron

fix : 新增用户角色活跃信息上报接口

bilingfeng 1 jaar geleden
bovenliggende
commit
c2bb6f6552

+ 6 - 0
game-module/game-module-sdk/pom.xml

@@ -127,6 +127,12 @@
             <artifactId>springfox-swagger-ui</artifactId>
             <version>${swagger2.ui.version}</version>
         </dependency>
+        <!-- kafka -->
+        <dependency>
+            <groupId>org.apache.kafka</groupId>
+            <artifactId>kafka-clients</artifactId>
+            <version>2.2.1</version>
+        </dependency>
     </dependencies>
 
     <build>

+ 1 - 1
game-module/game-module-sdk/src/main/java/com/zanxiang/game/module/sdk/SDKApplication.java

@@ -23,7 +23,7 @@ public class SDKApplication {
 
     public static void main(String[] args) {
         SpringApplication.run(SDKApplication.class, args);
-        System.out.println("赞象SDK服务启动成功 <多表增加字段, 解决线上bug问题> ( ´・・)ノ(._.`) \n" +
+        System.out.println("赞象SDK服务启动成功 <新增用户角色活跃信息上报接口> ( ´・・)ノ(._.`) \n" +
                 " ___________ _   __\n" +
                 "/  ___|  _  \\ | / /\n" +
                 "\\ `--.| | | | |/ / \n" +

+ 41 - 0
game-module/game-module-sdk/src/main/java/com/zanxiang/game/module/sdk/config/KafkaConfig.java

@@ -0,0 +1,41 @@
+package com.zanxiang.game.module.sdk.config;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.kafka.clients.producer.KafkaProducer;
+import org.apache.kafka.clients.producer.ProducerConfig;
+import org.apache.kafka.common.serialization.StringSerializer;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import java.net.InetAddress;
+import java.util.Properties;
+
+/**
+ * @author : lingfeng
+ * @time : 2022-09-28
+ * @description : kafka配置
+ */
+@Slf4j
+@Configuration
+public class KafkaConfig {
+
+    @Value("${spring.kafka.game-sdk.bootstrap-servers}")
+    private String gameSdkKafkaSevers;
+
+    @Bean("gameSdkKafkaProducer")
+    public KafkaProducer<String, String> gameKafkaProducer() {
+        String clientId = "UNKNOWN";
+        try {
+            clientId = InetAddress.getLocalHost().getHostAddress();
+        } catch (Exception e) {
+            log.error(e.getMessage(), e);
+        }
+        Properties props = new Properties();
+        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, gameSdkKafkaSevers);
+        props.put(ProducerConfig.CLIENT_ID_CONFIG, clientId);
+        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
+        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
+        return new KafkaProducer<>(props);
+    }
+}

+ 7 - 0
game-module/game-module-sdk/src/main/java/com/zanxiang/game/module/sdk/controller/UserController.java

@@ -106,4 +106,11 @@ public class UserController {
         return ResultVO.ok(result);
     }
 
+    @ApiOperation(value = "用户活跃信息上报")
+    @PostMapping("/active/call/role")
+    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = Boolean.class)})
+    public ResultVO<Boolean> gameRoleActiveCall(@Validated @RequestBody GameRoleActiveCallParam param, @ValidLogin UserData userData) {
+        return ResultVO.ok(gameUserRoleService.gameRoleActiveCall(userData, param));
+    }
+
 }

+ 37 - 0
game-module/game-module-sdk/src/main/java/com/zanxiang/game/module/sdk/pojo/param/GameRoleActiveCallParam.java

@@ -0,0 +1,37 @@
+package com.zanxiang.game.module.sdk.pojo.param;
+
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import javax.validation.constraints.NotBlank;
+import javax.validation.constraints.NotNull;
+
+/**
+ * @author : lingfeng
+ * @time : 2023-08-10
+ * @description : 用户角色活跃上报参数
+ */
+@Data
+public class GameRoleActiveCallParam {
+
+    /**
+     * 服务器id
+     */
+    @NotBlank(message = "服务器id不可为空")
+    @ApiModelProperty(notes = "服务器id")
+    private String serverId;
+
+    /**
+     * 角色id
+     */
+    @NotBlank(message = "角色id不可为空")
+    @ApiModelProperty(notes = "角色id")
+    private String roleId;
+
+    /**
+     * 角色等级
+     */
+    @NotNull(message = "角色等级不可为空")
+    @ApiModelProperty(notes = "角色等级")
+    private Long roleLevel;
+}

+ 10 - 0
game-module/game-module-sdk/src/main/java/com/zanxiang/game/module/sdk/service/IGameUserRoleService.java

@@ -3,6 +3,7 @@ package com.zanxiang.game.module.sdk.service;
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.zanxiang.game.module.mybatis.entity.GameUserRole;
 import com.zanxiang.game.module.mybatis.entity.User;
+import com.zanxiang.game.module.sdk.pojo.param.GameRoleActiveCallParam;
 import com.zanxiang.game.module.sdk.pojo.param.GameUserRoleUpdateParam;
 import com.zanxiang.game.module.sdk.pojo.param.UserData;
 
@@ -33,4 +34,13 @@ public interface IGameUserRoleService extends IService<GameUserRole> {
      * @param userData 用户数据
      */
     void callListenIn(GameUserRoleUpdateParam param, UserData userData);
+
+    /**
+     * 游戏角色活跃上报
+     *
+     * @param userData 用户数据
+     * @param param    参数
+     * @return boolean
+     */
+    boolean gameRoleActiveCall(UserData userData, GameRoleActiveCallParam param);
 }

+ 32 - 0
game-module/game-module-sdk/src/main/java/com/zanxiang/game/module/sdk/service/impl/GameUserRoleServiceImpl.java

@@ -11,6 +11,7 @@ import com.zanxiang.game.module.mybatis.mapper.GameUserRoleMapper;
 import com.zanxiang.game.module.sdk.constant.RedisKeyConstant;
 import com.zanxiang.game.module.sdk.enums.DataTypeEnum;
 import com.zanxiang.game.module.sdk.enums.LoginTypeEnum;
+import com.zanxiang.game.module.sdk.pojo.param.GameRoleActiveCallParam;
 import com.zanxiang.game.module.sdk.pojo.param.GameUserRoleUpdateParam;
 import com.zanxiang.game.module.sdk.pojo.param.UserData;
 import com.zanxiang.game.module.sdk.service.IGameUserRoleService;
@@ -21,7 +22,11 @@ import com.zanxiang.module.redis.service.IDistributedLockComponent;
 import com.zanxiang.module.util.DateUtil;
 import com.zanxiang.module.util.JsonUtil;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.kafka.clients.producer.KafkaProducer;
+import org.apache.kafka.clients.producer.ProducerRecord;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.client.RestTemplate;
@@ -53,6 +58,13 @@ public class GameUserRoleServiceImpl extends ServiceImpl<GameUserRoleMapper, Gam
     @Autowired
     private IDistributedLockComponent distributedLockComponent;
 
+    @Value("${spring.kafka.game-sdk.gameRoleActiveTopic}")
+    private String gameRoleActiveTopic;
+
+    @Autowired
+    @Qualifier("gameSdkKafkaProducer")
+    private KafkaProducer<String, String> kafkaProducer;
+
     @Override
     @Transactional(rollbackFor = Exception.class)
     public Boolean updateUserGameRole(GameUserRoleUpdateParam param, UserData userData) {
@@ -226,4 +238,24 @@ public class GameUserRoleServiceImpl extends ServiceImpl<GameUserRoleMapper, Gam
         } catch (Exception ignored) {
         }
     }
+
+    @Override
+    public boolean gameRoleActiveCall(UserData userData, GameRoleActiveCallParam param) {
+        log.error("接收到前端用户角色活跃上报, userId : {}, gameId : {}, serverId : {}, roleId : {}, roleLevel : {}",
+                userData.getUserId(), userData.getGameId(), param.getServerId(), param.getRoleId(), param.getRoleLevel());
+        Map<String, Object> activeParamMap = new HashMap<>(6);
+        activeParamMap.put("userId", userData.getUserId());
+        activeParamMap.put("gameId", userData.getGameId());
+        activeParamMap.put("serverId", param.getServerId());
+        activeParamMap.put("roleId", param.getRoleId());
+        activeParamMap.put("roleLevel", param.getRoleLevel());
+        activeParamMap.put("activeTime", System.currentTimeMillis());
+        try {
+            kafkaProducer.send(new ProducerRecord<>(gameRoleActiveTopic, userData.getUserId().toString(), JsonUtil.toString(activeParamMap)));
+        } catch (Exception e) {
+            log.error("用户角色活跃信息发送到 Kafka 异常!, activeParamMap : {}, e : {}", JsonUtil.toString(activeParamMap), e.getMessage());
+            return Boolean.FALSE;
+        }
+        return Boolean.TRUE;
+    }
 }