|
@@ -1,6 +1,7 @@
|
|
|
package com.java110.core.factory;
|
|
package com.java110.core.factory;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
+import com.java110.core.context.Environment;
|
|
|
import com.java110.dto.order.OrderDto;
|
|
import com.java110.dto.order.OrderDto;
|
|
|
import com.java110.utils.constant.ServiceConstant;
|
|
import com.java110.utils.constant.ServiceConstant;
|
|
|
import com.java110.utils.factory.ApplicationContextFactory;
|
|
import com.java110.utils.factory.ApplicationContextFactory;
|
|
@@ -34,21 +35,40 @@ public class Java110TransactionalFactory {
|
|
|
//订单服务
|
|
//订单服务
|
|
|
private static final String URL_API = ServiceConstant.SERVICE_ORDER_URL + "/order/oIdApi";
|
|
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
|
|
* 创建事务ID
|
|
|
*/
|
|
*/
|
|
|
private static final String CREATE_O_ID = URL_API + "/createOId";
|
|
private static final String CREATE_O_ID = URL_API + "/createOId";
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 创建事务ID
|
|
|
|
|
+ */
|
|
|
|
|
+ private static final String BOOT_CREATE_O_ID = BOOT_URL_API + "/createOId";
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 回退事务ID
|
|
* 回退事务ID
|
|
|
*/
|
|
*/
|
|
|
private static final String FALLBACK_O_ID = URL_API + "/fallBackOId";
|
|
private static final String FALLBACK_O_ID = URL_API + "/fallBackOId";
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 回退事务ID
|
|
|
|
|
+ */
|
|
|
|
|
+ private static final String BOOT_FALLBACK_O_ID = BOOT_URL_API + "/fallBackOId";
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 回退事务ID
|
|
* 回退事务ID
|
|
|
*/
|
|
*/
|
|
|
private static final String FINISH_O_ID = URL_API + "/finishOId";
|
|
private static final String FINISH_O_ID = URL_API + "/finishOId";
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 回退事务ID
|
|
|
|
|
+ */
|
|
|
|
|
+ private static final String BOOT_FINISH_O_ID = BOOT_URL_API + "/finishOId";
|
|
|
|
|
+
|
|
|
|
|
|
|
|
//全局事务ID
|
|
//全局事务ID
|
|
|
public static final String O_ID = "O-ID";
|
|
public static final String O_ID = "O-ID";
|
|
@@ -143,6 +163,51 @@ public class Java110TransactionalFactory {
|
|
|
* @param orderDto
|
|
* @param orderDto
|
|
|
*/
|
|
*/
|
|
|
private static void createOId(OrderDto 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);
|
|
RestTemplate restTemplate = ApplicationContextFactory.getBean("restTemplate", RestTemplate.class);
|
|
|
HttpHeaders header = new HttpHeaders();
|
|
HttpHeaders header = new HttpHeaders();
|
|
|
HttpEntity<String> httpEntity = new HttpEntity<String>(JSONObject.toJSONString(orderDto), header);
|
|
HttpEntity<String> httpEntity = new HttpEntity<String>(JSONObject.toJSONString(orderDto), header);
|
|
@@ -174,6 +239,58 @@ public class Java110TransactionalFactory {
|
|
|
*/
|
|
*/
|
|
|
public static void fallbackOId() {
|
|
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();
|
|
String oId = getOId();
|
|
|
if (StringUtil.isEmpty(oId) || ROLE_OBSERVER.equals(getServiceRole())) {
|
|
if (StringUtil.isEmpty(oId) || ROLE_OBSERVER.equals(getServiceRole())) {
|
|
|
//当前没有开启事务无需回退
|
|
//当前没有开启事务无需回退
|
|
@@ -212,6 +329,56 @@ public class Java110TransactionalFactory {
|
|
|
* 处理失败,回退事务
|
|
* 处理失败,回退事务
|
|
|
*/
|
|
*/
|
|
|
public static void finishOId() {
|
|
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();
|
|
String oId = getOId();
|
|
|
if (StringUtil.isEmpty(oId) || ROLE_OBSERVER.equals(getServiceRole())) {
|
|
if (StringUtil.isEmpty(oId) || ROLE_OBSERVER.equals(getServiceRole())) {
|
|
|
//当前没有开启事务无需回退
|
|
//当前没有开启事务无需回退
|