从服务端埋点到Kafka(二)

主要操作

主要的内容就是根据模型的字段要求,在相应的游戏逻辑执行时将埋点的json串发送到Kafka

代码实现

由于游戏服务端采用Java编写,所以现在以Java为例进行说明

public abstract class LogEvent implements Serializable {
   
	private static final Gson GSON = new Gson();
	public int serverId;
	protected long createTime;
	protected String logName = "";

	public LogEvent() {
   
		this.createTime = System.currentTimeMillis();
		this.serverId = LogHelper.getServerId();
		this.logName = getLogType().getName();
	}

	/**
	 * 日志类型
	 * @return
	 */
	public abstract LogType getLogType();

	//向Kafka发送日志
	public void send() {
   
		try {
   
			KafkaClient.send("xxxxLog", toString());
		} catch (Exception e) {
   
			e.printStackTrace();
		}
	}

	 @Override
	 public String toString() {
   
		 return GSON.toJson(this);
	 }
}



public abstract class CommonLogEvent extends LogEvent {
   
	// 用户账户
	public String account;
	// 用户账户ID
	public Long userId;
	// 角色ID
	public long roleId;
	// 角色等级
	public Integer roleLevel;
	// 角色名称
	public String roleName;
	// 渠道
	public String channelKey;
	// 设备ID
	public String deviceId;
	// 性别
	public Integer gender;

	public CommonLogEvent () {
   
	}

	public CommonLogEvent (PlayerControl playerControl) 
	{
   	
		//此处的字段根据游戏服务端相应的实现来赋值
		this.account = playerControl.getUserControl().getUsername();
		this.userId = playerControl.getUserControl().getId();
		this.roleId = playerControl.getId();
		this.roleLevel = playerControl.getLevel();
		this.roleName = playerControl.get(Attribute.name);
		this.channelKey = playerControl.getUserControl().getUserChannel();
		this.deviceId = playerControl.getUserControl().getDeviceId();
		this.serverId = ServerManager.getServerId();
	}
}

/**
 * 事件类型枚举类
 */
public enum LogType {
   
	LoginEntity(1, "LoginEntity"),
	CampEntity(2, "CampEntity"),
	CreateRoleEntity(3, "CreateRoleEntity"),
	CreateUserEntity(4, "CreateUserEntity"),
	LogoutEntity(5, "LogoutEntity"),
	OnlineTimeEntity(6, "OnlineTimeEntity"),
	RemoveRoleEntity(7, "RemoveRoleEntity"),
	CostCurrencyEntity(9, "CostCurrencyEntity"),
	GetCurrencyEntity(10, "GetCurrencyEntity"),
	DressEntity(11, "DressEntity"),
	EquipNoEntity(12, "EquipNoEntity"),
	EquipSlotEntity(13, "EquipSlotEntity"),
	FriendLogEntity(14, "FriendLogEntity"),
	CostItemEntity(16, "CostItemEntity"),
	GetItemEntity(17, "GetItemEntity"),
	CopyEntity(19, "CopyEntity"),
	GuildEntity(20, "GuildEntity"),
	GuildMemberEntity(21, "GuildMemberEntity"),
	LevelEntity(22, "LevelEntity"),
	MailEntity(23, "MailEntity"),
	SkillPositionEntity(26, "SkillPositionEntity"),
	TaskEntity(28, "TaskEntity"),
	RechargeEntity(29, "RechargeEntity"),
	GameActivityEntity(31, "GameActivityEntity"),
	MartialEntity(32, "MartialEntity"),
	MountEntity(33, "MountEntity"),
	PetEntity(34, "PetEntity"),
	ShopEntity(35, "ShopEntity"),
	NpcGiftEntity(36, "NpcGiftEntity"),
	BadRyKillEntity(37, "BadRyKillEntity"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Flink CDC 从 MySQL 同步到 Kafka 的流程如下: 1. 配置 MySQL 数据源:在 Flink CDC 中,使用 JDBC Connector 连接 MySQL 数据库,并配置相应的参数,例如数据库连接 URL、用户名、密码等。 2. 配置 Kafka 数据接收器:使用 Kafka Connector 连接 Kafka,配置相应的参数,例如 Kafka 主题、Kafka Broker 地址等。 3. 创建 Flink CDC 任务:使用 Flink SQL 或 Flink Table API 创建 Flink CDC 任务,并配置相应的数据源和数据接收器。 4. 启动 Flink CDC 任务:使用 Flink 自带的命令行工具或 Web UI 启动 Flink CDC 任务,开始同步 MySQL 数据到 Kafka 中。 具体的步骤如下: 1. 下载并安装 Flink:从 Flink 官网下载并安装 Flink。 2. 配置 MySQL 数据源:在 Flink 的 conf 目录下创建一个新的文件,例如 mysql.properties,配置 MySQL 数据源相关的参数,例如: ``` connector.class = jdbc connector.url = jdbc:mysql://localhost:3306/test?useSSL=false connector.table = my_table connector.username = root connector.password = password ``` 3. 配置 Kafka 数据接收器:在 Flink 的 conf 目录下创建一个新的文件,例如 kafka.properties,配置 Kafka 数据接收器相关的参数,例如: ``` connector.class = kafka connector.topic = my_topic connector.properties.bootstrap.servers = localhost:9092 ``` 4. 创建 Flink CDC 任务:使用 Flink SQL 或 Flink Table API 创建 Flink CDC 任务,例如: ``` CREATE TABLE my_table ( id INT, name STRING, age INT ) WITH ( 'connector' = 'jdbc', 'url' = 'jdbc:mysql://localhost:3306/test?useSSL=false', 'table-name' = 'my_table', 'username' = 'root', 'password' = 'password' ); CREATE TABLE my_topic ( id INT, name STRING, age INT ) WITH ( 'connector' = 'kafka', 'topic' = 'my_topic', 'properties.bootstrap.servers' = 'localhost:9092' ); INSERT INTO my_topic SELECT * FROM my_table; ``` 5. 启动 Flink CDC 任务:使用 Flink 自带的命令行工具或 Web UI 启动 Flink CDC 任务,例如: ``` ./bin/flink run -c com.example.MyCDCJob /path/to/my/cdc/job.jar ``` 通过以上步骤,就可以实现从 MySQL 同步数据到 Kafka 中的流程。需要注意的是,Flink CDC 可以根据实际的需求进行调整,例如任务并行度、缓冲区大小等参数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值