Skip to content

Commit 79aa206

Browse files
committed
to #8
1 parent e1c9d20 commit 79aa206

File tree

1 file changed

+16
-5
lines changed
  • src/cartservice/cartservice-provider/src/main/java/com/alibabacloud/hipstershop/provider

1 file changed

+16
-5
lines changed

src/cartservice/cartservice-provider/src/main/java/com/alibabacloud/hipstershop/provider/CartServiceImpl.java

+16-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.slf4j.LoggerFactory;
88
import org.springframework.beans.factory.annotation.Value;
99
import org.springframework.cloud.context.config.annotation.RefreshScope;
10+
import org.springframework.util.StringUtils;
1011

1112
import java.net.InetAddress;
1213
import java.net.UnknownHostException;
@@ -23,13 +24,23 @@ public class CartServiceImpl implements CartService {
2324

2425
private ConcurrentHashMap<String, List<CartItem>> cartStore = new ConcurrentHashMap<>();
2526

26-
@Value("${exception.ip:''}")
27-
private String exceptionIp;
27+
@Value("${exception.enable:false}")
28+
private String exceptionEnable;
29+
30+
private String localIp = getLocalIp();
2831

2932
@Override
3033
public List<CartItem> viewCart(String userID) {
3134

32-
if (exceptionIp != null && exceptionIp.equals(getLocalIp())) {
35+
int code = 0;
36+
37+
if (StringUtils.endsWithIgnoreCase("true", exceptionEnable)) {
38+
if (!StringUtils.isEmpty(localIp)) {
39+
code = localIp.hashCode();
40+
}
41+
}
42+
43+
if (code % 2 == 1) {
3344
throw new RuntimeException("runtime exception");
3445
}
3546

@@ -39,7 +50,7 @@ public List<CartItem> viewCart(String userID) {
3950
@Override
4051
public boolean addItemToCart(String userID, String productID, int quantity) {
4152
List<CartItem> itemList = cartStore.computeIfAbsent(userID, k -> new ArrayList<>());
42-
for (CartItem item: itemList) {
53+
for (CartItem item : itemList) {
4354
if (item.productID.equals(productID)) {
4455
item.quantity++;
4556
return true;
@@ -50,7 +61,7 @@ public boolean addItemToCart(String userID, String productID, int quantity) {
5061
}
5162

5263
private String getLocalIp() {
53-
InetAddress inetAddress= null;
64+
InetAddress inetAddress = null;
5465
try {
5566
inetAddress = InetAddress.getLocalHost();
5667
if (inetAddress != null) {

0 commit comments

Comments
 (0)