Skip to content

Commit b4bcffd

Browse files
committed
商品和订单中使用MQ
1 parent 4a84e6c commit b4bcffd

File tree

8 files changed

+179
-21
lines changed

8 files changed

+179
-21
lines changed

order/order-server/pom.xml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
<groupId>org.springframework.cloud</groupId>
2222
<artifactId>spring-cloud-starter-openfeign</artifactId>
2323
</dependency>
24+
<dependency>
25+
<groupId>org.springframework.cloud</groupId>
26+
<artifactId>spring-cloud-config-client</artifactId>
27+
</dependency>
2428
<dependency>
2529
<groupId>org.springframework.cloud</groupId>
2630
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
@@ -29,6 +33,10 @@
2933
<groupId>org.springframework.cloud</groupId>
3034
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
3135
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-data-redis</artifactId>
39+
</dependency>
3240
<!-- TODO spring-cloud-starter-hystrix ——> spring-cloud-starter-netflix-hystrix-->
3341
<!-- TODO 2.0.2.RELEASE需要引入-->
3442
<dependency>
@@ -51,10 +59,6 @@
5159
<groupId>com.google.code.gson</groupId>
5260
<artifactId>gson</artifactId>
5361
</dependency>
54-
<dependency>
55-
<groupId>org.springframework.cloud</groupId>
56-
<artifactId>spring-cloud-config-client</artifactId>
57-
</dependency>
5862
<dependency>
5963
<groupId>cn.algerfan</groupId>
6064
<artifactId>product-client</artifactId>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package cn.algerfan.order.server.message;
2+
3+
import cn.algerfan.order.server.util.JsonUtil;
4+
import cn.algerfan.product.common.ProductInfoOutput;
5+
import com.fasterxml.jackson.core.type.TypeReference;
6+
import lombok.extern.slf4j.Slf4j;
7+
import org.springframework.amqp.rabbit.annotation.Queue;
8+
import org.springframework.amqp.rabbit.annotation.RabbitListener;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.data.redis.core.StringRedisTemplate;
11+
import org.springframework.stereotype.Component;
12+
13+
import java.util.List;
14+
15+
/**
16+
* @author algerfan
17+
* @time 2019/8/27 11:59
18+
*/
19+
@Component
20+
@Slf4j
21+
public class ProductReceiver {
22+
23+
private static final String PRODUCT_STOCK_TEMPLATE = "product_stock_%s";
24+
25+
@Autowired
26+
private StringRedisTemplate stringRedisTemplate;
27+
28+
@RabbitListener(queuesToDeclare = @Queue("productInfo"))
29+
public void process(String message) {
30+
//message——>productInfoOutput
31+
List<ProductInfoOutput> productInfoOutputList = (List<ProductInfoOutput>) JsonUtil.fromJson(message,
32+
new TypeReference<List<ProductInfoOutput>>() {});
33+
log.info("从队列{}接收到消息{}", "productInfo", productInfoOutputList);
34+
//存储在redis中
35+
assert productInfoOutputList != null;
36+
for (ProductInfoOutput productInfoOutput : productInfoOutputList) {
37+
stringRedisTemplate.opsForValue().set(String.format(PRODUCT_STOCK_TEMPLATE, productInfoOutput.getProductId()),
38+
String.valueOf(productInfoOutput.getProductStock()));
39+
}
40+
}
41+
42+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package cn.algerfan.order.server.util;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.core.type.TypeReference;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
7+
import java.io.IOException;
8+
9+
/**
10+
* @author algerfan
11+
*/
12+
public class JsonUtil {
13+
14+
private static ObjectMapper objectMapper = new ObjectMapper();
15+
16+
/**
17+
* 转换为json字符串
18+
* @param object
19+
* @return
20+
*/
21+
public static String toJson(Object object) {
22+
try {
23+
return objectMapper.writeValueAsString(object);
24+
} catch (JsonProcessingException e) {
25+
e.printStackTrace();
26+
}
27+
return null;
28+
}
29+
30+
/**
31+
* json转对象
32+
* @param string
33+
* @param classType
34+
* @return
35+
*/
36+
public static Object fromJson(String string, Class classType) {
37+
try {
38+
return objectMapper.readValue(string, classType);
39+
} catch (IOException e) {
40+
e.printStackTrace();
41+
}
42+
return null;
43+
}
44+
45+
46+
/**
47+
* json转对象
48+
* @param string
49+
* @param typeReference
50+
* @return
51+
*/
52+
public static Object fromJson(String string, TypeReference typeReference) {
53+
try {
54+
return objectMapper.readValue(string, typeReference);
55+
} catch (IOException e) {
56+
e.printStackTrace();
57+
}
58+
return null;
59+
}
60+
}

product/product-server/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@
2020
<groupId>org.springframework.cloud</groupId>
2121
<artifactId>spring-cloud-starter-openfeign</artifactId>
2222
</dependency>
23+
<dependency>
24+
<groupId>org.springframework.cloud</groupId>
25+
<artifactId>spring-cloud-config-client</artifactId>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework.cloud</groupId>
29+
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
30+
</dependency>
2331
<!-- TODO 2.0.2.RELEASE需要引入-->
2432
<dependency>
2533
<groupId>org.springframework.boot</groupId>

product/product-server/src/main/java/cn/algerfan/product/server/service/impl/ProductServiceImpl.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import cn.algerfan.product.server.exception.ProductException;
99
import cn.algerfan.product.server.repository.ProductInfoRepository;
1010
import cn.algerfan.product.server.service.ProductService;
11+
import cn.algerfan.product.server.util.JsonUtil;
12+
import org.springframework.amqp.core.AmqpTemplate;
1113
import org.springframework.beans.BeanUtils;
1214
import org.springframework.beans.factory.annotation.Autowired;
1315
import org.springframework.stereotype.Service;
@@ -16,6 +18,7 @@
1618
import java.util.ArrayList;
1719
import java.util.List;
1820
import java.util.Optional;
21+
import java.util.stream.Collectors;
1922

2023
/**
2124
* @author algerfan
@@ -26,6 +29,8 @@ public class ProductServiceImpl implements ProductService {
2629

2730
@Autowired
2831
private ProductInfoRepository productInfoRepository;
32+
@Autowired
33+
private AmqpTemplate amqpTemplate;
2934

3035
@Override
3136
public List<ProductInfo> findUpAll() {
@@ -45,8 +50,20 @@ public List<ProductInfoOutput> productInfoList(List<String> productIdList) {
4550
}
4651

4752
@Override
48-
@Transactional(rollbackFor = Exception.class)
4953
public void decreaseStock(List<DecreaseStockOutput> cartDtoList) {
54+
List<ProductInfo> productInfoList = decreaseStockProcess(cartDtoList);
55+
//发送mq消息
56+
List<ProductInfoOutput> collect = productInfoList.stream().map(e -> {
57+
ProductInfoOutput productInfoOutput = new ProductInfoOutput();
58+
BeanUtils.copyProperties(e, productInfoOutput);
59+
return productInfoOutput;
60+
}).collect(Collectors.toList());
61+
amqpTemplate.convertAndSend("productInfo", JsonUtil.toJson(collect));
62+
}
63+
64+
@Transactional(rollbackFor = Exception.class)
65+
public List<ProductInfo> decreaseStockProcess(List<DecreaseStockOutput> cartDtoList) {
66+
List<ProductInfo> productInfoList = new ArrayList<>();
5067
for (DecreaseStockOutput cartDto : cartDtoList) {
5168
Optional<ProductInfo> productInfoOptional = productInfoRepository.findById(cartDto.getProductId());
5269
//判断商品是否存在
@@ -61,6 +78,8 @@ public void decreaseStock(List<DecreaseStockOutput> cartDtoList) {
6178
}
6279
productInfo.setProductStock(result);
6380
productInfoRepository.save(productInfo);
81+
productInfoList.add(productInfo);
6482
}
83+
return productInfoList;
6584
}
6685
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cn.algerfan.product.server.util;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
6+
/**
7+
* @author algerfan
8+
*/
9+
public class JsonUtil {
10+
11+
private static ObjectMapper objectMapper = new ObjectMapper();
12+
13+
/**
14+
* 转换为json字符串
15+
* @param object
16+
* @return
17+
*/
18+
public static String toJson(Object object) {
19+
try {
20+
return objectMapper.writeValueAsString(object);
21+
} catch (JsonProcessingException e) {
22+
e.printStackTrace();
23+
}
24+
return null;
25+
}
26+
}

product/product-server/src/main/resources/application.yml

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
eureka:
2+
client:
3+
service-url:
4+
defaultZone: http://127.0.0.1:8761/eureka/
5+
instance:
6+
prefer-ip-address: true
7+
spring:
8+
application:
9+
name: product
10+
cloud:
11+
config:
12+
discovery:
13+
enabled: true
14+
service-id: CONFIG
15+
profile: dev

0 commit comments

Comments
 (0)