-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
179 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
order/order-server/src/main/java/cn/algerfan/order/server/message/ProductReceiver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package cn.algerfan.order.server.message; | ||
|
||
import cn.algerfan.order.server.util.JsonUtil; | ||
import cn.algerfan.product.common.ProductInfoOutput; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.amqp.rabbit.annotation.Queue; | ||
import org.springframework.amqp.rabbit.annotation.RabbitListener; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.redis.core.StringRedisTemplate; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* @author algerfan | ||
* @time 2019/8/27 11:59 | ||
*/ | ||
@Component | ||
@Slf4j | ||
public class ProductReceiver { | ||
|
||
private static final String PRODUCT_STOCK_TEMPLATE = "product_stock_%s"; | ||
|
||
@Autowired | ||
private StringRedisTemplate stringRedisTemplate; | ||
|
||
@RabbitListener(queuesToDeclare = @Queue("productInfo")) | ||
public void process(String message) { | ||
//message——>productInfoOutput | ||
List<ProductInfoOutput> productInfoOutputList = (List<ProductInfoOutput>) JsonUtil.fromJson(message, | ||
new TypeReference<List<ProductInfoOutput>>() {}); | ||
log.info("从队列{}接收到消息{}", "productInfo", productInfoOutputList); | ||
//存储在redis中 | ||
assert productInfoOutputList != null; | ||
for (ProductInfoOutput productInfoOutput : productInfoOutputList) { | ||
stringRedisTemplate.opsForValue().set(String.format(PRODUCT_STOCK_TEMPLATE, productInfoOutput.getProductId()), | ||
String.valueOf(productInfoOutput.getProductStock())); | ||
} | ||
} | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
order/order-server/src/main/java/cn/algerfan/order/server/util/JsonUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package cn.algerfan.order.server.util; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* @author algerfan | ||
*/ | ||
public class JsonUtil { | ||
|
||
private static ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
/** | ||
* 转换为json字符串 | ||
* @param object | ||
* @return | ||
*/ | ||
public static String toJson(Object object) { | ||
try { | ||
return objectMapper.writeValueAsString(object); | ||
} catch (JsonProcessingException e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* json转对象 | ||
* @param string | ||
* @param classType | ||
* @return | ||
*/ | ||
public static Object fromJson(String string, Class classType) { | ||
try { | ||
return objectMapper.readValue(string, classType); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
|
||
|
||
/** | ||
* json转对象 | ||
* @param string | ||
* @param typeReference | ||
* @return | ||
*/ | ||
public static Object fromJson(String string, TypeReference typeReference) { | ||
try { | ||
return objectMapper.readValue(string, typeReference); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
product/product-server/src/main/java/cn/algerfan/product/server/util/JsonUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package cn.algerfan.product.server.util; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
/** | ||
* @author algerfan | ||
*/ | ||
public class JsonUtil { | ||
|
||
private static ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
/** | ||
* 转换为json字符串 | ||
* @param object | ||
* @return | ||
*/ | ||
public static String toJson(Object object) { | ||
try { | ||
return objectMapper.writeValueAsString(object); | ||
} catch (JsonProcessingException e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
eureka: | ||
client: | ||
service-url: | ||
defaultZone: http://127.0.0.1:8761/eureka/ | ||
instance: | ||
prefer-ip-address: true | ||
spring: | ||
application: | ||
name: product | ||
cloud: | ||
config: | ||
discovery: | ||
enabled: true | ||
service-id: CONFIG | ||
profile: dev |