mirror of
https://github.com/jeecgboot/JeecgBoot.git
synced 2026-02-05 10:05:33 +08:00
3.2.0-beta,重构很大:升级springboot2.6.6、spring-cloud-alibaba 2021.1、mybatisplus3.5.1、代码规范部分重构
This commit is contained in:
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>jeecg-cloud-test</artifactId>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<version>3.1.0</version>
|
||||
<version>3.2.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<description>公共测试模块</description>
|
||||
|
||||
@ -5,11 +5,6 @@ package org.jeecg.modules.test.constant;
|
||||
*/
|
||||
public interface CloudConstant {
|
||||
|
||||
/**
|
||||
* 微服务名【对应模块jeecg-boot-module-demo】
|
||||
*/
|
||||
public final static String SERVER_NAME_JEECGDEMO = "jeecg-demo";
|
||||
|
||||
/**
|
||||
* MQ测试队列名字
|
||||
*/
|
||||
|
||||
@ -2,21 +2,24 @@ package org.jeecg.modules.test.feign.client;
|
||||
|
||||
import org.jeecg.common.api.vo.Result;
|
||||
|
||||
import org.jeecg.common.constant.ServiceNameConstants;
|
||||
import org.jeecg.config.FeignConfig;
|
||||
import org.jeecg.modules.test.constant.CloudConstant;
|
||||
import org.jeecg.modules.test.feign.factory.JeecgTestClientFactory;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.Mapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
/**
|
||||
* 常规feign接口定义
|
||||
*/
|
||||
@FeignClient(value = CloudConstant.SERVER_NAME_JEECGDEMO, configuration = FeignConfig.class,fallbackFactory = JeecgTestClientFactory.class)
|
||||
@FeignClient(value = ServiceNameConstants.SERVICE_DEMO, configuration = FeignConfig.class,fallbackFactory = JeecgTestClientFactory.class)
|
||||
@Component
|
||||
public interface JeecgTestClient {
|
||||
|
||||
@PostMapping(value = "/test/getMessage")
|
||||
Result<Object> getMessage(@RequestParam(value = "name",required = false) String name);
|
||||
@GetMapping(value = "/test/getMessage")
|
||||
String getMessage(@RequestParam(value = "name",required = false) String name);
|
||||
}
|
||||
|
||||
@ -1,14 +0,0 @@
|
||||
package org.jeecg.modules.test.feign.client;
|
||||
|
||||
import org.jeecg.common.api.vo.Result;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
/**
|
||||
* 动态feign接口定义
|
||||
*/
|
||||
public interface JeecgTestClientDyn {
|
||||
|
||||
@PostMapping(value = "/test/getMessage")
|
||||
Result<String> getMessage(@RequestParam(value = "name",required = false) String name);
|
||||
}
|
||||
@ -1,63 +1,75 @@
|
||||
package org.jeecg.modules.test.feign.controller;
|
||||
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.jeecg.boot.starter.rabbitmq.client.RabbitMqClient;
|
||||
import org.jeecg.common.api.vo.Result;
|
||||
import org.jeecg.modules.test.feign.client.JeecgTestClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import com.alibaba.csp.sentinel.annotation.SentinelResource;
|
||||
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/sys/test")
|
||||
@Api(tags = "【微服务】单元测试")
|
||||
public class JeecgTestFeignController {
|
||||
|
||||
// @Autowired
|
||||
//private JeecgFeignService jeecgFeignService;
|
||||
|
||||
@Autowired
|
||||
private JeecgTestClient jeecgTestClient;
|
||||
|
||||
@Autowired
|
||||
private RabbitMqClient rabbitMqClient;
|
||||
/**
|
||||
* 熔断: fallbackFactory优先于 @SentinelResource
|
||||
*
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/getMessage")
|
||||
@ApiOperation(value = "测试feign调用demo服务1", notes = "测试feign @SentinelResource熔断写法 | 测试熔断关闭jeecg-demo服务")
|
||||
@SentinelResource(value = "test_more_getMessage", fallback = "getDefaultUser")
|
||||
public Result<String> getMessage(@RequestParam(value = "name", required = false) String name) {
|
||||
log.info("---------Feign fallbackFactory优先级高于@SentinelResource-----------------");
|
||||
String resultMsg = jeecgTestClient.getMessage(" I am jeecg-system 服务节点,呼叫 jeecg-demo!");
|
||||
return Result.OK(null, resultMsg);
|
||||
}
|
||||
|
||||
@PostMapping("getMessage")
|
||||
@ApiOperation(value = "测试feign", notes = "测试feign")
|
||||
@SentinelResource(value = "fallback",fallback = "getDefaultUser")
|
||||
public Result<Object> getMessage(@RequestParam(value = "name",required = false) String name) {
|
||||
return jeecgTestClient.getMessage("fegin——jeecg-boot2");
|
||||
/**
|
||||
* 测试方法:关闭demo服务,访问请求 http://127.0.0.1:9999/sys/test/getMessage
|
||||
*
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/getMessage2")
|
||||
@ApiOperation(value = "测试feign调用demo服务2", notes = "测试feign fallbackFactory熔断写法 | 测试熔断关闭jeecg-demo服务")
|
||||
public Result<String> getMessage2(@RequestParam(value = "name", required = false) String name) {
|
||||
log.info("---------测试 Feign fallbackFactory-----------------");
|
||||
String resultMsg = jeecgTestClient.getMessage(" I am jeecg-system 服务节点,呼叫 jeecg-demo!");
|
||||
return Result.OK(null, resultMsg);
|
||||
}
|
||||
|
||||
|
||||
// @GetMapping("getMessage2")
|
||||
// @ApiOperation(value = "测试动态feign", notes = "测试动态feign")
|
||||
// public Result<String> getMessage2() {
|
||||
// JeecgTestClientDyn myClientDyn = jeecgFeignService.newInstance(JeecgTestClientDyn.class, CloudConstant.SERVER_NAME_JEECGDEMO);
|
||||
// return myClientDyn.getMessage("动态fegin——jeecg-boot2");
|
||||
// }
|
||||
|
||||
@PostMapping("test")
|
||||
@GetMapping("/fallback")
|
||||
@ApiOperation(value = "测试熔断", notes = "测试熔断")
|
||||
@SentinelResource(value = "fallback",fallback = "getDefaultUser")
|
||||
public Result<Object> test(@RequestParam(value = "name",required = false) String name) {
|
||||
if(StringUtils.isEmpty(name)){
|
||||
@SentinelResource(value = "test_more_fallback", fallback = "getDefaultUser")
|
||||
public Result<Object> test(@RequestParam(value = "name", required = false) String name) {
|
||||
if (StringUtils.isEmpty(name)) {
|
||||
throw new IllegalArgumentException("name param is empty");
|
||||
}
|
||||
|
||||
return Result.OK();
|
||||
}
|
||||
|
||||
/**
|
||||
* 熔断,默认回调函数
|
||||
*
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
public Result<Object> getDefaultUser(String name) {
|
||||
System.out.println("熔断,默认回调函数");
|
||||
return Result.OK("访问超时");
|
||||
log.info("熔断,默认回调函数");
|
||||
return Result.error(null, "访问超时, 自定义 @SentinelResource Fallback");
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,7 +3,7 @@ package org.jeecg.modules.test.feign.factory;
|
||||
|
||||
|
||||
|
||||
import feign.hystrix.FallbackFactory;
|
||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
import org.jeecg.modules.test.feign.client.JeecgTestClient;
|
||||
import org.jeecg.modules.test.feign.fallback.JeecgTestFallback;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@ -7,8 +7,11 @@ import org.jeecg.modules.test.feign.client.JeecgTestClient;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
* 接口fallback实现
|
||||
*
|
||||
* @author: scott
|
||||
* @date: 2022/4/11 19:41
|
||||
*/
|
||||
public class JeecgTestFallback implements JeecgTestClient {
|
||||
|
||||
@Setter
|
||||
@ -16,7 +19,7 @@ public class JeecgTestFallback implements JeecgTestClient {
|
||||
|
||||
|
||||
@Override
|
||||
public Result<Object> getMessage(String name) {
|
||||
return Result.OK("访问超时");
|
||||
public String getMessage(String name) {
|
||||
return "访问超时, 自定义FallbackFactory";
|
||||
}
|
||||
}
|
||||
|
||||
@ -7,6 +7,7 @@ import org.jeecg.boot.starter.rabbitmq.client.RabbitMqClient;
|
||||
import org.jeecg.common.base.BaseMap;
|
||||
import org.jeecg.modules.test.constant.CloudConstant;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
@ -19,29 +20,33 @@ import java.util.Map;
|
||||
public class DemoLockTest {
|
||||
@Autowired
|
||||
RedissonLockClient redissonLock;
|
||||
@Autowired
|
||||
RabbitMqClient rabbitMqClient;
|
||||
// @Autowired
|
||||
// RabbitMqClient rabbitMqClient;
|
||||
|
||||
/**
|
||||
* 测试方法:
|
||||
* @Scheduled(cron = "0/5 * * * * ?") 表示每5秒执行一次
|
||||
* @JLock(lockKey = CloudConstant.REDISSON_DEMO_LOCK_KEY1)分布式锁,10秒钟才释放
|
||||
* 结果:每10秒钟输出一次 “执行 分布式锁 业务逻辑1” 就说明锁成功了
|
||||
*
|
||||
* 测试分布式锁【注解方式】
|
||||
*/
|
||||
//@Scheduled(cron = "0/5 * * * * ?")
|
||||
@Scheduled(cron = "0/5 * * * * ?")
|
||||
@JLock(lockKey = CloudConstant.REDISSON_DEMO_LOCK_KEY1)
|
||||
public void execute() throws InterruptedException {
|
||||
log.info("执行execute任务开始,休眠三秒");
|
||||
Thread.sleep(3000);
|
||||
System.out.println("=======================业务逻辑1=============================");
|
||||
Map map = new BaseMap();
|
||||
map.put("orderId", "BJ0001");
|
||||
rabbitMqClient.sendMessage(CloudConstant.MQ_JEECG_PLACE_ORDER, map);
|
||||
//延迟10秒发送
|
||||
map.put("orderId", "NJ0002");
|
||||
rabbitMqClient.sendMessage(CloudConstant.MQ_JEECG_PLACE_ORDER, map, 10000);
|
||||
log.info("execute任务结束,休眠三秒");
|
||||
log.info("执行execute任务开始,休眠十秒开始,当前系统时间戳(秒):"+ System.currentTimeMillis()/1000);
|
||||
Thread.sleep(10000);
|
||||
log.info("========执行 分布式锁 业务逻辑1=============");
|
||||
// Map map = new BaseMap();
|
||||
// map.put("orderId", "BJ0001");
|
||||
// rabbitMqClient.sendMessage(CloudConstant.MQ_JEECG_PLACE_ORDER, map);
|
||||
// //延迟10秒发送
|
||||
// map.put("orderId", "NJ0002");
|
||||
// rabbitMqClient.sendMessage(CloudConstant.MQ_JEECG_PLACE_ORDER, map, 10000);
|
||||
|
||||
log.info("execute任务结束,休眠十秒完成,当前系统时间戳(秒):"+ System.currentTimeMillis()/1000);
|
||||
}
|
||||
|
||||
public DemoLockTest() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试分布式锁【编码方式】
|
||||
@ -51,7 +56,7 @@ public class DemoLockTest {
|
||||
if (redissonLock.tryLock(CloudConstant.REDISSON_DEMO_LOCK_KEY2, -1, 6000)) {
|
||||
log.info("执行任务execute2开始,休眠十秒");
|
||||
Thread.sleep(10000);
|
||||
System.out.println("=======================业务逻辑2=============================");
|
||||
log.info("=============业务逻辑2===================");
|
||||
log.info("定时execute2结束,休眠十秒");
|
||||
|
||||
redissonLock.unlock(CloudConstant.REDISSON_DEMO_LOCK_KEY2);
|
||||
|
||||
@ -32,30 +32,28 @@ public class DemoJobHandler {
|
||||
* @param params
|
||||
* @return
|
||||
*/
|
||||
|
||||
@XxlJob(value = "demoJob")
|
||||
public ReturnT<String> demoJobHandler(String params) {
|
||||
log.info("我是定时任务,我执行了...............................");
|
||||
log.info("我是 jeecg-system 服务里的定时任务 demoJob,我执行了...............................");
|
||||
return ReturnT.SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* 2、分片广播任务
|
||||
*/
|
||||
|
||||
@XxlJob("shardingJobHandler")
|
||||
public ReturnT<String> shardingJobHandler(String param) throws Exception {
|
||||
|
||||
// 分片参数
|
||||
ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo();
|
||||
XxlJobLogger.log("分片参数:当前分片序号 = {}, 总分片数 = {}", shardingVO.getIndex(), shardingVO.getTotal());
|
||||
log.info("分片参数:当前分片序号 = {}, 总分片数 = {}", shardingVO.getIndex(), shardingVO.getTotal());
|
||||
|
||||
// 业务逻辑
|
||||
for (int i = 0; i < shardingVO.getTotal(); i++) {
|
||||
if (i == shardingVO.getIndex()) {
|
||||
XxlJobLogger.log("第 {} 片, 命中分片开始处理", i);
|
||||
log.info("第 {} 片, 命中分片开始处理", i);
|
||||
} else {
|
||||
XxlJobLogger.log("第 {} 片, 忽略", i);
|
||||
log.info("第 {} 片, 忽略", i);
|
||||
}
|
||||
}
|
||||
|
||||
@ -65,8 +63,9 @@ public class DemoJobHandler {
|
||||
|
||||
/**
|
||||
* 3、命令行任务
|
||||
*
|
||||
* 输入参数:ipconfig /all
|
||||
*/
|
||||
|
||||
@XxlJob("commandJobHandler")
|
||||
public ReturnT<String> commandJobHandler(String param) throws Exception {
|
||||
String command = param;
|
||||
@ -82,14 +81,14 @@ public class DemoJobHandler {
|
||||
// command log
|
||||
String line;
|
||||
while ((line = bufferedReader.readLine()) != null) {
|
||||
XxlJobLogger.log(line);
|
||||
log.info(line);
|
||||
}
|
||||
|
||||
// command exit
|
||||
process.waitFor();
|
||||
exitValue = process.exitValue();
|
||||
} catch (Exception e) {
|
||||
XxlJobLogger.log(e);
|
||||
log.info(e.getMessage(),e);
|
||||
} finally {
|
||||
if (bufferedReader != null) {
|
||||
bufferedReader.close();
|
||||
@ -106,18 +105,18 @@ public class DemoJobHandler {
|
||||
|
||||
/**
|
||||
* 4、跨平台Http任务
|
||||
* 参数示例:
|
||||
* "url: http://www.baidu.com\n" +
|
||||
* "method: get\n" +
|
||||
* "data: content\n";
|
||||
*
|
||||
* 输入参数:
|
||||
* url: https://www.baidu.com
|
||||
* method: get
|
||||
* data: content
|
||||
*/
|
||||
|
||||
@XxlJob("httpJobHandler")
|
||||
public ReturnT<String> httpJobHandler(String param) throws Exception {
|
||||
|
||||
// param parse
|
||||
if (param == null || param.trim().length() == 0) {
|
||||
XxlJobLogger.log("param[" + param + "] invalid.");
|
||||
log.info("param[" + param + "] invalid.");
|
||||
return ReturnT.FAIL;
|
||||
}
|
||||
String[] httpParams = param.split("\n");
|
||||
@ -138,11 +137,11 @@ public class DemoJobHandler {
|
||||
|
||||
// param valid
|
||||
if (url == null || url.trim().length() == 0) {
|
||||
XxlJobLogger.log("url[" + url + "] invalid.");
|
||||
log.info("url[" + url + "] invalid.");
|
||||
return ReturnT.FAIL;
|
||||
}
|
||||
if (method == null || !Arrays.asList("GET", "POST").contains(method)) {
|
||||
XxlJobLogger.log("method[" + method + "] invalid.");
|
||||
log.info("method[" + method + "] invalid.");
|
||||
return ReturnT.FAIL;
|
||||
}
|
||||
|
||||
@ -191,10 +190,10 @@ public class DemoJobHandler {
|
||||
}
|
||||
String responseMsg = result.toString();
|
||||
|
||||
XxlJobLogger.log(responseMsg);
|
||||
log.info(responseMsg);
|
||||
return ReturnT.SUCCESS;
|
||||
} catch (Exception e) {
|
||||
XxlJobLogger.log(e);
|
||||
log.info(e.getMessage(),e);
|
||||
return ReturnT.FAIL;
|
||||
} finally {
|
||||
try {
|
||||
@ -205,7 +204,7 @@ public class DemoJobHandler {
|
||||
connection.disconnect();
|
||||
}
|
||||
} catch (Exception e2) {
|
||||
XxlJobLogger.log(e2);
|
||||
log.info(e2.getMessage(),e2);
|
||||
}
|
||||
}
|
||||
|
||||
@ -215,10 +214,9 @@ public class DemoJobHandler {
|
||||
/**
|
||||
* 5、生命周期任务示例:任务初始化与销毁时,支持自定义相关逻辑;
|
||||
*/
|
||||
|
||||
@XxlJob(value = "demoJobHandler2", init = "init", destroy = "destroy")
|
||||
public ReturnT<String> demoJobHandler2(String param) throws Exception {
|
||||
XxlJobLogger.log("XXL-JOB, Hello World.");
|
||||
log.info("XXL-JOB, Hello World.");
|
||||
return ReturnT.SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
@ -22,9 +22,9 @@ public class XxclJobTest {
|
||||
* @return
|
||||
*/
|
||||
|
||||
@XxlJob(value = "testJob")
|
||||
@XxlJob(value = "xxclJobTest")
|
||||
public ReturnT<String> demoJobHandler(String params) {
|
||||
log.info("我是demo服务里的定时任务testJob,我执行了...............................");
|
||||
log.info("我是 jeecg-system 服务里的定时任务 xxclJobTest , 我执行了...............................");
|
||||
return ReturnT.SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user