Skip to content

Commit 305d4f3

Browse files
authored
Merge branch 'master' into tx-lcn-foxdd
2 parents a8dbdb2 + 35cd6e6 commit 305d4f3

12 files changed

Lines changed: 155 additions & 25 deletions

File tree

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
"LCN并不生产事务,LCN只是本地事务的搬运工"
44

5+
## 框架介绍
6+
7+
LCN分布式事务框架是一款事务协调性的框架,框架本身并不创建事务,只是对本地事务做协调控制。因此该框架与其他第三方的框架兼容性强,支持所有的关系型数据库事务,支持多数据源,支持与第三方数据库框架一块使用(例如 sharding-jdbc),在使用框架的时候只需要添加分布式事务的注解即可,对业务的侵入性低。LCN框架主要是为微服务框架提供分布式事务的支持,在微服务框架上做了进一步的事务机制优化,在一些负载场景上LCN事务机制要比本地事务机制的性能更好,4.0以后框架开方了插件机制可以让更多的第三方框架支持进来。
8+
9+
510
## 官方网址
611

7-
[www.txlcn.org](http://www.txlcn.org)
12+
[https://www.txlcn.org](https://www.txlcn.org)
813

914

1015
## 框架特点
@@ -21,7 +26,7 @@
2126

2227
## 原理介绍
2328

24-
[原理介绍](https://github.com/codingapi/tx-lcn/wiki) [视频讲解](http://www.txlcn.org/v4/index.html)
29+
[原理介绍](https://github.com/codingapi/tx-lcn/wiki) [视频讲解](https://www.txlcn.org/v4/index.html)
2530

2631
## 目录说明
2732

transaction-dubbo/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
<dependency>
7575
<groupId>com.alibaba</groupId>
7676
<artifactId>dubbo</artifactId>
77-
<version>2.5.3</version>
77+
<version>2.5.7</version>
7878
<exclusions>
7979
<exclusion>
8080
<groupId>org.springframework</groupId>

transaction-springcloud/src/main/java/com/codingapi/ribbon/loadbalancer/LcnLoadBalancerRule.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.codingapi.ribbon.loadbalancer;
22

3+
34
import com.alibaba.fastjson.JSONObject;
5+
46
import com.codingapi.tx.aop.bean.TxTransactionLocal;
57
import com.lorne.core.framework.utils.encode.MD5Util;
68
import com.netflix.loadbalancer.Server;

transaction-springcloud/src/main/java/com/codingapi/ribbon/loadbalancer/LcnNoOpLoadBalancerProxy.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ public LcnNoOpLoadBalancerProxy(){
2525
@Override
2626
public Server chooseServer(Object key){
2727
logger.info("enter chooseServer method, key:" + key);
28+
2829
List<Server> serverList = new ArrayList<Server>();
2930
return lcnLoadBalancerRule.proxy(serverList, super.chooseServer(key));
31+
3032
}
3133

3234
}

transaction-springcloud/src/main/java/com/codingapi/ribbon/loadbalancer/LcnZoneAwareLoadBalancerProxy.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ public LcnZoneAwareLoadBalancerProxy(IClientConfig clientConfig, IRule rule,
2626
@Override
2727
public Server chooseServer(Object key){
2828
logger.info("enter chooseServer method, key:" + key);
29+
2930
List<Server> serverList = new ArrayList<Server>();
3031
//获取处理之后的serverlist
3132
serverList = super.getServerListImpl().getUpdatedListOfServers();
3233
//获取过滤之后的serverlist
3334
serverList = super.getFilter().getFilteredListOfServers(serverList);
3435
return lcnLoadBalancerRule.proxy(serverList, super.chooseServer(key));
36+
3537
}
3638

3739
}

tx-client/src/main/java/com/codingapi/tx/Constants.java

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

3-
import java.util.Map;
4-
import java.util.concurrent.ConcurrentHashMap;
5-
63
import com.codingapi.tx.model.TxServer;
74

85
/**
@@ -28,11 +25,6 @@ public class Constants {
2825
* 模块唯一标示
2926
*/
3027
public static String uniqueKey;
31-
32-
/**
33-
* 用于优化ribbon负载
34-
*/
35-
public static Map<String, Object> cacheModelInfo = new ConcurrentHashMap<>();
3628

3729

3830
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.codingapi.tx.datasource.bean;
2+
3+
/**
4+
* create by lorne on 2017/12/7
5+
*/
6+
public class LCNDataSourceLocal {
7+
8+
private final static ThreadLocal<LCNDataSourceLocal> currentLocal = new ThreadLocal<LCNDataSourceLocal>();
9+
10+
public static LCNDataSourceLocal current() {
11+
return currentLocal.get();
12+
}
13+
14+
public static void setCurrent(LCNDataSourceLocal current) {
15+
currentLocal.set(current);
16+
}
17+
18+
private String key;
19+
20+
21+
public String getKey() {
22+
return key;
23+
}
24+
25+
public void setKey(String key) {
26+
this.key = key;
27+
}
28+
29+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.codingapi.tx.datasource.relational;
2+
3+
import com.codingapi.tx.datasource.ILCNResource;
4+
5+
import java.sql.Connection;
6+
7+
/**
8+
* create by lorne on 2017/12/7
9+
*/
10+
public interface LCNConnection extends Connection,ILCNResource<Connection> {
11+
}

tx-plugins-db/src/main/java/com/codingapi/tx/datasource/relational/LCNDBConnection.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* create by lorne on 2017/7/29
2323
*/
2424

25-
public class LCNDBConnection extends AbstractTransactionThread implements Connection,ILCNResource<Connection> {
25+
public class LCNDBConnection extends AbstractTransactionThread implements LCNConnection {
2626

2727

2828
private Logger logger = LoggerFactory.getLogger(LCNDBConnection.class);
@@ -178,7 +178,9 @@ public TxTask getWaitTask() {
178178

179179
@Override
180180
public void setAutoCommit(boolean autoCommit) throws SQLException {
181-
connection.setAutoCommit(false);
181+
if(connection!=null) {
182+
connection.setAutoCommit(false);
183+
}
182184
}
183185

184186
@Override
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.codingapi.tx.datasource.relational;
2+
3+
import com.codingapi.tx.aop.bean.TxTransactionLocal;
4+
import com.codingapi.tx.datasource.bean.LCNDataSourceLocal;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
import javax.sql.DataSource;
9+
import java.sql.Connection;
10+
import java.util.Map;
11+
import java.util.concurrent.ConcurrentHashMap;
12+
13+
14+
/**
15+
* 关系型数据库动态代理连接池对象
16+
* create by lorne on 2017/7/29
17+
*/
18+
19+
public class LCNDynamicTransactionDataSource extends LCNTransactionDataSource {
20+
21+
22+
private Logger logger = LoggerFactory.getLogger(LCNDynamicTransactionDataSource.class);
23+
24+
25+
private Map<String,DataSource> dataSourceMap = new ConcurrentHashMap<>();
26+
27+
28+
private String getNowDataSourceKey(){
29+
if(LCNDataSourceLocal.current()==null){
30+
return "default";
31+
}
32+
return LCNDataSourceLocal.current().getKey();
33+
}
34+
35+
@Override
36+
public boolean hasGroup(String group) {
37+
return super.hasGroup(getNowDataSourceKey()+group);
38+
}
39+
40+
@Override
41+
protected Connection createLcnConnection(Connection connection, TxTransactionLocal txTransactionLocal) {
42+
nowCount++;
43+
if(txTransactionLocal.isHasStart()){
44+
LCNStartConnection lcnStartConnection = new LCNStartConnection(connection,subNowCount);
45+
logger.info("get new start connection - > "+txTransactionLocal.getGroupId());
46+
pools.put(getNowDataSourceKey()+txTransactionLocal.getGroupId(), lcnStartConnection);
47+
txTransactionLocal.setHasConnection(true);
48+
return lcnStartConnection;
49+
}else {
50+
LCNDBConnection lcn = new LCNDBConnection(connection, dataSourceService, subNowCount);
51+
logger.info("get new connection ->" + txTransactionLocal.getGroupId());
52+
pools.put(getNowDataSourceKey()+txTransactionLocal.getGroupId(), lcn);
53+
txTransactionLocal.setHasConnection(true);
54+
return lcn;
55+
}
56+
}
57+
58+
public void addDataSource(String key, DataSource dataSource){
59+
dataSourceMap.put(key,dataSource);
60+
logger.info("add datasource of "+key);
61+
}
62+
63+
public void setDataSource(DataSource dataSource) {
64+
super.setDataSource(dataSource);
65+
addDataSource("default",dataSource);
66+
logger.info("load default datasource.");
67+
}
68+
69+
@Override
70+
protected DataSource getDataSource() {
71+
logger.info("getDataSource--->");
72+
if(LCNDataSourceLocal.current()==null){
73+
return super.getDataSource();
74+
}else{
75+
String key = LCNDataSourceLocal.current().getKey();
76+
logger.info("get datasource of "+key);
77+
return dataSourceMap.get(key);
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)