提交代码

dev-discount
胡锐 2 years ago
parent d8b9db3b6b
commit a3c288e2ee
  1. 2
      hai-order/src/main/java/com/web/controller/TestController.java

@ -1 +1 @@
package com.web.controller; import com.hai.common.utils.*; import com.hai.entity.HighOrder; import com.hai.model.ResponseData; import com.hai.order.service.OrderService; import com.hai.service.*; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.rocketmq.spring.core.RocketMQTemplate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; /** * @Auther: 胡锐 * @Description: * @Date: 2021/3/26 23:08 */ @Controller @RequestMapping(value = "/test") @Api(value = "订单接口") public class TestController { private static Logger log = LoggerFactory.getLogger(TestController.class); @Autowired private RocketMQTemplate rocketMQTemplate; @Resource private OrderService orderService; @Resource private RedisUtil redisUtil; @Autowired private RedisTemplate redisTemplate; @RequestMapping(value = "/rocketMq", method = RequestMethod.GET) @ResponseBody @ApiOperation(value = "") public ResponseData rocketMq(@RequestParam(name = "topicGroup", required = true) String topicGroup, @RequestParam(name = "body", required = true) String body) { try { long startTime = System.currentTimeMillis(); String couponKey = "123"; List<HighOrder> list = new LinkedList<>(); for (int i = 0; i <= 100;i++) { list.add(orderService.getDetailByOrderNo("HF2022080918014365701")); /* list.add(orderService.getDetailByOrderNo("HF2022080918014365701"));*/ /* updateUserWithRedisLock("coupon_" + couponKey, i+""); new Thread(() -> {*/ /* redisTemplate.opsForValue().setIfAbsent("coupon_" + couponKey, i); String couponKey = "123"; String couponKey = "123"; String couponKey = "123"; String couponKey = "123"; String couponKey = "123"; List<HighOrder> list = new LinkedList<>(); String couponKey = "123"; for (int i = 0; i <= 100;i++) { String couponKey = "123"; list.add(orderService.getDetailByOrderNo("HF2022080918014365701")); String couponKey = "123"; /* list.add(orderService.getDetailByOrderNo("HF2022080918014365701"));*/ String couponKey = "123"; /* updateUserWithRedisLock("coupon_" + couponKey, i+""); t); } catch (Exception e) { log.error("HighUserCardController --> rocketMq() error!", e); return ResponseMsgUtil.exception(e); } } public void updateUserWithRedisLock(String couponKey, String value) throws InterruptedException { // 占分布式锁,去redis占坑 // 1. 分布式锁占坑 Boolean lock = redisTemplate.opsForValue().setIfAbsent("coupon" + couponKey, value); if(lock) { //加锁成功... // todo business System.out.println("加锁成功: " + value); redisTemplate.delete("coupon" + couponKey); //删除key,释放锁 } else { System.out.println("加锁失败"); /* Thread.sleep(100); // 加锁失败,重试 updateUserWithRedisLock("sysUser");*/ } } } package com.web.controller; import com.hai.common.utils.*; import com.hai.entity.HighOrder; import com.hai.model.ResponseData; import com.hai.order.service.OrderService; import com.hai.service.*; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.rocketmq.client.producer.SendResult; import org.apache.rocketmq.spring.core.RocketMQTemplate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.integration.support.MessageBuilder; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; /** * @Auther: 胡锐 * @Description: * @Date: 2021/3/26 23:08 */ @Controller @RequestMapping(value = "/test") @Api(value = "订单接口") public class TestController { private static Logger log = LoggerFactory.getLogger(TestController.class); @Autowired private RocketMQTemplate rocketMQTemplate; @Resource private OrderService orderService; @Resource private RedisUtil redisUtil; @Autowired private RedisTemplate redisTemplate; @RequestMapping(value = "/rocketMq", method = RequestMethod.GET) @ResponseBody @ApiOperation(value = "") public ResponseData rocketMq(@RequestParam(name = "topicGroup", required = true) String topicGroup, @RequestParam(name = "body", required = true) String body) { try { long startTime = System.currentTimeMillis(); String couponKey = "123"; new Thread(() -> {*/ HighOrder order = orderService.getDetailByOrderNo("HF2022080918014365701"); org.springframework.messaging.Message message = MessageBuilder.withPayload(order).build(); SendResult sendResult = rocketMQTemplate.syncSend("order-topic:create", message, 3000); List<HighOrder> list = new LinkedList<>(); for (int i = 0; i <= 100;i++) { list.add(orderService.getDetailByOrderNo("HF2022080918014365701")); /* list.add(orderService.getDetailByOrderNo("HF2022080918014365701"));*/ /* updateUserWithRedisLock("coupon_" + couponKey, i+""); new Thread(() -> {*/ /* redisTemplate.opsForValue().setIfAbsent("coupon_" + couponKey, i); List<HighOrder> list = new LinkedList<>(); List<HighOrder> list = new LinkedList<>(); String couponKey = "123"; List<HighOrder> list = new LinkedList<>(); List<HighOrder> list = new LinkedList<>(); List<HighOrder> list = new LinkedList<>(); List<HighOrder> list = new LinkedList<>(); for (int i = 0; i <= 100;i++) { List<HighOrder> list = new LinkedList<>(); list.add(orderService.getDetailByOrderNo("HF2022080918014365701")); List<HighOrder> list = new LinkedList<>(); /* list.add(orderService.getDetailByOrderNo("HF2022080918014365701"));*/ List<HighOrder> list = new LinkedList<>(); /* updateUserWithRedisLock("coupon_" + couponKey, i+""); return ResponseMsgUtil.success(sendResult); } catch (Exception e) { log.error("HighUserCardController --> rocketMq() error!", e); return ResponseMsgUtil.exception(e); } } public void updateUserWithRedisLock(String couponKey, String value) throws InterruptedException { // 占分布式锁,去redis占坑 // 1. 分布式锁占坑 Boolean lock = redisTemplate.opsForValue().setIfAbsent("coupon" + couponKey, value); if(lock) { //加锁成功... // todo business System.out.println("加锁成功: " + value); redisTemplate.delete("coupon" + couponKey); //删除key,释放锁 } else { System.out.println("加锁失败"); /* Thread.sleep(100); // 加锁失败,重试 updateUserWithRedisLock("sysUser");*/ } } }
Loading…
Cancel
Save