java110
2023-05-23 cca504b84583f8f8289b34e794939b8590a16446
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package com.java110.core.factory;
 
import com.alibaba.fastjson.JSONObject;
import com.java110.core.context.Environment;
import com.java110.dto.order.OrderDto;
import com.java110.utils.constant.ServiceConstant;
import com.java110.utils.factory.ApplicationContextFactory;
import com.java110.utils.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import com.java110.core.log.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RestTemplate;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * @ClassName Java110TransactionalFactory
 * @Description TODO
 * @Author wuxw
 * @Date 2020/7/3 22:30
 * @Version 1.0
 * add by wuxw 2020/7/3
 **/
public class Java110TransactionalFactory {
 
    private static Logger logger = LoggerFactory.getLogger(Java110TransactionalFactory.class);
 
    //订单服务
    private static final String URL_API = ServiceConstant.SERVICE_ORDER_URL + "/order/oIdApi";
 
    private static final String BOOT_URL_API = "http://127.0.0.1:8008/order/oIdApi";
 
    /**
     * 创建事务ID
     */
    private static final String CREATE_O_ID = URL_API + "/createOId";
 
 
    /**
     * 创建事务ID
     */
    private static final String BOOT_CREATE_O_ID = BOOT_URL_API + "/createOId";
 
 
    /**
     * 回退事务ID
     */
    private static final String FALLBACK_O_ID = URL_API + "/fallBackOId";
 
    /**
     * 回退事务ID
     */
    private static final String BOOT_FALLBACK_O_ID = BOOT_URL_API + "/fallBackOId";
 
    /**
     * 回退事务ID
     */
    private static final String FINISH_O_ID = URL_API + "/finishOId";
 
    /**
     * 回退事务ID
     */
    private static final String BOOT_FINISH_O_ID = BOOT_URL_API + "/finishOId";
 
 
    //全局事务ID
    public static final String O_ID = "O-ID";
 
    //当前服务ID
    public static final String SERVICE_ID = "SERVICE-ID";
 
    //服务角色 创建事务者 还是 观察者
    public static final String SERVICE_ROLE = "SERVICE-ROLE";
 
    //创建者
    public static final String ROLE_CREATE = "creator";
 
    public static final String ROLE_OBSERVER = "observer";
 
    private static ThreadLocal<Map<String, String>> threadLocal = new ThreadLocal<Map<String, String>>() {
        @Override
        protected Map<String, String> initialValue() {
            return new HashMap<String, String>();
        }
    };
 
    public static String put(String key, String value) {
        return threadLocal.get().put(key, value);
    }
 
    public static String get(String key) {
        return threadLocal.get().get(key);
    }
 
    public static String remove(String key) {
        return threadLocal.get().remove(key);
    }
 
    public static Map<String, String> entries() {
        return threadLocal.get();
    }
 
    public static String getOrCreateOId(OrderDto orderDto) {
 
        //全局事务开启者
        if (StringUtils.isEmpty(orderDto.getoId())) {
            createOId(orderDto);
            put(SERVICE_ROLE, ROLE_CREATE);
        } else {
            put(SERVICE_ROLE, ROLE_OBSERVER);//观察者
        }
        //将事务ID 存放起来
        put(O_ID, orderDto.getoId());
        return orderDto.getoId();
    }
 
    /**
     * 清理事务
     */
    public static void clearOId() {
        //清理事务
        if(!StringUtil.isEmpty(getOId())) {
            remove(O_ID);
        }
        //清理角色
        if(!StringUtil.isEmpty(getServiceRole())) {
            remove(SERVICE_ROLE);
        }
 
    }
 
    /**
     * 获取事务ID
     *
     * @return
     */
    public static String getOId() {
        String oId = get(O_ID);
        return oId;
    }
 
 
    /**
     * 获取事务ID
     *
     * @return
     */
    public static String getServiceRole() {
        String oId = get(SERVICE_ROLE);
        return oId;
    }
 
    /**
     * 创建事务
     *
     * @param orderDto
     */
    private static void createOId(OrderDto orderDto) {
        if(Environment.isStartBootWay()){
            createOIdByBoot(orderDto);
        }else{
            createOIdByCloud(orderDto);
        }
    }
 
    /**
     * 创建事务
     *
     * @param orderDto
     */
    private static void createOIdByBoot(OrderDto orderDto) {
        RestTemplate restTemplate = ApplicationContextFactory.getBean("outRestTemplate", RestTemplate.class);
        HttpHeaders header = new HttpHeaders();
        HttpEntity<String> httpEntity = new HttpEntity<String>(JSONObject.toJSONString(orderDto), header);
        ResponseEntity<String> responseEntity = null;
        try {
            responseEntity = restTemplate.exchange(BOOT_CREATE_O_ID, HttpMethod.POST, httpEntity, String.class);
        } catch (HttpStatusCodeException e) { //这里spring 框架 在4XX 或 5XX 时抛出 HttpServerErrorException 异常,需要重新封装一下
            responseEntity = new ResponseEntity<String>(e.getResponseBodyAsString(), e.getStatusCode());
        } catch (Exception e) {
            responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        } finally {
            logger.debug("请求地址为,{} 请求订单服务信息,{},订单服务返回信息,{}", BOOT_CREATE_O_ID, httpEntity, responseEntity);
        }
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            throw new IllegalArgumentException("创建事务失败" + responseEntity.getBody());
        }
        JSONObject order = JSONObject.parseObject(responseEntity.getBody());
 
        if (!order.containsKey("oId") || StringUtils.isEmpty(order.getString("oId"))) {
            throw new IllegalArgumentException("创建事务失败" + responseEntity.getBody());
        }
        orderDto.setoId(order.getString("oId"));
        //将事务ID 存放起来
        put(O_ID, orderDto.getoId());
    }
 
    /**
     * 创建事务
     *
     * @param orderDto
     */
    private static void createOIdByCloud(OrderDto orderDto) {
        RestTemplate restTemplate = ApplicationContextFactory.getBean("restTemplate", RestTemplate.class);
        HttpHeaders header = new HttpHeaders();
        HttpEntity<String> httpEntity = new HttpEntity<String>(JSONObject.toJSONString(orderDto), header);
        ResponseEntity<String> responseEntity = null;
        try {
            responseEntity = restTemplate.exchange(CREATE_O_ID, HttpMethod.POST, httpEntity, String.class);
        } catch (HttpStatusCodeException e) { //这里spring 框架 在4XX 或 5XX 时抛出 HttpServerErrorException 异常,需要重新封装一下
            responseEntity = new ResponseEntity<String>(e.getResponseBodyAsString(), e.getStatusCode());
        } catch (Exception e) {
            responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        } finally {
            logger.debug("请求地址为,{} 请求订单服务信息,{},订单服务返回信息,{}", CREATE_O_ID, httpEntity, responseEntity);
        }
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            throw new IllegalArgumentException("创建事务失败" + responseEntity.getBody());
        }
        JSONObject order = JSONObject.parseObject(responseEntity.getBody());
 
        if (!order.containsKey("oId") || StringUtils.isEmpty(order.getString("oId"))) {
            throw new IllegalArgumentException("创建事务失败" + responseEntity.getBody());
        }
        orderDto.setoId(order.getString("oId"));
        //将事务ID 存放起来
        put(O_ID, orderDto.getoId());
    }
 
    /**
     * 处理失败,回退事务
     */
    public static void fallbackOId() {
 
        if(Environment.isStartBootWay()){
            fallbackOIdByBoot();
        }else{
            fallbackOIdByCloud();
        }
 
    }
 
    /**
     * 处理失败,回退事务
     */
    public static void fallbackOIdByBoot() {
 
        String oId = getOId();
        if (StringUtil.isEmpty(oId) || ROLE_OBSERVER.equals(getServiceRole())) {
            //当前没有开启事务无需回退
            logger.debug("当前没有开启事务无需回退");
            return;
        }
        OrderDto orderDto = new OrderDto();
        orderDto.setoId(oId);
        RestTemplate restTemplate = ApplicationContextFactory.getBean("outRestTemplate", RestTemplate.class);
        HttpHeaders header = new HttpHeaders();
        HttpEntity<String> httpEntity = new HttpEntity<String>(JSONObject.toJSONString(orderDto), header);
        ResponseEntity<String> responseEntity = null;
        try {
            responseEntity = restTemplate.exchange(BOOT_FALLBACK_O_ID, HttpMethod.POST, httpEntity, String.class);
        } catch (HttpStatusCodeException e) { //这里spring 框架 在4XX 或 5XX 时抛出 HttpServerErrorException 异常,需要重新封装一下
            responseEntity = new ResponseEntity<String>(e.getResponseBodyAsString(), e.getStatusCode());
        } catch (Exception e) {
            responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        } finally {
            logger.debug("回退事务 请求地址为,{} 请求订单服务信息,{},订单服务返回信息,{}", FALLBACK_O_ID, httpEntity, responseEntity);
        }
 
        if (responseEntity.getStatusCode() == HttpStatus.NOT_FOUND) {
            //当前没有开启事务无需回退
            logger.debug("当前没有开启事务无需回退");
            return;
        }
 
        if (responseEntity.getStatusCode() == HttpStatus.BAD_REQUEST) {
            throw new IllegalArgumentException("回退事务失败" + responseEntity.getBody());
        }
 
    }
 
    /**
     * 处理失败,回退事务
     */
    public static void fallbackOIdByCloud() {
 
        String oId = getOId();
        if (StringUtil.isEmpty(oId) || ROLE_OBSERVER.equals(getServiceRole())) {
            //当前没有开启事务无需回退
            logger.debug("当前没有开启事务无需回退");
            return;
        }
        OrderDto orderDto = new OrderDto();
        orderDto.setoId(oId);
        RestTemplate restTemplate = ApplicationContextFactory.getBean("restTemplate", RestTemplate.class);
        HttpHeaders header = new HttpHeaders();
        HttpEntity<String> httpEntity = new HttpEntity<String>(JSONObject.toJSONString(orderDto), header);
        ResponseEntity<String> responseEntity = null;
        try {
            responseEntity = restTemplate.exchange(FALLBACK_O_ID, HttpMethod.POST, httpEntity, String.class);
        } catch (HttpStatusCodeException e) { //这里spring 框架 在4XX 或 5XX 时抛出 HttpServerErrorException 异常,需要重新封装一下
            responseEntity = new ResponseEntity<String>(e.getResponseBodyAsString(), e.getStatusCode());
        } catch (Exception e) {
            responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        } finally {
            logger.debug("回退事务 请求地址为,{} 请求订单服务信息,{},订单服务返回信息,{}", FALLBACK_O_ID, httpEntity, responseEntity);
        }
 
        if (responseEntity.getStatusCode() == HttpStatus.NOT_FOUND) {
            //当前没有开启事务无需回退
            logger.debug("当前没有开启事务无需回退");
            return;
        }
 
        if (responseEntity.getStatusCode() == HttpStatus.BAD_REQUEST) {
            throw new IllegalArgumentException("回退事务失败" + responseEntity.getBody());
        }
 
    }
 
    /**
     * 处理失败,回退事务
     */
    public static void finishOId() {
        if(Environment.isStartBootWay()){
            finishOIdByBoot();
        }else{
            finishOIdByCloud();
        }
 
    }
 
    /**
     * 处理失败,回退事务
     */
    public static void finishOIdByBoot() {
        String oId = getOId();
        if (StringUtil.isEmpty(oId) || ROLE_OBSERVER.equals(getServiceRole())) {
            //当前没有开启事务无需回退
            logger.debug("当前没有开启事务无需回退");
            return;
        }
        OrderDto orderDto = new OrderDto();
        orderDto.setoId(oId);
        RestTemplate restTemplate = ApplicationContextFactory.getBean("outRestTemplate", RestTemplate.class);
        HttpHeaders header = new HttpHeaders();
        HttpEntity<String> httpEntity = new HttpEntity<String>(JSONObject.toJSONString(orderDto), header);
        ResponseEntity<String> responseEntity = null;
        try {
            responseEntity = restTemplate.exchange(BOOT_FINISH_O_ID, HttpMethod.POST, httpEntity, String.class);
        } catch (HttpStatusCodeException e) { //这里spring 框架 在4XX 或 5XX 时抛出 HttpServerErrorException 异常,需要重新封装一下
            responseEntity = new ResponseEntity<String>(e.getResponseBodyAsString(), e.getStatusCode());
        } catch (Exception e) {
            responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        } finally {
            logger.debug("完成事务 请求地址为,{} 请求订单服务信息,{},订单服务返回信息,{}", BOOT_FINISH_O_ID, httpEntity, responseEntity);
        }
 
        if (responseEntity.getStatusCode() == HttpStatus.NOT_FOUND) {
            //当前没有开启事务无需回退
            logger.debug("当前没有开启事务无需处理");
            return;
        }
 
        if (responseEntity.getStatusCode() == HttpStatus.BAD_REQUEST) {
            throw new IllegalArgumentException("完成事务失败" + responseEntity.getBody());
        }
 
    }
 
    /**
     * 处理失败,回退事务
     */
    public static void finishOIdByCloud() {
        String oId = getOId();
        if (StringUtil.isEmpty(oId) || ROLE_OBSERVER.equals(getServiceRole())) {
            //当前没有开启事务无需回退
            logger.debug("当前没有开启事务无需回退");
            return;
        }
        OrderDto orderDto = new OrderDto();
        orderDto.setoId(oId);
        RestTemplate restTemplate = ApplicationContextFactory.getBean("restTemplate", RestTemplate.class);
        HttpHeaders header = new HttpHeaders();
        HttpEntity<String> httpEntity = new HttpEntity<String>(JSONObject.toJSONString(orderDto), header);
        ResponseEntity<String> responseEntity = null;
        try {
            responseEntity = restTemplate.exchange(FINISH_O_ID, HttpMethod.POST, httpEntity, String.class);
        } catch (HttpStatusCodeException e) { //这里spring 框架 在4XX 或 5XX 时抛出 HttpServerErrorException 异常,需要重新封装一下
            responseEntity = new ResponseEntity<String>(e.getResponseBodyAsString(), e.getStatusCode());
        } catch (Exception e) {
            responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        } finally {
            logger.debug("完成事务 请求地址为,{} 请求订单服务信息,{},订单服务返回信息,{}", FINISH_O_ID, httpEntity, responseEntity);
        }
 
        if (responseEntity.getStatusCode() == HttpStatus.NOT_FOUND) {
            //当前没有开启事务无需回退
            logger.debug("当前没有开启事务无需处理");
            return;
        }
 
        if (responseEntity.getStatusCode() == HttpStatus.BAD_REQUEST) {
            throw new IllegalArgumentException("完成事务失败" + responseEntity.getBody());
        }
 
    }
}