Skip to content

Commit ab34c25

Browse files
authored
Merge pull request #3 from codingapi/tx-lcn-motan
Tx lcn motan
2 parents 0d888cd + f332a4a commit ab34c25

7 files changed

Lines changed: 335 additions & 5 deletions

File tree

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<module>tx-plugins-db</module>
2020
<module>tx-plugins-nodb</module>
2121
<module>tx-plugins-redis</module>
22+
<module>transaction-motan</module>
2223
</modules>
2324

2425
<properties>

transaction-dubbo/src/main/java/com/codingapi/tx/dubbo/service/impl/ModelNameServiceImpl.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class ModelNameServiceImpl implements ModelNameService {
1818

1919

2020
@Autowired
21-
private ApplicationConfig applicationConfig;
21+
private ApplicationConfig applicationConfig;
2222

2323
@Autowired
2424
private ProviderConfig providerConfig;
@@ -28,11 +28,11 @@ public class ModelNameServiceImpl implements ModelNameService {
2828

2929
@Override
3030
public String getModelName() {
31-
return applicationConfig.getName();
31+
return applicationConfig.getName();
3232
}
3333

3434

35-
private String getIp(){
35+
private String getIp() {
3636
if (host == null) {
3737
try {
3838
host = InetAddress.getLocalHost().getHostAddress();
@@ -45,8 +45,8 @@ private String getIp(){
4545

4646
@Override
4747
public String getUniqueKey() {
48-
String address = getIp()+providerConfig.getPort();
49-
return MD5Util.md5(address.getBytes());
48+
String address = getIp() + providerConfig.getPort();
49+
return MD5Util.md5(address.getBytes());
5050
}
5151

5252

transaction-motan/pom.xml

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
8+
<artifactId>transaction-motan</artifactId>
9+
<groupId>com.codingapi</groupId>
10+
<version>1.0.0</version>
11+
12+
<name>transaction-motan</name>
13+
14+
<description>transaction-dubbo project for Spring Boot</description>
15+
16+
17+
<licenses>
18+
<license>
19+
<name>The Apache License, Version 2.0</name>
20+
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
21+
</license>
22+
</licenses>
23+
<developers>
24+
<developer>
25+
<name>michael</name>
26+
<email>zfvip_it@163.com</email>
27+
<roles>
28+
<role>developer</role>
29+
</roles>
30+
<timezone>+8</timezone>
31+
</developer>
32+
</developers>
33+
34+
35+
<properties>
36+
<maven.compile.source>1.7</maven.compile.source>
37+
<maven.compile.target>1.7</maven.compile.target>
38+
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
39+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
40+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
41+
<java.version>1.7</java.version>
42+
<motan>0.3.1</motan>
43+
</properties>
44+
45+
<dependencies>
46+
<dependency>
47+
<groupId>com.codingapi</groupId>
48+
<artifactId>tx-client</artifactId>
49+
<version>1.0.0</version>
50+
</dependency>
51+
52+
<!-- motan config start-->
53+
<dependency>
54+
<groupId>com.weibo</groupId>
55+
<artifactId>motan-core</artifactId>
56+
<version>${motan}</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>com.weibo</groupId>
60+
<artifactId>motan-transport-netty</artifactId>
61+
<version>${motan}</version>
62+
</dependency>
63+
64+
<!-- only needed for spring-based features -->
65+
<dependency>
66+
<groupId>com.weibo</groupId>
67+
<artifactId>motan-springsupport</artifactId>
68+
<version>${motan}</version>
69+
</dependency>
70+
71+
<dependency>
72+
<groupId>com.weibo</groupId>
73+
<artifactId>motan-registry-zookeeper</artifactId>
74+
<version>${motan}</version>
75+
</dependency>
76+
<!-- motan config end-->
77+
</dependencies>
78+
79+
<build>
80+
<plugins>
81+
<plugin>
82+
<groupId>org.apache.maven.plugins</groupId>
83+
<artifactId>maven-compiler-plugin</artifactId>
84+
<configuration>
85+
<source>1.7</source>
86+
<target>1.7</target>
87+
</configuration>
88+
</plugin>
89+
<plugin>
90+
<groupId>org.apache.maven.plugins</groupId>
91+
<artifactId>maven-compiler-plugin</artifactId>
92+
<version>${maven-compiler-plugin.version}</version>
93+
<configuration>
94+
<source>${maven.compile.source}</source>
95+
<target>${maven.compile.target}</target>
96+
<encoding>${project.build.sourceEncoding}</encoding>
97+
</configuration>
98+
</plugin>
99+
<plugin>
100+
<groupId>org.apache.maven.plugins</groupId>
101+
<artifactId>maven-source-plugin</artifactId>
102+
<version>2.4</version>
103+
<executions>
104+
<execution>
105+
<id>attach-sources</id>
106+
<goals>
107+
<goal>jar-no-fork</goal>
108+
</goals>
109+
</execution>
110+
</executions>
111+
</plugin>
112+
<plugin>
113+
<groupId>org.sonatype.plugins</groupId>
114+
<artifactId>nexus-staging-maven-plugin</artifactId>
115+
<version>1.6.3</version>
116+
<extensions>true</extensions>
117+
<configuration>
118+
<serverId>ossrh</serverId>
119+
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
120+
<autoReleaseAfterClose>true</autoReleaseAfterClose>
121+
</configuration>
122+
</plugin>
123+
<plugin>
124+
<groupId>org.apache.maven.plugins</groupId>
125+
<artifactId>maven-javadoc-plugin</artifactId>
126+
<version>2.10.3</version>
127+
<executions>
128+
<execution>
129+
<id>attach-javadocs</id>
130+
<goals>
131+
<goal>jar</goal>
132+
</goals>
133+
</execution>
134+
</executions>
135+
</plugin>
136+
<plugin>
137+
<groupId>org.apache.maven.plugins</groupId>
138+
<artifactId>maven-gpg-plugin</artifactId>
139+
<version>1.6</version>
140+
<executions>
141+
<execution>
142+
<id>sign-artifacts</id>
143+
<phase>verify</phase>
144+
<goals>
145+
<goal>sign</goal>
146+
</goals>
147+
</execution>
148+
</executions>
149+
</plugin>
150+
</plugins>
151+
</build>
152+
153+
</project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.codingapi.tx.motan.filter;
2+
3+
import com.codingapi.tx.aop.bean.TxTransactionLocal;
4+
import com.weibo.api.motan.common.MotanConstants;
5+
import com.weibo.api.motan.core.extension.Activation;
6+
import com.weibo.api.motan.core.extension.SpiMeta;
7+
import com.weibo.api.motan.filter.Filter;
8+
import com.weibo.api.motan.rpc.Caller;
9+
import com.weibo.api.motan.rpc.Request;
10+
import com.weibo.api.motan.rpc.Response;
11+
import com.weibo.api.motan.rpc.RpcContext;
12+
13+
import java.util.Map;
14+
15+
/**
16+
* <p>motan拦截器</p>
17+
*
18+
* @author 张峰 zfvip_it@163.com
19+
* @createTime: 2017/11/17 15:38
20+
*/
21+
@SpiMeta(name = "transaction")
22+
@Activation(key = {MotanConstants.NODE_TYPE_SERVICE, MotanConstants.NODE_TYPE_REFERER})
23+
public class TransactionFilter implements Filter {
24+
25+
public Response filter(Caller<?> caller, Request request) {
26+
TxTransactionLocal txTransactionLocal = TxTransactionLocal.current();
27+
if (txTransactionLocal != null) {
28+
request.setAttachment("tx-group", txTransactionLocal.getGroupId());
29+
request.setAttachment("tx-maxTimeOut", String.valueOf(txTransactionLocal.getMaxTimeOut()));
30+
} else {
31+
Map<String, String> map = request.getAttachments();
32+
if (map != null && !map.isEmpty()) {
33+
if (map.containsKey("tx-group")) {
34+
RpcContext.getContext().putAttribute("tx-group", request.getAttachments().get("tx-group"));
35+
}
36+
if (map.containsKey("tx-maxTimeOut")) {
37+
RpcContext.getContext().putAttribute("tx-maxTimeOut", request.getAttachments().get("tx-maxTimeOut"));
38+
}
39+
}
40+
}
41+
return caller.call(request);
42+
}
43+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.codingapi.tx.motan.interceptor;
2+
3+
import com.codingapi.tx.aop.service.AspectBeforeService;
4+
import com.weibo.api.motan.rpc.RpcContext;
5+
import org.aspectj.lang.ProceedingJoinPoint;
6+
import org.springframework.stereotype.Component;
7+
8+
import javax.annotation.Resource;
9+
10+
/**
11+
* <p>类说明</p>
12+
*
13+
* @author 张峰 zfvip_it@163.com
14+
* @createTime: 2017/11/17 15:42
15+
*/
16+
@Component
17+
public class TxManagerInterceptor {
18+
19+
@Resource
20+
private AspectBeforeService aspectBeforeService;
21+
22+
23+
public Object around(ProceedingJoinPoint point) throws Throwable {
24+
25+
String groupId = null;
26+
int maxTimeOut = 0;
27+
try {
28+
groupId = (String) RpcContext.getContext().getAttribute("tx-group");
29+
maxTimeOut = Integer.parseInt(RpcContext.getContext().getAttribute("tx-maxTimeOut").toString());
30+
} catch (Exception e) {
31+
}
32+
return aspectBeforeService.around(groupId, maxTimeOut, point);
33+
}
34+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.codingapi.tx.motan.service.impl;
2+
3+
import com.codingapi.tx.listener.service.ModelNameService;
4+
import com.lorne.core.framework.utils.encode.MD5Util;
5+
import com.weibo.api.motan.config.springsupport.BasicServiceConfigBean;
6+
import org.apache.commons.lang.StringUtils;
7+
import org.springframework.core.env.Environment;
8+
import org.springframework.stereotype.Service;
9+
10+
import javax.annotation.Resource;
11+
import java.net.InetAddress;
12+
import java.net.UnknownHostException;
13+
14+
/**
15+
* <p>类说明</p>
16+
*
17+
* @author 张峰 zfvip_it@163.com
18+
* @createTime: 2017/11/17 13:30
19+
*/
20+
@Service
21+
public class ModelNameServiceImpl implements ModelNameService {
22+
23+
@Resource
24+
private BasicServiceConfigBean basicServiceConfigBean;
25+
26+
@Resource
27+
private Environment environment;
28+
29+
private String host = null;
30+
31+
public String getModelName() {
32+
return environment.getProperty("tx.application");
33+
}
34+
35+
public String getUniqueKey() {
36+
String address = getIp() + getPort();
37+
return MD5Util.md5(address.getBytes());
38+
}
39+
40+
public String getIpAddress() {
41+
String address = getIp() + ":" + getPort();
42+
return address;
43+
}
44+
45+
private String getIp() {
46+
if (host == null) {
47+
try {
48+
host = InetAddress.getLocalHost().getHostAddress();
49+
} catch (UnknownHostException e) {
50+
e.printStackTrace();
51+
}
52+
}
53+
return host;
54+
}
55+
56+
private String getPort() {
57+
String export = basicServiceConfigBean.getExport();
58+
if (StringUtils.isNotBlank(export)) {
59+
return export.split(":")[1];
60+
}
61+
return null;
62+
}
63+
64+
/**
65+
* 超时时间
66+
*
67+
* @return
68+
*/
69+
public String getTimeOut() {
70+
return basicServiceConfigBean.getRequestTimeout().toString();
71+
}
72+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.codingapi.tx.motan.service.impl;
2+
3+
import com.codingapi.tx.Constants;
4+
import com.codingapi.tx.listener.service.TimeOutService;
5+
import com.weibo.api.motan.config.springsupport.BasicServiceConfigBean;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.stereotype.Service;
8+
9+
/**
10+
* <p>类说明</p>
11+
*
12+
* @author 张峰 zfvip_it@163.com
13+
* @createTime: 2017/11/17 13:30
14+
*/
15+
@Service
16+
public class TimeOutServiceImpl implements TimeOutService {
17+
18+
19+
@Autowired
20+
private BasicServiceConfigBean basicServiceConfigBean;
21+
22+
23+
public void loadOutTime() {
24+
int timeOut = basicServiceConfigBean.getRequestTimeout();
25+
Constants.maxOutTime = timeOut;
26+
}
27+
}

0 commit comments

Comments
 (0)