This commit is contained in:
zhaohu
2026-06-12 16:14:58 +08:00
parent cf71897528
commit a7ba0f21e5
235 changed files with 33574 additions and 0 deletions
@@ -0,0 +1,3 @@
#自定义配置
apollo:
name: apollo-name
+51
View File
@@ -0,0 +1,51 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springblade</groupId>
<artifactId>blade-example</artifactId>
<version>${revision}</version>
</parent>
<artifactId>blade-apollo</artifactId>
<dependencies>
<!--Blade-->
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-biz-common</artifactId>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-cloud</artifactId>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-launch</artifactId>
</dependency>
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>2.2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<configuration>
<skip>${docker.fabric.skip}</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,46 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.cloud.client.BladeCloudApplication;
import org.springblade.core.launch.BladeApplication;
/**
* Apollo启动器
*
* @author Chill
*/
@Slf4j
@BladeCloudApplication
public class ApolloApplication {
public static void main(String[] args) {
BladeApplication.run("blade-apollo", ApolloApplication.class, args);
}
}
@@ -0,0 +1,52 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.listener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
/**
* ApolloListener
*
* @author Chill
*/
@Slf4j
@Component
public class ApolloListener {
@Value("${apollo.name:undefined}")
private String name;
@EventListener(ApplicationReadyEvent.class)
public void handleApplicationReady() {
log.info("====================ApolloListener ready to work====================");
log.info("apollo.name:{}", name);
log.info("====================ApolloListener ready to work====================");
}
}
@@ -0,0 +1,13 @@
#服务器端口
server:
port: 8508
app:
id: blade-apollo
apollo:
#apollo官方文档:https://www.apolloconfig.com/
#apollo官方测试地址:http://81.68.181.139 ,账号/密码:apollo/admin
meta: http://81.68.181.139:8080
bootstrap:
enabled: true
namespaces: application,common
+24
View File
@@ -0,0 +1,24 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springblade</groupId>
<artifactId>blade-example</artifactId>
<version>${revision}</version>
</parent>
<artifactId>blade-mqtt-broker</artifactId>
<name>${project.artifactId}</name>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>net.dreamlu</groupId>
<artifactId>mica-mqttx-server-spring-boot-starter</artifactId>
<version>3.1.12</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,47 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.mqtt.broker;
import org.springblade.core.launch.BladeApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import static org.springblade.common.constant.LauncherConstant.APPLICATION_MQTT_BROKER_NAME;
/**
* MQTT Broker 启动器
*
* @author Chill
*/
@EnableScheduling
@SpringBootApplication
public class MqttBrokerApplication {
public static void main(String[] args) {
BladeApplication.run(APPLICATION_MQTT_BROKER_NAME, MqttBrokerApplication.class, args);
}
}
@@ -0,0 +1,107 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.mqtt.broker.listener;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.iot.mqtt.spring.server.event.MqttClientOfflineEvent;
import net.dreamlu.iot.mqtt.spring.server.event.MqttClientOnlineEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
/**
* 客户端上下线监听器
*
* @author Chill
*/
@Slf4j
@Service
public class MqttConnectStatusListener {
@EventListener
public void online(MqttClientOnlineEvent event) {
log.info("MQTT客户端上线: {}", event);
// 客户端上线处理逻辑
String clientId = event.getClientId();
log.info("客户端上线 - clientId: {}", clientId);
// 可以在这里添加上线处理逻辑,例如:
// 1. 记录上线日志
// 2. 更新设备状态
// 3. 发送上线通知
// 4. 统计在线数量等
handleClientOnline(clientId);
}
@EventListener
public void offline(MqttClientOfflineEvent event) {
log.info("MQTT客户端下线: {}", event);
// 客户端下线处理逻辑
String clientId = event.getClientId();
log.info("客户端下线 - clientId: {}", clientId);
// 可以在这里添加下线处理逻辑,例如:
// 1. 记录下线日志
// 2. 更新设备状态
// 3. 发送下线通知
// 4. 清理资源等
handleClientOffline(clientId);
}
/**
* 处理客户端上线
*
* @param clientId 客户端ID
*/
private void handleClientOnline(String clientId) {
try {
// 添加具体的上线处理逻辑
log.debug("执行客户端上线处理: clientId={}", clientId);
} catch (Exception e) {
log.error("处理客户端上线异常: clientId={}", clientId, e);
}
}
/**
* 处理客户端下线
*
* @param clientId 客户端ID
*/
private void handleClientOffline(String clientId) {
try {
// 添加具体的下线处理逻辑
log.debug("执行客户端下线处理: clientId={}", clientId);
} catch (Exception e) {
log.error("处理客户端下线异常: clientId={}", clientId, e);
}
}
}
@@ -0,0 +1,91 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.mqtt.broker.listener;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.iot.mqtt.codec.MqttPublishMessage;
import net.dreamlu.iot.mqtt.codec.MqttQoS;
import net.dreamlu.iot.mqtt.core.server.event.IMqttMessageListener;
import org.springframework.stereotype.Service;
import org.tio.core.ChannelContext;
import org.tio.utils.buffer.ByteBufferUtil;
/**
* 服务端接收消息监听器
*
* @author Chill
*/
@Slf4j
@Service
public class MqttServerMessageListener implements IMqttMessageListener {
@Override
public void onMessage(ChannelContext context, String clientId, String topic, MqttQoS qoS, MqttPublishMessage message) {
log.info("收到MQTT消息 - clientId: {}, topic: {}, qoS: {}, payload: {}",
clientId, topic, qoS, ByteBufferUtil.toString(message.getPayload()));
// 自定义数据流转逻辑
// 可以在这里添加消息处理逻辑,例如:
// 1. 数据解析和格式化
// 2. 业务规则处理
// 3. 数据存储
// 4. 消息转发
// 5. 告警处理等
handleMessage(clientId, topic, message);
}
/**
* 处理接收到的消息
*
* @param clientId 客户端ID
* @param topic 主题
* @param message 消息
*/
private void handleMessage(String clientId, String topic, MqttPublishMessage message) {
try {
String payload = ByteBufferUtil.toString(message.getPayload());
// 根据主题进行不同的处理
if (topic.contains("/property/")) {
log.info("处理属性消息: {}", payload);
// 处理设备属性上报
} else if (topic.contains("/event/")) {
log.info("处理事件消息: {}", payload);
// 处理设备事件上报
} else if (topic.contains("/command/")) {
log.info("处理命令消息: {}", payload);
// 处理设备命令响应
} else {
log.info("处理通用消息: {}", payload);
// 处理其他类型消息
}
} catch (Exception e) {
log.error("处理MQTT消息异常: clientId={}, topic={}", clientId, topic, e);
}
}
}
@@ -0,0 +1,49 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.mqtt.broker.service;
import org.tio.core.ChannelContext;
/**
* MQTT 认证服务
*
* @author Chill
*/
public interface IMqttAuthService {
/**
* 设备连接认证
*
* @param context ChannelContext
* @param clientId clientId
* @param userName userName
* @param password password
* @param clientNodeIp clientNodeIp
* @return 是否成功
*/
boolean hasAuth(ChannelContext context, String clientId, String userName, String password, String clientNodeIp);
}
@@ -0,0 +1,67 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.mqtt.broker.service.impl;
import lombok.extern.slf4j.Slf4j;
import org.springblade.mqtt.broker.service.IMqttAuthService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.tio.core.ChannelContext;
/**
* MQTT 认证处理服务
*
* @author Chill
*/
@Slf4j
@Service
public class MqttAuthServiceImpl implements IMqttAuthService {
/**
* MQTT 认证最大间隔时间,默认 5 分钟
*/
@Value("${mqtt.auth.timeSecondDiff:300}")
private int timeSecondDiff;
@Override
public boolean hasAuth(ChannelContext context, String clientId, String userName, String password, String clientNodeIp) {
log.info("MQTT 客户端认证: clientId={}, userName={}, clientNodeIp={}", clientId, userName, clientNodeIp);
// 自定义账号认证逻辑
// 这里可以根据实际需求添加具体的认证逻辑
// 例如:查询数据库验证用户名密码、检查设备白名单等
// 简单的demo认证逻辑:允许所有连接
if (userName != null && password != null) {
log.info("MQTT 客户端认证成功: clientId={}", clientId);
return true;
}
log.warn("MQTT 客户端认证失败: clientId={}", clientId);
return false;
}
}
@@ -0,0 +1,45 @@
# 服务器配置
server:
port: 8290
undertow:
# 以下的配置会影响buffer,这些buffer会用于服务器连接的IO操作,有点类似netty的池化内存管理
buffer-size: 1024
# 是否分配的直接内存
direct-buffers: true
# 线程配置
threads:
# 设置IO线程数, 它主要执行非阻塞的任务,它们会负责多个连接, 默认设置每个CPU核心一个线程
io: 16
# 阻塞任务线程池, 当执行类似servlet请求阻塞操作, undertow会从这个线程池中取得线程,它的值设置取决于系统的负载
worker: 400
servlet:
# 编码配置
encoding:
charset: UTF-8
force: true
# MQTT服务端配置
mqtt:
server:
enabled: true # 是否开启服务端,默认:true
port: 11883 # 端口,默认:1883
name: Blade-MQTT-Broker # 名称,默认:Mica-Mqtt-Server
heartbeat-timeout: 120000 # 心跳超时,单位毫秒,默认: 1000 * 120
read-buffer-size: 8KB # 接收数据的 buffer size,默认:8k
max-bytes-in-message: 10MB # 消息解析最大 bytes 长度,默认:10M
auth:
enable: false # 是否开启 mqtt 认证
debug: true # 如果开启 prometheus 指标收集建议关闭
stat-enable: true # 开启指标收集,debug 和 prometheus 开启时需要打开,默认开启,关闭节省内存
web-port: 28083 # http、websocket 端口,默认:8083
websocket-enable: true # 是否开启 websocket,默认: true
http-enable: false # 是否开启 http api,默认: false
ssl: # mqtt tcp ssl 认证
enabled: false # 是否开启 ssl 认证,2.1.0 开始支持双向认证
keystore-path: # 必须参数:ssl keystore 目录,支持 classpath:/ 路径。
keystore-pass: # 必选参数:ssl keystore 密码
truststore-path: # 可选参数:ssl 双向认证 truststore 目录,支持 classpath:/ 路径。
truststore-pass: # 可选参数:ssl 双向认证 truststore 密码
client-auth: none # 是否需要客户端认证(双向认证),默认:NONE(不需要)
auth:
timeSecondDiff: 300 # 认证时间差,默认5分钟
+24
View File
@@ -0,0 +1,24 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springblade</groupId>
<artifactId>blade-example</artifactId>
<version>${revision}</version>
</parent>
<artifactId>blade-mqtt-client</artifactId>
<name>${project.artifactId}</name>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>net.dreamlu</groupId>
<artifactId>mica-mqttx-client</artifactId>
<version>3.1.12</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,47 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.mqtt.client;
import org.springblade.core.launch.BladeApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import static org.springblade.common.constant.LauncherConstant.APPLICATION_MQTT_CLIENT_NAME;
/**
* MQTT Client 启动器
*
* @author Chill
*/
@EnableScheduling
@SpringBootApplication
public class MqttClientApplication {
public static void main(String[] args) {
BladeApplication.run(APPLICATION_MQTT_CLIENT_NAME, MqttClientApplication.class, args);
}
}
@@ -0,0 +1,98 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.mqtt.client.listener;
import lombok.extern.slf4j.Slf4j;
import org.springblade.mqtt.client.simulator.DeviceSimulator;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
/**
* 设备应用监听器
*
* @author Chill
*/
@Slf4j
@Component
public class DeviceListener {
@Value("${mqtt.client.productKey:DEMO_PRODUCT}")
private String productKey;
@Value("${mqtt.client.deviceName:DEMO_DEVICE}")
private String deviceName;
@Value("${mqtt.client.deviceSecret:demo_secret_123}")
private String deviceSecret;
@Value("${mqtt.client.serverHost:localhost}")
private String serverHost;
@Value("${mqtt.client.serverPort:1883}")
private int serverPort;
@Value("${mqtt.client.enabled:true}")
private boolean enabled;
@EventListener(ApplicationReadyEvent.class)
public void handleApplicationReady() {
if (!enabled) {
log.info("MQTT客户端已禁用,跳过启动");
return;
}
log.info("应用启动完成,开始初始化MQTT客户端");
log.info("配置信息: productKey={}, deviceName={}, serverHost={}:{}",
productKey, deviceName, serverHost, serverPort);
try {
// 初始化设备模拟器
DeviceSimulator simulator = new DeviceSimulator(
productKey,
deviceName,
deviceSecret,
serverHost,
serverPort
);
// 启动模拟器
simulator.start();
log.info("设备模拟器启动成功");
// 添加关闭钩子
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
log.info("应用关闭,停止设备模拟器");
simulator.stop();
}));
} catch (Exception e) {
log.error("启动设备模拟器失败", e);
}
}
}
@@ -0,0 +1,282 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.mqtt.client.simulator;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.iot.mqtt.core.client.MqttClient;
import org.springblade.mqtt.client.support.DataReq;
import org.springblade.mqtt.client.support.NtpReq;
import org.tio.utils.buffer.ByteBufferUtil;
import org.tio.utils.json.JsonUtil;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* 设备模拟器
*
* @author Chill
*/
@Slf4j
@RequiredArgsConstructor
public class DeviceSimulator {
private final String productKey;
private final String deviceName;
private final String deviceSecret;
private final String serverHost;
private final int serverPort;
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(4);
/**
* 启动模拟器
*/
public void start() {
log.info("启动设备模拟器: productKey={}, deviceName={}", productKey, deviceName);
// 初始化 MQTT 客户端
String clientId = generateClientId();
String username = generateUsername();
String password = generatePassword();
log.info("连接配置: clientId={}, username={}, host={}:{}", clientId, username, serverHost, serverPort);
try {
MqttClient client = MqttClient.create()
.ip(serverHost)
.port(serverPort)
.username(username)
.password(password)
.clientId(clientId)
.connectSync();
log.info("MQTT客户端连接成功");
// 订阅主题
subscribeTopics(client);
// 启动任务调度
scheduleTasks(client);
} catch (Exception e) {
log.error("MQTT客户端连接失败", e);
}
}
/**
* 生成客户端ID
*
* @return clientId
*/
private String generateClientId() {
return productKey + "_" + deviceName + "_" + System.currentTimeMillis();
}
/**
* 生成用户名
*
* @return username
*/
private String generateUsername() {
return deviceName + "&" + productKey;
}
/**
* 生成密码
*
* @return password
*/
private String generatePassword() {
// 简化的密码生成逻辑
return deviceSecret;
}
/**
* 订阅主题
*
* @param client MqttClient
*/
private void subscribeTopics(MqttClient client) {
// 订阅设备相关主题
String topicPrefix = "/blade/sys/" + productKey + "/" + deviceName;
// 订阅属性设置
client.subQos0(topicPrefix + "/thing/service/property/set", (context, topic, message, payload) -> {
log.info("收到属性设置: topic={}, payload={}", topic, ByteBufferUtil.toString(payload));
});
// 订阅服务调用
client.subQos0(topicPrefix + "/thing/service/+", (context, topic, message, payload) -> {
log.info("收到服务调用: topic={}, payload={}", topic, ByteBufferUtil.toString(payload));
});
// 订阅NTP响应
client.subQos0("/blade/ext/ntp/" + productKey + "/" + deviceName + "/response", (context, topic, message, payload) -> {
log.info("收到NTP响应: topic={}, payload={}", topic, ByteBufferUtil.toString(payload));
});
log.info("已订阅设备主题: {}", topicPrefix);
}
/**
* 任务调度
*
* @param client MqttClient
*/
private void scheduleTasks(MqttClient client) {
// 定期上报属性数据(每30秒)
scheduler.scheduleAtFixedRate(() -> {
publishPropertyData(client);
}, 5, 30, TimeUnit.SECONDS);
// 定期上报事件数据(每60秒)
scheduler.scheduleAtFixedRate(() -> {
publishEventData(client);
}, 10, 60, TimeUnit.SECONDS);
// 定期发送心跳数据(每120秒)
scheduler.scheduleAtFixedRate(() -> {
publishHeartbeat(client);
}, 15, 120, TimeUnit.SECONDS);
// 定期请求NTP时间(每300秒)
scheduler.scheduleAtFixedRate(() -> {
publishNtpRequest(client);
}, 20, 300, TimeUnit.SECONDS);
log.info("已启动定时任务调度");
}
/**
* 发布属性数据
*
* @param client MqttClient
*/
private void publishPropertyData(MqttClient client) {
try {
DataReq<Map<String, Object>> req = new DataReq<>();
req.setId(UUID.randomUUID().toString().replace("-", ""));
req.setVersion("1.0");
Map<String, Object> params = new HashMap<>();
params.put("temperature", 20 + Math.random() * 10); // 温度:20-30度
params.put("humidity", 40 + Math.random() * 30); // 湿度:40-70%
params.put("lightSwitch", Math.random() > 0.5 ? 1 : 0); // 灯开关
req.setParams(params);
String topic = "/blade/sys/" + productKey + "/" + deviceName + "/thing/event/property/post";
client.publish(topic, JsonUtil.toJsonBytes(req));
log.info("已发布属性数据: {}", JsonUtil.toJsonString(params));
} catch (Exception e) {
log.error("发布属性数据失败", e);
}
}
/**
* 发布事件数据
*
* @param client MqttClient
*/
private void publishEventData(MqttClient client) {
try {
DataReq<Map<String, Object>> req = new DataReq<>();
req.setId(UUID.randomUUID().toString().replace("-", ""));
req.setVersion("1.0");
Map<String, Object> params = new HashMap<>();
Map<String, Object> output = new HashMap<>();
output.put("eventLevel", "INFO");
output.put("eventMessage", "设备运行正常");
output.put("timestamp", System.currentTimeMillis());
params.put("output", JsonUtil.toJsonString(output));
params.put("eventName", "状态事件");
params.put("eventType", "info");
req.setParams(params);
String topic = "/blade/sys/" + productKey + "/" + deviceName + "/thing/event/StatusEvent/post";
client.publish(topic, JsonUtil.toJsonBytes(req));
log.info("已发布事件数据: {}", JsonUtil.toJsonString(params));
} catch (Exception e) {
log.error("发布事件数据失败", e);
}
}
/**
* 发布心跳数据
*
* @param client MqttClient
*/
private void publishHeartbeat(MqttClient client) {
try {
Map<String, Object> heartbeat = new HashMap<>();
heartbeat.put("deviceId", deviceName);
heartbeat.put("productKey", productKey);
heartbeat.put("timestamp", System.currentTimeMillis());
heartbeat.put("status", "online");
String topic = "/blade/sys/" + productKey + "/" + deviceName + "/heartbeat";
client.publish(topic, JsonUtil.toJsonBytes(heartbeat));
log.info("已发布心跳数据: {}", JsonUtil.toJsonString(heartbeat));
} catch (Exception e) {
log.error("发布心跳数据失败", e);
}
}
/**
* 发布NTP请求
*
* @param client MqttClient
*/
private void publishNtpRequest(MqttClient client) {
try {
NtpReq req = new NtpReq();
req.setDeviceSendTime(String.valueOf(System.currentTimeMillis()));
String topic = "/blade/ext/ntp/" + productKey + "/" + deviceName + "/request";
client.publish(topic, JsonUtil.toJsonBytes(req));
log.info("已发布NTP请求: {}", JsonUtil.toJsonString(req));
} catch (Exception e) {
log.error("发布NTP请求失败", e);
}
}
/**
* 停止模拟器
*/
public void stop() {
scheduler.shutdown();
log.info("设备模拟器已停止");
}
}
@@ -0,0 +1,67 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.mqtt.client.support;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import java.io.Serializable;
/**
* 设备通用数据格式
*
* @author Chill
*/
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class DataReq<T> implements Serializable {
/**
* 消息ID号。uuid,去掉短横线,32位,全局唯一,用于ack或系统消息追踪
*/
private String id;
/**
* 协议版本号,目前协议版本号唯一取值为1.0
*/
private String version;
/**
* 扩展功能的参数,其下包含各功能字段。平台可扩展,或可自行扩展,自行扩展的参数需在自定义解析模块自行解析
*/
private SysBean sys;
/**
* 请求方法。
*/
private String method;
/**
* 请求参数
*/
private T params;
}
@@ -0,0 +1,45 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.mqtt.client.support;
import lombok.Data;
import java.io.Serializable;
/**
* NTP 请求
*
* @author Chill
*/
@Data
public class NtpReq implements Serializable {
/**
* 设备端发送时间
*/
private String deviceSendTime;
}
@@ -0,0 +1,62 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.mqtt.client.support;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* 扩展功能
*
* @author Chill
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SysBean implements Serializable {
/**
* 不响应 ack
*/
public static final int ACK_NO = 0;
/**
* 响应 ack
*/
public static final int ACK_NEED = 1;
public SysBean(boolean ackNeed) {
this(ackNeed ? ACK_NEED : ACK_NO);
}
/**
* 扩展功能字段,表示是否返回响应数据。0:不返回响应数据1:返回响应数据
*/
private int ack;
}
@@ -0,0 +1,29 @@
# 服务器配置
server:
port: 8291
undertow:
# 以下的配置会影响buffer,这些buffer会用于服务器连接的IO操作,有点类似netty的池化内存管理
buffer-size: 1024
# 是否分配的直接内存
direct-buffers: true
# 线程配置
threads:
# 设置IO线程数, 它主要执行非阻塞的任务,它们会负责多个连接, 默认设置每个CPU核心一个线程
io: 16
# 阻塞任务线程池, 当执行类似servlet请求阻塞操作, undertow会从这个线程池中取得线程,它的值设置取决于系统的负载
worker: 400
servlet:
# 编码配置
encoding:
charset: UTF-8
force: true
# MQTT客户端配置
mqtt:
client:
enabled: true # 是否启用MQTT客户端
productKey: "BLADE_DEMO_PRODUCT" # 产品标识
deviceName: "BLADE_DEMO_DEVICE_001" # 设备名称
deviceSecret: "blade_demo_secret_123456" # 设备密钥
serverHost: "localhost" # MQTT服务器地址
serverPort: 11883 # MQTT服务器端口
@@ -0,0 +1,27 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springblade</groupId>
<artifactId>blade-example</artifactId>
<version>${revision}</version>
</parent>
<artifactId>blade-rabbit-listener</artifactId>
<name>${project.artifactId}</name>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-tool</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,47 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.rabbit.listener;
import org.springblade.core.launch.BladeApplication;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import static org.springblade.common.constant.LauncherConstant.APPLICATION_RABBIT_LISTENER_NAME;
/**
* Rabbit 消费者启动器
*
* @author Chill
*/
@EnableRabbit
@SpringBootApplication
public class RabbitListenerApplication {
public static void main(String[] args) {
BladeApplication.run(APPLICATION_RABBIT_LISTENER_NAME, RabbitListenerApplication.class, args);
}
}
@@ -0,0 +1,92 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.rabbit.listener.config;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Rabbit 消费者配置
*
* @author Chill
*/
@Configuration
public class RabbitListenerConfiguration {
/**
* 示例队列名
*/
public static final String DEMO_QUEUE_NAME = "blade.demo.queue";
/**
* 消息队列名
*/
public static final String MESSAGE_QUEUE_NAME = "blade.message.queue";
/**
* 交换机名
*/
public static final String EXCHANGE_NAME = "blade.amqp.exchange";
/**
* 示例路由规则
*/
public static final String DEMO_ROUTING_KEY_PATTERN = "demo.#";
/**
* 消息路由规则
*/
public static final String MESSAGE_ROUTING_KEY_PATTERN = "message.#";
@Bean
public TopicExchange exchange() {
return new TopicExchange(EXCHANGE_NAME);
}
@Bean
public Queue demoQueue() {
return new Queue(DEMO_QUEUE_NAME);
}
@Bean
public Queue messageQueue() {
return new Queue(MESSAGE_QUEUE_NAME);
}
@Bean
public Binding demoBinding(Queue demoQueue, TopicExchange exchange) {
return BindingBuilder.bind(demoQueue).to(exchange).with(DEMO_ROUTING_KEY_PATTERN);
}
@Bean
public Binding messageBinding(Queue messageQueue, TopicExchange exchange) {
return BindingBuilder.bind(messageQueue).to(exchange).with(MESSAGE_ROUTING_KEY_PATTERN);
}
}
@@ -0,0 +1,116 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.rabbit.listener.listener;
import lombok.extern.slf4j.Slf4j;
import org.springblade.rabbit.listener.config.RabbitListenerConfiguration;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* Rabbit 消息消费者
*
* @author Chill
*/
@Slf4j
@Component
public class MessageConsumer {
/**
* 监听示例队列消息
*
* @param message 消息
*/
@RabbitListener(queues = RabbitListenerConfiguration.DEMO_QUEUE_NAME)
public void receiveDemoMessage(Message message) {
String routingKey = message.getMessageProperties().getReceivedRoutingKey();
String payload = new String(message.getBody());
log.info("收到示例消息: routingKey={}, payload={}", routingKey, payload);
// 处理示例消息的业务逻辑
processDemoMessage(routingKey, payload);
}
/**
* 监听普通消息队列消息
*
* @param message 消息
*/
@RabbitListener(queues = RabbitListenerConfiguration.MESSAGE_QUEUE_NAME)
public void receiveMessage(Message message) {
String routingKey = message.getMessageProperties().getReceivedRoutingKey();
String payload = new String(message.getBody());
log.info("收到普通消息: routingKey={}, payload={}", routingKey, payload);
// 处理普通消息的业务逻辑
processMessage(routingKey, payload);
}
/**
* 处理示例消息
*
* @param routingKey 路由键
* @param payload 消息载荷
*/
private void processDemoMessage(String routingKey, String payload) {
try {
// 这里可以添加具体的业务处理逻辑
log.debug("处理示例消息: routingKey={}, payload={}", routingKey, payload);
// 示例: 如果是特定的路由键,执行特定操作
if (routingKey.startsWith("demo.test")) {
log.info("执行测试操作: {}", payload);
} else if (routingKey.startsWith("demo.data")) {
log.info("处理数据: {}", payload);
}
} catch (Exception e) {
log.error("处理示例消息失败: routingKey={}, payload={}", routingKey, payload, e);
}
}
/**
* 处理普通消息
*
* @param routingKey 路由键
* @param payload 消息载荷
*/
private void processMessage(String routingKey, String payload) {
try {
// 这里可以添加具体的业务处理逻辑
log.debug("处理普通消息: routingKey={}, payload={}", routingKey, payload);
// 示例: 根据路由键进行不同的处理
if (routingKey.startsWith("message.notify")) {
log.info("处理通知消息: {}", payload);
} else if (routingKey.startsWith("message.event")) {
log.info("处理事件消息: {}", payload);
}
} catch (Exception e) {
log.error("处理普通消息失败: routingKey={}, payload={}", routingKey, payload, e);
}
}
}
@@ -0,0 +1,22 @@
server:
port: 8302
spring:
# RabbitMQ 配置
rabbitmq:
host: localhost
port: 5672
username: root
password: root
virtual-host: /
connection-timeout: 15000
# 消费者配置
listener:
simple:
acknowledge-mode: auto
retry:
enabled: true
initial-interval: 1000
max-attempts: 3
max-interval: 10000
multiplier: 1.0
@@ -0,0 +1,27 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springblade</groupId>
<artifactId>blade-example</artifactId>
<version>${revision}</version>
</parent>
<artifactId>blade-rabbit-publisher</artifactId>
<name>${project.artifactId}</name>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-tool</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,49 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.rabbit.publisher;
import org.springblade.core.launch.BladeApplication;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import static org.springblade.common.constant.LauncherConstant.APPLICATION_RABBIT_PUBLISHER_NAME;
/**
* Rabbit 生产者启动器
*
* @author Chill
*/
@EnableRabbit
@EnableScheduling
@SpringBootApplication
public class RabbitPublisherApplication {
public static void main(String[] args) {
BladeApplication.run(APPLICATION_RABBIT_PUBLISHER_NAME, RabbitPublisherApplication.class, args);
}
}
@@ -0,0 +1,50 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.rabbit.publisher.config;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Rabbit 生产者配置
*
* @author Chill
*/
@Configuration
public class RabbitPublisherConfiguration {
/**
* 交换机名
*/
public static final String EXCHANGE_NAME = "blade.amqp.exchange";
@Bean
public TopicExchange exchange() {
return new TopicExchange(EXCHANGE_NAME);
}
}
@@ -0,0 +1,90 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.rabbit.publisher.service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.rabbit.publisher.config.RabbitPublisherConfiguration;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
/**
* Rabbit 消息生产者服务
*
* @author Chill
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class RabbitMessageService {
private final RabbitTemplate rabbitTemplate;
/**
* 发送示例消息
*
* @param routingKey 路由键
* @param message 消息内容
*/
public void sendDemoMessage(String routingKey, Object message) {
try {
rabbitTemplate.convertAndSend(RabbitPublisherConfiguration.EXCHANGE_NAME, routingKey, message);
log.info("发送示例消息成功: routingKey={}, message={}", routingKey, message);
} catch (Exception e) {
log.error("发送示例消息失败: routingKey={}, message={}", routingKey, message, e);
}
}
/**
* 发送普通消息
*
* @param routingKey 路由键
* @param message 消息内容
*/
public void sendMessage(String routingKey, Object message) {
try {
rabbitTemplate.convertAndSend(RabbitPublisherConfiguration.EXCHANGE_NAME, routingKey, message);
log.info("发送消息成功: routingKey={}, message={}", routingKey, message);
} catch (Exception e) {
log.error("发送消息失败: routingKey={}, message={}", routingKey, message, e);
}
}
/**
* 发送字节数组消息
*
* @param routingKey 路由键
* @param payload 消息载荷
*/
public void sendPayload(String routingKey, byte[] payload) {
try {
rabbitTemplate.convertAndSend(RabbitPublisherConfiguration.EXCHANGE_NAME, routingKey, payload);
log.info("发送载荷消息成功: routingKey={}, payloadSize={}", routingKey, payload.length);
} catch (Exception e) {
log.error("发送载荷消息失败: routingKey={}, payloadSize={}", routingKey, payload.length, e);
}
}
}
@@ -0,0 +1,192 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.rabbit.publisher.simulator;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.rabbit.publisher.service.RabbitMessageService;
import org.springblade.rabbit.publisher.support.MessageData;
import org.springblade.core.tool.jackson.JsonUtil;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* Rabbit 消息模拟器(模拟设备消息的转发)
*
* @author Chill
*/
@Slf4j
@Component
@RequiredArgsConstructor
public class MessageSimulator implements CommandLineRunner {
private final RabbitMessageService rabbitMessageService;
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(4);
@Override
public void run(String... args) {
log.info("启动 Rabbit 消息生产者模拟器");
// 启动定时任务
startScheduledTasks();
}
/**
* 启动定时任务
*/
private void startScheduledTasks() {
// 定期发送示例消息(每30秒)
scheduler.scheduleAtFixedRate(this::sendDemoMessage, 10, 30, TimeUnit.SECONDS);
// 定期发送数据消息(每60秒)
scheduler.scheduleAtFixedRate(this::sendDataMessage, 15, 60, TimeUnit.SECONDS);
// 定期发送通知消息(每120秒)
scheduler.scheduleAtFixedRate(this::sendNotifyMessage, 20, 120, TimeUnit.SECONDS);
// 定期发送事件消息(每180秒)
scheduler.scheduleAtFixedRate(this::sendEventMessage, 25, 180, TimeUnit.SECONDS);
log.info("已启动消息生产者定时任务");
}
/**
* 发送示例消息
*/
private void sendDemoMessage() {
try {
MessageData messageData = new MessageData();
messageData.setId(UUID.randomUUID().toString().replace("-", ""));
messageData.setTimestamp(System.currentTimeMillis());
messageData.setType("demo");
Map<String, Object> data = new HashMap<>();
data.put("temperature", 20 + Math.random() * 10);
data.put("humidity", 40 + Math.random() * 30);
data.put("status", Math.random() > 0.5 ? "online" : "offline");
messageData.setData(data);
String routingKey = "demo.test." + System.currentTimeMillis();
rabbitMessageService.sendDemoMessage(routingKey, JsonUtil.toJson(messageData));
log.debug("已发送示例消息: routingKey={}", routingKey);
} catch (Exception e) {
log.error("发送示例消息失败", e);
}
}
/**
* 发送数据消息
*/
private void sendDataMessage() {
try {
MessageData messageData = new MessageData();
messageData.setId(UUID.randomUUID().toString().replace("-", ""));
messageData.setTimestamp(System.currentTimeMillis());
messageData.setType("data");
Map<String, Object> data = new HashMap<>();
data.put("deviceId", "device_" + (int)(Math.random() * 100));
data.put("value", Math.random() * 100);
data.put("unit", "celsius");
messageData.setData(data);
String routingKey = "demo.data." + System.currentTimeMillis();
rabbitMessageService.sendDemoMessage(routingKey, JsonUtil.toJson(messageData));
log.debug("已发送数据消息: routingKey={}", routingKey);
} catch (Exception e) {
log.error("发送数据消息失败", e);
}
}
/**
* 发送通知消息
*/
private void sendNotifyMessage() {
try {
MessageData messageData = new MessageData();
messageData.setId(UUID.randomUUID().toString().replace("-", ""));
messageData.setTimestamp(System.currentTimeMillis());
messageData.setType("notify");
Map<String, Object> data = new HashMap<>();
data.put("title", "系统通知");
data.put("content", "这是一条通知消息");
data.put("level", Math.random() > 0.5 ? "info" : "warning");
messageData.setData(data);
String routingKey = "message.notify." + System.currentTimeMillis();
rabbitMessageService.sendMessage(routingKey, JsonUtil.toJson(messageData));
log.debug("已发送通知消息: routingKey={}", routingKey);
} catch (Exception e) {
log.error("发送通知消息失败", e);
}
}
/**
* 发送事件消息
*/
private void sendEventMessage() {
try {
MessageData messageData = new MessageData();
messageData.setId(UUID.randomUUID().toString().replace("-", ""));
messageData.setTimestamp(System.currentTimeMillis());
messageData.setType("event");
Map<String, Object> data = new HashMap<>();
data.put("eventType", "user_action");
data.put("action", Math.random() > 0.5 ? "login" : "logout");
data.put("userId", "user_" + (int)(Math.random() * 1000));
messageData.setData(data);
String routingKey = "message.event." + System.currentTimeMillis();
rabbitMessageService.sendMessage(routingKey, JsonUtil.toJson(messageData));
log.debug("已发送事件消息: routingKey={}", routingKey);
} catch (Exception e) {
log.error("发送事件消息失败", e);
}
}
/**
* 停止模拟器
*/
public void stop() {
if (!scheduler.isShutdown()) {
scheduler.shutdown();
log.info("消息生产者模拟器已停止");
}
}
}
@@ -0,0 +1,69 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.rabbit.publisher.support;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
/**
* Rabbit 消息数据
*
* @author Chill
*/
@Data
public class MessageData implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
/**
* 消息ID
*/
private String id;
/**
* 消息类型
*/
private String type;
/**
* 时间戳
*/
private Long timestamp;
/**
* 消息数据
*/
private Object data;
/**
* 消息版本
*/
private String version = "1.0";
}
@@ -0,0 +1,15 @@
server:
port: 8301
spring:
# RabbitMQ 配置
rabbitmq:
host: localhost
port: 5672
username: root
password: root
virtual-host: /
connection-timeout: 15000
# 生产者配置
publisher-confirm-type: correlated
publisher-returns: true
+30
View File
@@ -0,0 +1,30 @@
-- 创建 order库、业务表、undo_log表
create database seata_order;
use seata_order;
DROP TABLE IF EXISTS `tb_order`;
CREATE TABLE `tb_order` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` varchar(255) DEFAULT NULL,
`commodity_code` varchar(255) DEFAULT NULL,
`count` int(11) DEFAULT 0,
`money` int(11) DEFAULT 0,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `undo_log`
(
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`branch_id` BIGINT(20) NOT NULL,
`xid` VARCHAR(100) NOT NULL,
`context` VARCHAR(128) NOT NULL,
`rollback_info` LONGBLOB NOT NULL,
`log_status` INT(11) NOT NULL,
`log_created` DATETIME NOT NULL,
`log_modified` DATETIME NOT NULL,
`ext` VARCHAR(100) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
) ENGINE = InnoDB
AUTO_INCREMENT = 1
DEFAULT CHARSET = utf8;
+27
View File
@@ -0,0 +1,27 @@
<?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>blade-example</artifactId>
<groupId>org.springblade</groupId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>blade-seata-order</artifactId>
<name>${project.artifactId}</name>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-boot</artifactId>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-starter-transaction</artifactId>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,37 @@
/**
* Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springblade.seata.order;
import org.springblade.common.constant.LauncherConstant;
import org.springblade.core.cloud.feign.EnableBladeFeign;
import org.springblade.core.launch.BladeApplication;
import org.springblade.core.transaction.annotation.SeataCloudApplication;
/**
* Order启动器
*
* @author Chill
*/
@EnableBladeFeign
@SeataCloudApplication
public class SeataOrderApplication {
public static void main(String[] args) {
BladeApplication.run(LauncherConstant.APPLICATION_SEATA_ORDER_NAME, SeataOrderApplication.class, args);
}
}
@@ -0,0 +1,34 @@
package org.springblade.seata.order.controller;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.api.R;
import org.springblade.seata.order.service.IOrderService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* OrderController
*
* @author Chill
*/
@RestController
@RequestMapping("order")
@AllArgsConstructor
public class OrderController {
private final IOrderService orderService;
/**
* 创建订单
*
* @param userId 用户id
* @param commodityCode 商品代码
* @param count 数量
* @return boolean
*/
@RequestMapping("/create")
public R createOrder(String userId, String commodityCode, Integer count) {
return R.status(orderService.createOrder(userId, commodityCode, count));
}
}
@@ -0,0 +1,33 @@
package org.springblade.seata.order.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serial;
import java.io.Serializable;
import java.math.BigDecimal;
/**
* Order
*
* @author Chill
*/
@Data
@Accessors(chain = true)
@TableName("tb_order")
public class Order implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private String userId;
private String commodityCode;
private Integer count;
private BigDecimal money;
}
@@ -0,0 +1,25 @@
package org.springblade.seata.order.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* StorageClient
*
* @author Chill
*/
@FeignClient(name = "blade-seata-storage", fallback = StorageClientFallback.class)
public interface IStorageClient {
/**
* 减库存
*
* @param commodityCode 商品代码
* @param count 数量
* @return boolean
*/
@GetMapping("/storage/deduct")
int deduct(@RequestParam("commodityCode") String commodityCode, @RequestParam("count") Integer count);
}
@@ -0,0 +1,18 @@
package org.springblade.seata.order.feign;
import org.springframework.stereotype.Component;
/**
* StorageClientFallback
*
* @author Chill
*/
@Component
public class StorageClientFallback implements IStorageClient {
@Override
public int deduct(String commodityCode, Integer count) {
return -1;
}
}
@@ -0,0 +1,12 @@
package org.springblade.seata.order.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springblade.seata.order.entity.Order;
/**
* OrderMapper
*
* @author Chill
*/
public interface OrderMapper extends BaseMapper<Order> {
}
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.springblade.seata.order.mapper.OrderMapper">
</mapper>
@@ -0,0 +1,23 @@
package org.springblade.seata.order.service;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springblade.seata.order.entity.Order;
/**
* IOrderService
*
* @author Chill
*/
public interface IOrderService extends IService<Order> {
/**
* 创建订单
*
* @param userId 用户id
* @param commodityCode 商品代码
* @param count 数量
* @return boolean
*/
boolean createOrder(String userId, String commodityCode, Integer count);
}
@@ -0,0 +1,48 @@
package org.springblade.seata.order.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.AllArgsConstructor;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.seata.order.entity.Order;
import org.springblade.seata.order.feign.IStorageClient;
import org.springblade.seata.order.mapper.OrderMapper;
import org.springblade.seata.order.service.IOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
/**
* OrderServiceImpl
*
* @author Chill
*/
@Service
@AllArgsConstructor
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {
private final IStorageClient storageClient;
@Override
@GlobalTransactional
@Transactional(rollbackFor = Exception.class)
public boolean createOrder(String userId, String commodityCode, Integer count) {
int maxCount = 100;
BigDecimal orderMoney = new BigDecimal(count).multiply(new BigDecimal(5));
Order order = new Order()
.setUserId(userId)
.setCommodityCode(commodityCode)
.setCount(count)
.setMoney(orderMoney);
int cnt1 = baseMapper.insert(order);
int cnt2 = storageClient.deduct(commodityCode, count);
if (cnt2 < 0) {
throw new ServiceException("创建订单失败");
} else if (count > maxCount) {
throw new ServiceException("超过订单最大值,创建订单失败");
}
return cnt1 > 0 && cnt2 > 0;
}
}
@@ -0,0 +1,15 @@
#服务器端口
server:
port: 8501
#数据源配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/seata_order?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true
username: root
password: root
#常规配置
blade:
mybatis-plus:
tenant-mode: false
@@ -0,0 +1,33 @@
-- 创建 storage库、业务表、undo_log表
create database seata_storage;
use seata_storage;
DROP TABLE IF EXISTS `tb_storage`;
CREATE TABLE `tb_storage` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`commodity_code` varchar(255) DEFAULT NULL,
`count` int(11) DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE KEY (`commodity_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `undo_log`
(
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`branch_id` BIGINT(20) NOT NULL,
`xid` VARCHAR(100) NOT NULL,
`context` VARCHAR(128) NOT NULL,
`rollback_info` LONGBLOB NOT NULL,
`log_status` INT(11) NOT NULL,
`log_created` DATETIME NOT NULL,
`log_modified` DATETIME NOT NULL,
`ext` VARCHAR(100) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
) ENGINE = InnoDB
AUTO_INCREMENT = 1
DEFAULT CHARSET = utf8;
-- 初始化库存模拟数据
INSERT INTO seata_storage.tb_storage (id, commodity_code, count) VALUES (1, 'product-1', 9999999);
INSERT INTO seata_storage.tb_storage (id, commodity_code, count) VALUES (2, 'product-2', 0);
+28
View File
@@ -0,0 +1,28 @@
<?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>blade-example</artifactId>
<groupId>org.springblade</groupId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>blade-seata-storage</artifactId>
<name>${project.artifactId}</name>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-boot</artifactId>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-starter-transaction</artifactId>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,37 @@
/**
* Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springblade.seata.storage;
import org.springblade.common.constant.LauncherConstant;
import org.springblade.core.cloud.feign.EnableBladeFeign;
import org.springblade.core.launch.BladeApplication;
import org.springblade.core.transaction.annotation.SeataCloudApplication;
/**
* Storage启动器
*
* @author Chill
*/
@EnableBladeFeign
@SeataCloudApplication
public class SeataStorageApplication {
public static void main(String[] args) {
BladeApplication.run(LauncherConstant.APPLICATION_SEATA_STORAGE_NAME, SeataStorageApplication.class, args);
}
}
@@ -0,0 +1,31 @@
package org.springblade.seata.storage.controller;
import lombok.AllArgsConstructor;
import org.springblade.seata.storage.service.IStorageService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* StorageController
*
* @author Chill
*/
@RestController
@AllArgsConstructor
@RequestMapping("storage")
public class StorageController {
private final IStorageService storageService;
/**
* 减库存
*
* @param commodityCode 商品代码
* @param count 数量
*/
@RequestMapping(path = "/deduct")
public int deduct(String commodityCode, Integer count) {
return storageService.deduct(commodityCode, count);
}
}
@@ -0,0 +1,26 @@
package org.springblade.seata.storage.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
/**
* storage
*
* @author Chill
*/
@Data
@TableName("tb_storage")
public class Storage implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
private Long id;
private String commodityCode;
private Long count;
}
@@ -0,0 +1,12 @@
package org.springblade.seata.storage.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springblade.seata.storage.entity.Storage;
/**
* StorageMapper
*
* @author Chill
*/
public interface StorageMapper extends BaseMapper<Storage> {
}
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.springblade.seata.storage.mapper.StorageMapper">
</mapper>
@@ -0,0 +1,22 @@
package org.springblade.seata.storage.service;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springblade.seata.storage.entity.Storage;
/**
* IStorageService
*
* @author Chill
*/
public interface IStorageService extends IService<Storage> {
/**
* 减库存
*
* @param commodityCode 商品代码
* @param count 数量
* @return boolean
*/
int deduct(String commodityCode, int count);
}
@@ -0,0 +1,30 @@
package org.springblade.seata.storage.service.impl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.seata.storage.entity.Storage;
import org.springblade.seata.storage.mapper.StorageMapper;
import org.springblade.seata.storage.service.IStorageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* StorageServiceImpl
*
* @author Chill
*/
@Service
public class StorageServiceImpl extends ServiceImpl<StorageMapper, Storage> implements IStorageService {
@Override
@Transactional(rollbackFor = Exception.class)
public int deduct(String commodityCode, int count) {
Storage storage = baseMapper.selectOne(Wrappers.<Storage>query().lambda().eq(Storage::getCommodityCode, commodityCode));
if (storage.getCount() < count) {
throw new RuntimeException("超过库存数,扣除失败!");
}
storage.setCount(storage.getCount() - count);
return baseMapper.updateById(storage);
}
}
@@ -0,0 +1,15 @@
#服务器端口
server:
port: 8502
#数据源配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/seata_storage?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true
username: root
password: root
#常规配置
blade:
mybatis-plus:
tenant-mode: false
+41
View File
@@ -0,0 +1,41 @@
<?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>blade-example</artifactId>
<groupId>org.springblade</groupId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>blade-sharding-jdbc</artifactId>
<name>${project.artifactId}</name>
<packaging>jar</packaging>
<dependencies>
<!-- bladex -->
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-boot</artifactId>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-starter-swagger</artifactId>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-starter-tenant</artifactId>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-starter-sharding</artifactId>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-auto</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,46 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.sharding;
import org.springblade.core.cloud.client.BladeCloudApplication;
import org.springblade.core.launch.BladeApplication;
import static org.springblade.common.constant.LauncherConstant.APPLICATION_SHARDING_NAME;
/**
* 启动器
*
* @author Chill
*/
@BladeCloudApplication
public class ShardingApplication {
public static void main(String[] args) {
BladeApplication.run(APPLICATION_SHARDING_NAME, ShardingApplication.class, args);
}
}
@@ -0,0 +1,150 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.sharding.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.sharding.annotation.ShardingDS;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.sharding.entity.Notice;
import org.springblade.sharding.service.INoticeService;
import org.springblade.sharding.vo.NoticeVO;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
/**
* 控制器
*
* @author Chill
*/
@RestController
@RequestMapping("notice")
@AllArgsConstructor
@Tag(name = "通知公告", description = "通知公告接口")
public class NoticeController extends BladeController {
private final INoticeService noticeService;
/**
* 新增或修改(使用sharding)
*/
@ShardingDS
@PostMapping("/submit")
@ApiOperationSupport(order = 1)
@Operation(summary = "新增或修改", description = "传入notice")
public R submit(@RequestBody Notice notice) {
return R.status(noticeService.saveOrUpdate(notice));
}
/**
* 分页(使用sharding)
*/
@ShardingDS
@GetMapping("/list")
@Parameters({
@Parameter(name = "category", description = "公告类型", in = ParameterIn.QUERY, schema = @Schema(type = "integer")),
@Parameter(name = "title", description = "公告标题", in = ParameterIn.QUERY, schema = @Schema(type = "string"))
})
@ApiOperationSupport(order = 2)
@Operation(summary = "分页", description = "传入notice")
public R<IPage<Notice>> list(@Parameter(hidden = true) @RequestParam Map<String, Object> notice, Query query) {
IPage<Notice> pages = noticeService.page(Condition.getPage(query), Condition.getQueryWrapper(notice, Notice.class));
return R.data(pages);
}
/**
* 多表联合查询自定义分页
*/
@GetMapping("/page")
@Parameters({
@Parameter(name = "category", description = "公告类型", in = ParameterIn.QUERY, schema = @Schema(type = "integer")),
@Parameter(name = "title", description = "公告标题", in = ParameterIn.QUERY, schema = @Schema(type = "string"))
})
@ApiOperationSupport(order = 3)
@Operation(summary = "分页", description = "传入notice")
public R<IPage<NoticeVO>> page(@Parameter(hidden = true) NoticeVO notice, Query query) {
IPage<NoticeVO> pages = noticeService.selectNoticePage(Condition.getPage(query), notice);
return R.data(pages);
}
/**
* 新增
*/
@PostMapping("/save")
@ApiOperationSupport(order = 4)
@Operation(summary = "新增", description = "传入notice")
public R save(@RequestBody Notice notice) {
return R.status(noticeService.save(notice));
}
/**
* 修改
*/
@PostMapping("/update")
@ApiOperationSupport(order = 5)
@Operation(summary = "修改", description = "传入notice")
public R update(@RequestBody Notice notice) {
return R.status(noticeService.updateById(notice));
}
/**
* 详情
*/
@GetMapping("/detail")
@ApiOperationSupport(order = 6)
@Operation(summary = "详情", description = "传入notice")
public R<Notice> detail(Notice notice) {
Notice detail = noticeService.getOne(Condition.getQueryWrapper(notice));
return R.data(detail);
}
/**
* 删除
*/
@ShardingDS
@PostMapping("/remove")
@ApiOperationSupport(order = 7)
@Operation(summary = "逻辑删除", description = "传入notice")
public R remove(@Parameter(name = "主键集合") @RequestParam String ids) {
boolean temp = noticeService.deleteLogic(Func.toLongList(ids));
return R.status(temp);
}
}
@@ -0,0 +1,81 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.sharding.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springblade.core.mp.base.BaseEntity;
import java.io.Serial;
import java.util.Date;
/**
* 实体类
*
* @author Chill
*/
@Data
@TableName("blade_notice")
@EqualsAndHashCode(callSuper = true)
public class Notice extends BaseEntity {
@Serial
private static final long serialVersionUID = 1L;
/**
* 租户ID
*/
@Schema(description = "租户ID")
private String tenantId;
/**
* 标题
*/
@Schema(description = "标题")
private String title;
/**
* 通知类型
*/
@Schema(description = "通知类型")
private Integer category;
/**
* 发布日期
*/
@Schema(description = "发布日期")
private Date releaseTime;
/**
* 内容
*/
@Schema(description = "内容")
private String content;
}
@@ -0,0 +1,51 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.sharding.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.sharding.entity.Notice;
import org.springblade.sharding.vo.NoticeVO;
import java.util.List;
/**
* Mapper 接口
*
* @author Chill
*/
public interface NoticeMapper extends BaseMapper<Notice> {
/**
* 自定义分页
*
* @param page 分页
* @param notice 实体
* @return List<NoticeVO>
*/
List<NoticeVO> selectNoticePage(IPage page, NoticeVO notice);
}
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.springblade.sharding.mapper.NoticeMapper">
<!-- 通用查询映射结果 -->
<resultMap id="noticeResultMap" type="org.springblade.sharding.entity.Notice">
<result column="id" property="id"/>
<result column="create_user" property="createUser"/>
<result column="create_time" property="createTime"/>
<result column="update_user" property="updateUser"/>
<result column="update_time" property="updateTime"/>
<result column="status" property="status"/>
<result column="is_deleted" property="isDeleted"/>
<result column="release_time" property="releaseTime"/>
<result column="title" property="title"/>
<result column="content" property="content"/>
</resultMap>
<!-- 通用查询映射结果 -->
<resultMap id="noticeVOResultMap" type="org.springblade.sharding.vo.NoticeVO">
<result column="id" property="id"/>
<result column="create_user" property="createUser"/>
<result column="create_time" property="createTime"/>
<result column="update_user" property="updateUser"/>
<result column="update_time" property="updateTime"/>
<result column="status" property="status"/>
<result column="is_deleted" property="isDeleted"/>
<result column="release_time" property="releaseTime"/>
<result column="title" property="title"/>
<result column="content" property="content"/>
</resultMap>
<select id="selectNoticePage" resultMap="noticeVOResultMap">
SELECT
n.*,
d.dict_value AS categoryName
FROM
blade_notice n
LEFT JOIN ( SELECT * FROM blade_dict WHERE CODE = 'notice' ) d ON n.category = d.dict_key
WHERE
n.is_deleted = 0 and n.tenant_id = #{notice.tenantId}
<if test="notice.title!=null">
and n.title like concat(concat('%', #{notice.title}), '%')
</if>
<if test="notice.category!=null">
and n.category = #{notice.category}
</if>
</select>
</mapper>
@@ -0,0 +1,49 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.sharding.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseService;
import org.springblade.sharding.entity.Notice;
import org.springblade.sharding.vo.NoticeVO;
/**
* 服务类
*
* @author Chill
*/
public interface INoticeService extends BaseService<Notice> {
/**
* 自定义分页
* @param page
* @param notice
* @return
*/
IPage<NoticeVO> selectNoticePage(IPage<NoticeVO> page, NoticeVO notice);
}
@@ -0,0 +1,52 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.sharding.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.sharding.entity.Notice;
import org.springblade.sharding.mapper.NoticeMapper;
import org.springblade.sharding.service.INoticeService;
import org.springblade.sharding.vo.NoticeVO;
import org.springframework.stereotype.Service;
/**
* 服务实现类
*
* @author Chill
*/
@Service
public class NoticeServiceImpl extends BaseServiceImpl<NoticeMapper, Notice> implements INoticeService {
@Override
public IPage<NoticeVO> selectNoticePage(IPage<NoticeVO> page, NoticeVO notice) {
// 若不使用mybatis-plus自带的分页方法,则不会自动带入tenantId,所以我们需要自行注入
notice.setTenantId(AuthUtil.getTenantId());
return page.setRecords(baseMapper.selectNoticePage(page, notice));
}
}
@@ -0,0 +1,23 @@
package org.springblade.sharding.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springblade.sharding.entity.Notice;
/**
* 通知公告视图类
*
* @author Chill
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class NoticeVO extends Notice {
@Schema(description = "通知类型名")
private String categoryName;
@Schema(description = "租户编号")
private String tenantId;
}
@@ -0,0 +1,61 @@
#不分库仅分表
spring:
#主数据源
datasource:
# MySql
url: jdbc:mysql://localhost:3306/bladex?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true
username: root
password: root
#sharding数据源
shardingsphere:
datasource:
#定义的数据库集合
names: ds1
#数据库连接别名
ds1:
#数据连接池类型
type: com.alibaba.druid.pool.DruidDataSource
#驱动类型
driver-class-name: com.mysql.cj.jdbc.Driver
#jdbc地址
url: jdbc:mysql://localhost:3306/bladex?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true
#数据库账号
username: root
#数据库密码
password: root
rules:
sharding:
#主键生成类型
key-generators:
snowflake:
type: SNOWFLAKE
tables:
#分表代号
blade_notice:
#实际节点名称,格式为 库名$->{0..n1}.表名$->{0..n2}
#blade_notice_$->{1..2} 代表有 blade_notice_1和blade_notice_2两个表
actual-data-nodes: ds1.blade_notice_$->{1..2}
#分表策略
table-strategy:
standard:
#分表列名
sharding-column: id
#分表算法名,不可用下划线
sharding-algorithm-name: blade-notice-inline
#主键生成策略
key-generate-strategy:
#主键名
column: id
#策略算法
key-generator-name: snowflake
#分库分表算法
sharding-algorithms:
#分表算法名
blade-notice-inline:
#算法类型
type: INLINE
props:
#算法表达式
#表达式写法非常自由,以下配置仅作为参考
#blade_notice_$->{id % 2 + 1} 代表对id进行取余,根据id的基偶来指向blade_notice_1和blade_notice_2
algorithm-expression: blade_notice_$->{id % 2 + 1}
@@ -0,0 +1,71 @@
#不分表仅分库
spring:
#主数据源
datasource:
# MySql
url: jdbc:mysql://localhost:3306/bladex?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true
username: root
password: root
#sharding数据源
shardingsphere:
datasource:
names: ds1,ds2
#数据库连接别名
ds1:
#数据连接池类型
type: com.alibaba.druid.pool.DruidDataSource
#驱动类型
driver-class-name: com.mysql.cj.jdbc.Driver
#jdbc地址
url: jdbc:mysql://localhost:3306/bladex1?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true
#数据库账号
username: root
#数据库密码
password: root
#数据库连接别名
ds2:
#数据连接池类型
type: com.alibaba.druid.pool.DruidDataSource
#驱动类型
driver-class-name: com.mysql.cj.jdbc.Driver
#jdbc地址
jdbc-url: jdbc:mysql://localhost:3306/bladex2?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true
#数据库账号
username: root
#数据库密码
password: root
rules:
sharding:
#主键生成类型
key-generators:
snowflake:
type: SNOWFLAKE
#默认分库策略
default-database-strategy:
standard:
#列名
sharding-column: id
#算法名
sharding-algorithm-name: database-inline
tables:
#表名代号
blade_notice:
#实际节点名称,格式为 库名$->{0..n1}.表名
actual-data-nodes: ds$->{1..2}.blade_notice
#主键生成策略
key-generate-strategy:
#主键名
column: id
#策略算法
key-generator-name: snowflake
#分库分表算法
sharding-algorithms:
#分库算法名
database-inline:
#算法类型
type: INLINE
props:
#算法表达式
#表达式写法非常自由,以下配置仅作为参考
#ds$->{id % 2 + 1} 代表对id进行取余,根据id的基偶来指向 ds1和ds2
algorithm-expression: ds$->{id % 2 + 1}
@@ -0,0 +1,88 @@
#分库+分表
spring:
#主数据源
datasource:
# MySql
url: jdbc:mysql://localhost:3306/bladex?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true
username: root
password: root
#sharding数据源
shardingsphere:
datasource:
names: ds1,ds2
#数据库连接别名
ds1:
#数据连接池类型
type: com.alibaba.druid.pool.DruidDataSource
#驱动类型
driver-class-name: com.mysql.cj.jdbc.Driver
#jdbc地址
url: jdbc:mysql://localhost:3306/bladex1?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true
#数据库账号
username: root
#数据库密码
password: root
#数据库连接别名
ds2:
#数据连接池类型
type: com.alibaba.druid.pool.DruidDataSource
#驱动类型
driver-class-name: com.mysql.cj.jdbc.Driver
#jdbc地址
jdbc-url: jdbc:mysql://localhost:3306/bladex2?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true
#数据库账号
username: root
#数据库密码
password: root
rules:
sharding:
#主键生成类型
key-generators:
snowflake:
type: SNOWFLAKE
#默认分库策略
default-database-strategy:
standard:
#列名
sharding-column: id
#算法名
sharding-algorithm-name: database-inline
tables:
#分表代号
blade_notice:
#实际节点名称,格式为 库名$->{0..n1}.表名$->{0..n2}
#ds$->{1..2}.blade_notice_$->{1..2} 代表有 ds1和ds2两个库以及blade_notice_1和blade_notice_2两个表
actual-data-nodes: ds$->{1..2}.blade_notice_$->{1..2}
#分表策略
table-strategy:
standard:
#分表列名
sharding-column: id
#分表算法名,不可用下划线
sharding-algorithm-name: blade-notice-inline
#主键生成策略
key-generate-strategy:
#主键名
column: id
#策略算法
key-generator-name: snowflake
#分库分表算法
sharding-algorithms:
#分库算法名
database-inline:
#算法类型
type: INLINE
props:
#算法表达式
#表达式写法非常自由,以下配置仅作为参考
#ds$->{id % 2 + 1} 代表对id进行取余,根据id的基偶来指向 ds1和ds2
algorithm-expression: ds$->{id % 2 + 1}
#分表算法名
blade-notice-inline:
#算法类型
type: INLINE
props:
#算法表达式
#表达式写法非常自由,以下配置仅作为参考
#blade_notice_$->{id % 2 + 1} 代表对id进行取余,根据id的基偶来指向blade_notice_1和blade_notice_2
algorithm-expression: blade_notice_$->{id % 2 + 1}
@@ -0,0 +1,18 @@
## server
server:
port: 8701
spring:
#sharding通用配置
shardingsphere:
mode:
#内存模式
type: Standalone
props:
#是否展示sql
sql-show: true
blade:
#开启分库分表自动配置
#nacos会覆盖此配置,需要在nacos也配置为true
sharding:
enabled: true
+13
View File
@@ -0,0 +1,13 @@
FROM bladex/alpine-java:openjdk17_cn_slim
LABEL maintainer="bladejava@qq.com"
RUN mkdir -p /blade/xxljob
WORKDIR /blade/xxljob
EXPOSE 7008
COPY ./target/blade-xxljob.jar ./app.jar
ENTRYPOINT ["java", "--add-opens", "java.base/java.lang=ALL-UNNAMED", "--add-opens", "java.base/java.lang.reflect=ALL-UNNAMED", "-Djava.security.egd=file:/dev/./urandom", "-jar", "app.jar"]
+22
View File
@@ -0,0 +1,22 @@
## 分布式任务调度服务启动
### XXL-JOB
1. 执行/doc/sql文件夹下的sql脚本
2. 拉取docker镜像
```shell
docker pull xuxueli/xxl-job-admin:2.4.0
```
3. 执行docker命令运行服务
```shell
docker run -d --add-host="host.docker.internal:host-gateway" \
-p 8080:8080 \
--restart=always \
--name xxl-job \
-e TZ="Asia/Shanghai" \
-e JAVA_OPTS="-Xms512M -Xmx512m" \
-e PARAMS="--spring.datasource.url=jdbc:mysql://host.docker.internal:3306/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai --spring.datasource.username=root --spring.datasource.password=root" \
-v ~/docker/xxl-job:/data/applogs \
xuxueli/xxl-job-admin:2.4.0
```
@@ -0,0 +1,123 @@
-- --------------------------------------
-- XXL-JOB v2.4.0
-- Copyright (c) 2015-present, xuxueli.
-- --------------------------------------
CREATE database if NOT EXISTS `xxl_job` default character set utf8mb4 collate utf8mb4_unicode_ci;
use `xxl_job`;
SET NAMES utf8mb4;
CREATE TABLE `xxl_job_info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`job_group` int(11) NOT NULL COMMENT '执行器主键ID',
`job_desc` varchar(255) NOT NULL,
`add_time` datetime DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
`author` varchar(64) DEFAULT NULL COMMENT '作者',
`alarm_email` varchar(255) DEFAULT NULL COMMENT '报警邮件',
`schedule_type` varchar(50) NOT NULL DEFAULT 'NONE' COMMENT '调度类型',
`schedule_conf` varchar(128) DEFAULT NULL COMMENT '调度配置,值含义取决于调度类型',
`misfire_strategy` varchar(50) NOT NULL DEFAULT 'DO_NOTHING' COMMENT '调度过期策略',
`executor_route_strategy` varchar(50) DEFAULT NULL COMMENT '执行器路由策略',
`executor_handler` varchar(255) DEFAULT NULL COMMENT '执行器任务handler',
`executor_param` varchar(512) DEFAULT NULL COMMENT '执行器任务参数',
`executor_block_strategy` varchar(50) DEFAULT NULL COMMENT '阻塞处理策略',
`executor_timeout` int(11) NOT NULL DEFAULT '0' COMMENT '任务执行超时时间,单位秒',
`executor_fail_retry_count` int(11) NOT NULL DEFAULT '0' COMMENT '失败重试次数',
`glue_type` varchar(50) NOT NULL COMMENT 'GLUE类型',
`glue_source` mediumtext COMMENT 'GLUE源代码',
`glue_remark` varchar(128) DEFAULT NULL COMMENT 'GLUE备注',
`glue_updatetime` datetime DEFAULT NULL COMMENT 'GLUE更新时间',
`child_jobid` varchar(255) DEFAULT NULL COMMENT '子任务ID,多个逗号分隔',
`trigger_status` tinyint(4) NOT NULL DEFAULT '0' COMMENT '调度状态:0-停止,1-运行',
`trigger_last_time` bigint(13) NOT NULL DEFAULT '0' COMMENT '上次调度时间',
`trigger_next_time` bigint(13) NOT NULL DEFAULT '0' COMMENT '下次调度时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `xxl_job_log` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`job_group` int(11) NOT NULL COMMENT '执行器主键ID',
`job_id` int(11) NOT NULL COMMENT '任务,主键ID',
`executor_address` varchar(255) DEFAULT NULL COMMENT '执行器地址,本次执行的地址',
`executor_handler` varchar(255) DEFAULT NULL COMMENT '执行器任务handler',
`executor_param` varchar(512) DEFAULT NULL COMMENT '执行器任务参数',
`executor_sharding_param` varchar(20) DEFAULT NULL COMMENT '执行器任务分片参数,格式如 1/2',
`executor_fail_retry_count` int(11) NOT NULL DEFAULT '0' COMMENT '失败重试次数',
`trigger_time` datetime DEFAULT NULL COMMENT '调度-时间',
`trigger_code` int(11) NOT NULL COMMENT '调度-结果',
`trigger_msg` text COMMENT '调度-日志',
`handle_time` datetime DEFAULT NULL COMMENT '执行-时间',
`handle_code` int(11) NOT NULL COMMENT '执行-状态',
`handle_msg` text COMMENT '执行-日志',
`alarm_status` tinyint(4) NOT NULL DEFAULT '0' COMMENT '告警状态:0-默认、1-无需告警、2-告警成功、3-告警失败',
PRIMARY KEY (`id`),
KEY `I_trigger_time` (`trigger_time`),
KEY `I_handle_code` (`handle_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `xxl_job_log_report` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`trigger_day` datetime DEFAULT NULL COMMENT '调度-时间',
`running_count` int(11) NOT NULL DEFAULT '0' COMMENT '运行中-日志数量',
`suc_count` int(11) NOT NULL DEFAULT '0' COMMENT '执行成功-日志数量',
`fail_count` int(11) NOT NULL DEFAULT '0' COMMENT '执行失败-日志数量',
`update_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `i_trigger_day` (`trigger_day`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `xxl_job_logglue` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`job_id` int(11) NOT NULL COMMENT '任务,主键ID',
`glue_type` varchar(50) DEFAULT NULL COMMENT 'GLUE类型',
`glue_source` mediumtext COMMENT 'GLUE源代码',
`glue_remark` varchar(128) NOT NULL COMMENT 'GLUE备注',
`add_time` datetime DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `xxl_job_registry` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`registry_group` varchar(50) NOT NULL,
`registry_key` varchar(255) NOT NULL,
`registry_value` varchar(255) NOT NULL,
`update_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `i_g_k_v` (`registry_group`,`registry_key`,`registry_value`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `xxl_job_group` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`app_name` varchar(64) NOT NULL COMMENT '执行器AppName',
`title` varchar(12) NOT NULL COMMENT '执行器名称',
`address_type` tinyint(4) NOT NULL DEFAULT '0' COMMENT '执行器地址类型:0=自动注册、1=手动录入',
`address_list` text COMMENT '执行器地址列表,多地址逗号分隔',
`update_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `xxl_job_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL COMMENT '账号',
`password` varchar(50) NOT NULL COMMENT '密码',
`role` tinyint(4) NOT NULL COMMENT '角色:0-普通用户、1-管理员',
`permission` varchar(255) DEFAULT NULL COMMENT '权限:执行器ID列表,多个逗号分割',
PRIMARY KEY (`id`),
UNIQUE KEY `i_username` (`username`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `xxl_job_lock` (
`lock_name` varchar(50) NOT NULL COMMENT '锁名称',
PRIMARY KEY (`lock_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO `xxl_job_group`(`id`, `app_name`, `title`, `address_type`, `address_list`, `update_time`) VALUES (1, 'blade-xxl-job-executor', '示例执行器', 1, 'http://host.docker.internal:9999', '2024-04-01 00:00:00' );
INSERT INTO `xxl_job_info`(`id`, `job_group`, `job_desc`, `add_time`, `update_time`, `author`, `alarm_email`, `schedule_type`, `schedule_conf`, `misfire_strategy`, `executor_route_strategy`, `executor_handler`, `executor_param`, `executor_block_strategy`, `executor_timeout`, `executor_fail_retry_count`, `glue_type`, `glue_source`, `glue_remark`, `glue_updatetime`, `child_jobid`) VALUES (1, 1, '测试任务', '2018-11-03 22:21:31', '2018-11-03 22:21:31', 'XXL', '', 'CRON', '0 0 0 * * ? *', 'DO_NOTHING', 'FIRST', 'demoJobHandler', '', 'SERIAL_EXECUTION', 0, 0, 'BEAN', '', 'GLUE代码初始化', '2018-11-03 22:21:31', '');
INSERT INTO `xxl_job_user`(`id`, `username`, `password`, `role`, `permission`) VALUES (1, 'admin', 'e10adc3949ba59abbe56e057f20f883e', 1, NULL);
INSERT INTO `xxl_job_lock` ( `lock_name`) VALUES ( 'schedule_lock');
commit;
+59
View File
@@ -0,0 +1,59 @@
<?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>blade-example</artifactId>
<groupId>org.springblade</groupId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>blade-xxl-job</artifactId>
<name>${project.artifactId}</name>
<packaging>jar</packaging>
<properties>
<xxljob.version>2.4.0</xxljob.version>
</properties>
<dependencies>
<!--Blade-->
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-biz-common</artifactId>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-cloud</artifactId>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-launch</artifactId>
</dependency>
<!--Job-->
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>${xxljob.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<configuration>
<skip>${docker.fabric.skip}</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,44 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.xxljob;
import org.springblade.common.constant.LauncherConstant;
import org.springblade.core.cloud.client.BladeCloudApplication;
import org.springblade.core.launch.BladeApplication;
/**
* 启动器
*
* @author Chill
*/
@BladeCloudApplication
public class XxlJobApplication {
public static void main(String[] args) {
BladeApplication.run(LauncherConstant.APPLICATION_XXLJOB_NAME, XxlJobApplication.class, args);
}
}
@@ -0,0 +1,77 @@
package org.springblade.xxljob.config;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* xxl-job config
*
* @author xuxueli
*/
@Slf4j
@Configuration(proxyBeanMethods = false)
public class XxlJobConfig {
@Value("${xxl.job.admin.addresses}")
private String adminAddresses;
@Value("${xxl.job.accessToken}")
private String accessToken;
@Value("${xxl.job.executor.appname}")
private String appname;
@Value("${xxl.job.executor.address}")
private String address;
@Value("${xxl.job.executor.ip}")
private String ip;
@Value("${xxl.job.executor.port}")
private int port;
@Value("${xxl.job.executor.logpath}")
private String logPath;
@Value("${xxl.job.executor.logretentiondays}")
private int logRetentionDays;
@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
log.info(">>>>>>>>>>> xxl-job config init.");
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
xxlJobSpringExecutor.setAppname(appname);
xxlJobSpringExecutor.setAddress(address);
xxlJobSpringExecutor.setIp(ip);
xxlJobSpringExecutor.setPort(port);
xxlJobSpringExecutor.setAccessToken(accessToken);
xxlJobSpringExecutor.setLogPath(logPath);
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
return xxlJobSpringExecutor;
}
/**
* 针对多网卡、容器内部署等情况,可借助 "spring-cloud-commons" 提供的 "InetUtils" 组件灵活定制注册IP;
*
* 1、引入依赖:
* <dependency>
* <groupId>org.springframework.cloud</groupId>
* <artifactId>spring-cloud-commons</artifactId>
* <version>${version}</version>
* </dependency>
*
* 2、配置文件,或者容器启动变量
* spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.'
*
* 3、获取IP
* String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
*/
}
@@ -0,0 +1,256 @@
package org.springblade.xxljob.handler;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
/**
* XxlJob开发示例(Bean模式)
* <p>
* 开发步骤:
* 1、任务开发:在Spring Bean实例中,开发Job方法;
* 2、注解配置:为Job方法添加注解 "@XxlJob(value="自定义jobhandler名称", init = "JobHandler初始化方法", destroy = "JobHandler销毁方法")",注解value值对应的是调度中心新建任务的JobHandler属性的值。
* 3、执行日志:需要通过 "XxlJobHelper.log" 打印执行日志;
* 4、任务结果:默认任务结果为 "成功" 状态,不需要主动设置;如有诉求,比如设置任务结果为失败,可以通过 "XxlJobHelper.handleFail/handleSuccess" 自主设置任务结果;
*
* @author xuxueli 2019-12-11 21:52:51
*/
@Component
public class SampleXxlJob {
private static Logger logger = LoggerFactory.getLogger(SampleXxlJob.class);
/**
* 1、简单任务示例(Bean模式)
*/
@XxlJob("demoJobHandler")
public void demoJobHandler() throws Exception {
String param = XxlJobHelper.getJobParam();
XxlJobHelper.log("XXL-JOB, Hello World." + param);
for (int i = 0; i < 5; i++) {
XxlJobHelper.log("beat at:" + i);
TimeUnit.SECONDS.sleep(2);
}
// default success
}
/**
* 2、分片广播任务
*/
@XxlJob("shardingJobHandler")
public void shardingJobHandler() throws Exception {
// 分片参数
int shardIndex = XxlJobHelper.getShardIndex();
int shardTotal = XxlJobHelper.getShardTotal();
XxlJobHelper.log("分片参数:当前分片序号 = {}, 总分片数 = {}", shardIndex, shardTotal);
// 业务逻辑
for (int i = 0; i < shardTotal; i++) {
if (i == shardIndex) {
XxlJobHelper.log("第 {} 片, 命中分片开始处理", i);
} else {
XxlJobHelper.log("第 {} 片, 忽略", i);
}
}
}
/**
* 3、命令行任务
*/
@XxlJob("commandJobHandler")
public void commandJobHandler() throws Exception {
String command = XxlJobHelper.getJobParam();
int exitValue = -1;
BufferedReader bufferedReader = null;
try {
// command process
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command(command);
processBuilder.redirectErrorStream(true);
Process process = processBuilder.start();
//Process process = Runtime.getRuntime().exec(command);
BufferedInputStream bufferedInputStream = new BufferedInputStream(process.getInputStream());
bufferedReader = new BufferedReader(new InputStreamReader(bufferedInputStream));
// command log
String line;
while ((line = bufferedReader.readLine()) != null) {
XxlJobHelper.log(line);
}
// command exit
process.waitFor();
exitValue = process.exitValue();
} catch (Exception e) {
XxlJobHelper.log(e);
} finally {
if (bufferedReader != null) {
bufferedReader.close();
}
}
if (exitValue == 0) {
// default success
} else {
XxlJobHelper.handleFail("command exit value(" + exitValue + ") is failed");
}
}
/**
* 4、跨平台Http任务
* 参数示例:
* "url: http://www.baidu.com\n" +
* "method: get\n" +
* "data: content\n";
*/
@XxlJob("httpJobHandler")
public void httpJobHandler() throws Exception {
// param parse
String param = XxlJobHelper.getJobParam();
if (param == null || param.trim().length() == 0) {
XxlJobHelper.log("param[" + param + "] invalid.");
XxlJobHelper.handleFail();
return;
}
String[] httpParams = param.split("\n");
String url = null;
String method = null;
String data = null;
for (String httpParam : httpParams) {
if (httpParam.startsWith("url:")) {
url = httpParam.substring(httpParam.indexOf("url:") + 4).trim();
}
if (httpParam.startsWith("method:")) {
method = httpParam.substring(httpParam.indexOf("method:") + 7).trim().toUpperCase();
}
if (httpParam.startsWith("data:")) {
data = httpParam.substring(httpParam.indexOf("data:") + 5).trim();
}
}
// param valid
if (url == null || url.trim().length() == 0) {
XxlJobHelper.log("url[" + url + "] invalid.");
XxlJobHelper.handleFail();
return;
}
if (method == null || !Arrays.asList("GET", "POST").contains(method)) {
XxlJobHelper.log("method[" + method + "] invalid.");
XxlJobHelper.handleFail();
return;
}
boolean isPostMethod = method.equals("POST");
// request
HttpURLConnection connection = null;
BufferedReader bufferedReader = null;
try {
// connection
URL realUrl = new URL(url);
connection = (HttpURLConnection) realUrl.openConnection();
// connection setting
connection.setRequestMethod(method);
connection.setDoOutput(isPostMethod);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setReadTimeout(5 * 1000);
connection.setConnectTimeout(3 * 1000);
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
connection.setRequestProperty("Accept-Charset", "application/json;charset=UTF-8");
// do connection
connection.connect();
// data
if (isPostMethod && data != null && data.trim().length() > 0) {
DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
dataOutputStream.write(data.getBytes("UTF-8"));
dataOutputStream.flush();
dataOutputStream.close();
}
// valid StatusCode
int statusCode = connection.getResponseCode();
if (statusCode != 200) {
throw new RuntimeException("Http Request StatusCode(" + statusCode + ") Invalid.");
}
// result
bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
StringBuilder result = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
result.append(line);
}
String responseMsg = result.toString();
XxlJobHelper.log(responseMsg);
return;
} catch (Exception e) {
XxlJobHelper.log(e);
XxlJobHelper.handleFail();
return;
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
if (connection != null) {
connection.disconnect();
}
} catch (Exception e2) {
XxlJobHelper.log(e2);
}
}
}
/**
* 5、生命周期任务示例:任务初始化与销毁时,支持自定义相关逻辑;
*/
@XxlJob(value = "demoJobHandler2", init = "init", destroy = "destroy")
public void demoJobHandler2() throws Exception {
XxlJobHelper.log("XXL-JOB, Hello World.");
}
public void init() {
logger.info("init");
}
public void destroy() {
logger.info("destroy");
}
}
@@ -0,0 +1,25 @@
server:
port: 7008
logging:
config: classpath:logback.xml
spring:
main:
allow-circular-references: true
xxl:
job:
accessToken: default_token
admin:
addresses: http://127.0.0.1:8080/xxl-job-admin
executor:
# 执行器名称
appname: blade-xxl-job-executor
# 若使用docker部署xxl-job-admin并且本地开发机调试连接,需要开启指定ip,采用docker的internal配置
# 若生产部署不涉及跨网段,可不配置ip
ip: 'http://host.docker.internal'
port: 9999
logpath: ../data/applogs/xxl-job/jobhandler
logretentiondays: -1
address: ''
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="false" debug="false">
<contextName>logback</contextName>
<property name="log.path" value="../data/applogs/xxl-job/xxl-job-executor-sample-springboot.log"/>
<!-- 彩色日志依赖的渲染类 -->
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/>
<conversionRule conversionWord="wex"
converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/>
<conversionRule conversionWord="wEx"
converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/>
<!-- 彩色日志格式 -->
<property name="CONSOLE_LOG_PATTERN"
value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<!-- 控制台输出 -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>utf8</charset>
</encoder>
</appender>
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}.%d{yyyy-MM-dd}.zip</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%date %level [%thread] %logger{36} [%file : %line] %msg%n
</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="console"/>
<appender-ref ref="file"/>
</root>
</configuration>
+36
View File
@@ -0,0 +1,36 @@
<?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>BladeX-Biz</artifactId>
<groupId>org.springblade</groupId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>blade-example</artifactId>
<name>${project.artifactId}</name>
<packaging>pom</packaging>
<description>BladeX 微服务范例集合</description>
<modules>
<module>blade-apollo</module>
<module>blade-mqtt-broker</module>
<module>blade-mqtt-client</module>
<module>blade-rabbit-listener</module>
<module>blade-rabbit-publisher</module>
<module>blade-seata-order</module>
<module>blade-seata-storage</module>
<module>blade-sharding-jdbc</module>
<module>blade-xxl-job</module>
</modules>
<dependencies>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-biz-common</artifactId>
</dependency>
</dependencies>
</project>