From 4e7907983a99e695f2acba4dec8bd73b7dbe6b2b Mon Sep 17 00:00:00 2001
From: java110 <928255095@qq.com>
Date: 星期四, 29 六月 2023 14:41:33 +0800
Subject: [PATCH] optimize
---
java110-core/src/main/java/com/java110/core/factory/Java110TransactionalFactory.java | 192 +++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 190 insertions(+), 2 deletions(-)
diff --git a/java110-core/src/main/java/com/java110/core/factory/Java110TransactionalFactory.java b/java110-core/src/main/java/com/java110/core/factory/Java110TransactionalFactory.java
old mode 100644
new mode 100755
index 25fa152..54ba154
--- a/java110-core/src/main/java/com/java110/core/factory/Java110TransactionalFactory.java
+++ b/java110-core/src/main/java/com/java110/core/factory/Java110TransactionalFactory.java
@@ -1,14 +1,19 @@
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 org.slf4j.LoggerFactory;
-import org.springframework.http.*;
+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;
@@ -30,10 +35,19 @@
//璁㈠崟鏈嶅姟
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
@@ -43,7 +57,17 @@
/**
* 鍥為��浜嬪姟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
@@ -84,6 +108,7 @@
}
public static String getOrCreateOId(OrderDto orderDto) {
+
//鍏ㄥ眬浜嬪姟寮�鍚��
if (StringUtils.isEmpty(orderDto.getoId())) {
createOId(orderDto);
@@ -94,6 +119,21 @@
//灏嗕簨鍔D 瀛樻斁璧锋潵
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);
+ }
+
}
/**
@@ -123,6 +163,51 @@
* @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"));
+ //灏嗕簨鍔D 瀛樻斁璧锋潵
+ 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);
@@ -153,6 +238,59 @@
* 澶勭悊澶辫触锛屽洖閫�浜嬪姟
*/
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())) {
//褰撳墠娌℃湁寮�鍚簨鍔℃棤闇�鍥為��
@@ -191,6 +329,56 @@
* 澶勭悊澶辫触锛屽洖閫�浜嬪姟
*/
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())) {
//褰撳墠娌℃湁寮�鍚簨鍔℃棤闇�鍥為��
--
Gitblit v1.8.0