Skip to content

Commit 1433c5b

Browse files
committed
优化txUrl 和 TxManager请求方式,提供api方式自定义修改
1 parent 27fd4c5 commit 1433c5b

4 files changed

Lines changed: 55 additions & 45 deletions

File tree

tx-client/src/main/java/com/codingapi/tx/config/ConfigReader.java

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.codingapi.tx.config;
22

3+
import com.codingapi.tx.config.service.TxManagerTxUrlService;
34
import com.lorne.core.framework.utils.config.ConfigUtils;
45
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.context.ApplicationContext;
69
import org.springframework.stereotype.Component;
710

811
/**
@@ -11,60 +14,43 @@
1114
@Component
1215
public class ConfigReader {
1316

14-
private String txUrl;
15-
16-
private String configName = "tx.properties";
17-
18-
private String configKey = "url";
1917

2018
private Logger logger = LoggerFactory.getLogger(ConfigReader.class);
2119

2220

23-
public ConfigReader() {
24-
loadConfig();
25-
}
21+
private TxManagerTxUrlService txManagerTxUrlService;
2622

27-
private void loadConfig(){
28-
try {
29-
txUrl = ConfigUtils.getString(configName, configKey);
23+
@Autowired
24+
private ApplicationContext spring;
3025

31-
//兼容3.0的配置地址
32-
if (txUrl.contains("/tx/manager/getServer")) {
33-
txUrl = txUrl.replace("getServer", "");
34-
}
3526

36-
//添加后缀/
37-
if (!txUrl.endsWith("/")){
38-
txUrl+="/";
39-
}
27+
public String getTxUrl() {
4028

29+
try {
30+
txManagerTxUrlService = spring.getBean(TxManagerTxUrlService.class);
4131
}catch (Exception e){
4232
logger.error(e.getLocalizedMessage());
4333
}
44-
}
4534

46-
/**
47-
* 重新设置配置文件名称
48-
* @param configName 配置文件名称
49-
* @param key 配置文件key值
50-
*/
51-
public void setConfigName(String configName,String key) {
52-
this.configName = configName;
53-
this.configKey = key;
54-
loadConfig();
55-
}
35+
if(txManagerTxUrlService == null){
36+
txManagerTxUrlService = new TxManagerTxUrlService() {
5637

57-
/**
58-
* 设置TxManager服务地址 格式如 http://127.0.0.1:8899/tx/manager/
59-
* @param txUrl
60-
*/
61-
public void setTxUrl(String txUrl) {
62-
this.txUrl = txUrl;
63-
}
38+
private final String configName = "tx.properties";
6439

40+
private final String configKey = "url";
6541

66-
public String getTxUrl() {
67-
return txUrl;
42+
@Override
43+
public String getTxUrl() {
44+
return ConfigUtils.getString(configName,configKey);
45+
}
46+
};
47+
48+
logger.info("load default txManagerTxUrlService");
49+
}else{
50+
logger.info("load txManagerTxUrlService");
51+
}
52+
53+
return txManagerTxUrlService.getTxUrl();
6854
}
6955

7056

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.codingapi.tx.config.service;
2+
3+
/**
4+
* create by lorne on 2017/11/18
5+
*/
6+
public interface TxManagerTxUrlService {
7+
8+
String getTxUrl();
9+
}

tx-client/src/main/java/com/codingapi/tx/netty/service/TxManagerHttpRequestHelper.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33

44
import com.lorne.core.framework.utils.http.HttpUtils;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.context.ApplicationContext;
59
import org.springframework.stereotype.Component;
610

711
/**
@@ -11,15 +15,23 @@
1115
public class TxManagerHttpRequestHelper {
1216

1317

14-
private HttpRequestService httpRequestService;
18+
private TxManagerHttpRequestService httpRequestService;
19+
20+
@Autowired
21+
private ApplicationContext spring;
22+
23+
private Logger logger = LoggerFactory.getLogger(TxManagerHttpRequestHelper.class);
1524

16-
public void setHttpRequestService(HttpRequestService httpRequestService) {
17-
this.httpRequestService = httpRequestService;
18-
}
1925

2026
private void reloadHttpRequestService(){
27+
try {
28+
httpRequestService = spring.getBean(TxManagerHttpRequestService.class);
29+
}catch (Exception e){
30+
logger.error(e.getLocalizedMessage());
31+
}
32+
2133
if(httpRequestService==null){
22-
httpRequestService = new HttpRequestService() {
34+
httpRequestService = new TxManagerHttpRequestService() {
2335
@Override
2436
public String httpGet(String url) {
2537
return HttpUtils.get(url);
@@ -30,6 +42,9 @@ public String httpPost(String url, String params) {
3042
return HttpUtils.post(url, params);
3143
}
3244
};
45+
logger.info("load default HttpRequestService .");
46+
}else {
47+
logger.info("load HttpRequestService .");
3348
}
3449
}
3550

tx-client/src/main/java/com/codingapi/tx/netty/service/HttpRequestService.java renamed to tx-client/src/main/java/com/codingapi/tx/netty/service/TxManagerHttpRequestService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* create by lorne on 2017/11/17
55
*/
6-
public interface HttpRequestService {
6+
public interface TxManagerHttpRequestService {
77

88
String httpGet(String url);
99

0 commit comments

Comments
 (0)