diff --git a/hai-order/src/main/java/com/web/controller/TestController.java b/hai-order/src/main/java/com/web/controller/TestController.java index b8e1e92e..d29518cf 100644 --- a/hai-order/src/main/java/com/web/controller/TestController.java +++ b/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.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 list = new ArrayList<>(); for (int i = 0; i <= 1000;i++) { orderService.getDetailByOrderNo("HF2022080918014365701"); /* list.add(orderService.getDetailByOrderNo("HF2022080918014365701"));*/ /* updateUserWithRedisLock("coupon_" + couponKey, i+""); new Thread(() -> {*/ /* redisTemplate.opsForValue().setIfAbsent("coupon_" + couponKey, i); System.out.println("加入数据" + i);*/ // redisTemplate.delete("coupon" + couponKey); // System.out.println("释放数据" + index); /* });*/ } System.out.println(System.currentTimeMillis() - startTime); return ResponseMsgUtil.success(list); } 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");*/ } } } \ No newline at end of file +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 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); System.out.println("加入数据" + i);*/ // redisTemplate.delete("coupon" + couponKey); // System.out.println("释放数据" + index); /* });*/ } System.out.println(System.currentTimeMillis() - startTime); return ResponseMsgUtil.success(list); } 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");*/ } } } \ No newline at end of file diff --git a/hai-service/pom.xml b/hai-service/pom.xml index 10b9756a..613f8523 100644 --- a/hai-service/pom.xml +++ b/hai-service/pom.xml @@ -47,6 +47,11 @@ org.springframework.boot spring-boot-starter-data-redis + + com.alicp.jetcache + jetcache-starter-redis + 2.5.0 + @@ -352,11 +357,6 @@ org.projectlombok lombok - - com.alicp.jetcache - jetcache-starter-redis - 2.6.7 - diff --git a/hai-service/src/main/java/com/hai/order/service/OrderService.java b/hai-service/src/main/java/com/hai/order/service/OrderService.java index 3dcf3dc2..85b2a5bb 100644 --- a/hai-service/src/main/java/com/hai/order/service/OrderService.java +++ b/hai-service/src/main/java/com/hai/order/service/OrderService.java @@ -4,6 +4,9 @@ import com.alicp.jetcache.anno.CacheType; import com.alicp.jetcache.anno.Cached; import com.hai.entity.HighOrder; +import java.util.List; +import java.util.Map; + /** * @className: OrderService * @author: HuRui @@ -11,6 +14,7 @@ import com.hai.entity.HighOrder; **/ public interface OrderService { - @Cached(cacheType = CacheType.REMOTE,name="OrderService.getDetailByOrderNo", expire = 3600) HighOrder getDetailByOrderNo(String orderNo); + + List getOrderList(Map param); } diff --git a/hai-service/src/main/java/com/hai/order/service/impl/OrderServiceImpl.java b/hai-service/src/main/java/com/hai/order/service/impl/OrderServiceImpl.java index cd6b4fe1..b511fa82 100644 --- a/hai-service/src/main/java/com/hai/order/service/impl/OrderServiceImpl.java +++ b/hai-service/src/main/java/com/hai/order/service/impl/OrderServiceImpl.java @@ -1,25 +1,35 @@ package com.hai.order.service.impl; +import com.alicp.jetcache.anno.CacheType; +import com.alicp.jetcache.anno.Cached; import com.hai.entity.HighOrder; import com.hai.order.service.OrderService; import com.hai.service.HighOrderService; import org.springframework.stereotype.Service; import javax.annotation.Resource; +import java.util.List; +import java.util.Map; /** * @className: OrderServiceImpl * @author: HuRui * @date: 2022/8/18 **/ -@Service("orderService123") +@Service("newOrderService") public class OrderServiceImpl implements OrderService { @Resource private HighOrderService highOrderService; @Override + @Cached(cacheType = CacheType.BOTH,name="Order:", key = "#orderNo",expire = 3600) public HighOrder getDetailByOrderNo(String orderNo) { return highOrderService.getOrderByOrderNo(orderNo); } + + @Override + public List getOrderList(Map param) { + return highOrderService.getOrderList(param); + } }