7
7
import org .slf4j .LoggerFactory ;
8
8
import org .springframework .beans .factory .annotation .Value ;
9
9
import org .springframework .cloud .context .config .annotation .RefreshScope ;
10
+ import org .springframework .util .StringUtils ;
10
11
11
12
import java .net .InetAddress ;
12
13
import java .net .UnknownHostException ;
@@ -23,13 +24,23 @@ public class CartServiceImpl implements CartService {
23
24
24
25
private ConcurrentHashMap <String , List <CartItem >> cartStore = new ConcurrentHashMap <>();
25
26
26
- @ Value ("${exception.ip:''}" )
27
- private String exceptionIp ;
27
+ @ Value ("${exception.enable:false}" )
28
+ private String exceptionEnable ;
29
+
30
+ private String localIp = getLocalIp ();
28
31
29
32
@ Override
30
33
public List <CartItem > viewCart (String userID ) {
31
34
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 ) {
33
44
throw new RuntimeException ("runtime exception" );
34
45
}
35
46
@@ -39,7 +50,7 @@ public List<CartItem> viewCart(String userID) {
39
50
@ Override
40
51
public boolean addItemToCart (String userID , String productID , int quantity ) {
41
52
List <CartItem > itemList = cartStore .computeIfAbsent (userID , k -> new ArrayList <>());
42
- for (CartItem item : itemList ) {
53
+ for (CartItem item : itemList ) {
43
54
if (item .productID .equals (productID )) {
44
55
item .quantity ++;
45
56
return true ;
@@ -50,7 +61,7 @@ public boolean addItemToCart(String userID, String productID, int quantity) {
50
61
}
51
62
52
63
private String getLocalIp () {
53
- InetAddress inetAddress = null ;
64
+ InetAddress inetAddress = null ;
54
65
try {
55
66
inetAddress = InetAddress .getLocalHost ();
56
67
if (inetAddress != null ) {
0 commit comments