Skip to content

Commit 6279c8d

Browse files
chidaodezhongshengwu-sheng
authored andcommitted
support shardingjdbc database storage feature (apache#1347)
* support shardingjdbc database storage feature
1 parent cab4244 commit 6279c8d

File tree

243 files changed

+14086
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

243 files changed

+14086
-0
lines changed

apm-collector/apm-collector-boot/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@
146146
<artifactId>collector-storage-h2-provider</artifactId>
147147
<version>${project.version}</version>
148148
</dependency>
149+
<dependency>
150+
<groupId>org.apache.skywalking</groupId>
151+
<artifactId>collector-storage-shardingjdbc-provider</artifactId>
152+
<version>${project.version}</version>
153+
</dependency>
149154
<!-- storage provider -->
150155
<!-- remote provider -->
151156
<dependency>

apm-collector/apm-collector-component/client-component/pom.xml

+8
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,13 @@
101101
</exclusion>
102102
</exclusions>
103103
</dependency>
104+
<dependency>
105+
<groupId>io.shardingjdbc</groupId>
106+
<artifactId>sharding-jdbc-core</artifactId>
107+
</dependency>
108+
<dependency>
109+
<groupId>commons-dbcp</groupId>
110+
<artifactId>commons-dbcp</artifactId>
111+
</dependency>
104112
</dependencies>
105113
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
20+
package org.apache.skywalking.apm.collector.client.shardingjdbc;
21+
22+
import java.sql.Connection;
23+
import java.sql.PreparedStatement;
24+
import java.sql.ResultSet;
25+
import java.sql.SQLException;
26+
import java.sql.Statement;
27+
import java.util.HashMap;
28+
import java.util.Map;
29+
import java.util.Properties;
30+
31+
import javax.sql.DataSource;
32+
33+
import org.apache.commons.dbcp.BasicDataSource;
34+
import org.apache.skywalking.apm.collector.client.Client;
35+
import org.slf4j.Logger;
36+
import org.slf4j.LoggerFactory;
37+
38+
import io.shardingjdbc.core.api.ShardingDataSourceFactory;
39+
import io.shardingjdbc.core.api.config.ShardingRuleConfiguration;
40+
41+
/**
42+
* @author linjiaqi
43+
*/
44+
public class ShardingjdbcClient implements Client {
45+
46+
private final Logger logger = LoggerFactory.getLogger(ShardingjdbcClient.class);
47+
48+
private Map<String, ShardingjdbcClientConfig> shardingjdbcClientConfig;
49+
50+
private ShardingRuleConfiguration shardingRuleConfiguration;
51+
52+
private Map<String, DataSource> shardingDataSource = new HashMap<String, DataSource>();
53+
54+
private DataSource dataSource;
55+
56+
public ShardingjdbcClient(Map<String, ShardingjdbcClientConfig> shardingjdbcClientConfig, ShardingRuleConfiguration shardingRuleConfiguration) {
57+
this.shardingjdbcClientConfig = shardingjdbcClientConfig;
58+
this.shardingRuleConfiguration = shardingRuleConfiguration;
59+
}
60+
61+
@Override public void initialize() throws ShardingjdbcClientException {
62+
try {
63+
shardingjdbcClientConfig.forEach((key, value) -> {
64+
BasicDataSource basicDataSource = new BasicDataSource();
65+
basicDataSource.setDriverClassName(value.getDriverClass());
66+
basicDataSource.setUrl(value.getUrl());
67+
basicDataSource.setUsername(value.getUserName());
68+
basicDataSource.setPassword(value.getPassword());
69+
shardingDataSource.put(key, basicDataSource);
70+
logger.info("add sharding datasource: {}, url: {}", key, value.getUrl());
71+
});
72+
dataSource = ShardingDataSourceFactory.createDataSource(shardingDataSource, shardingRuleConfiguration,
73+
new HashMap<String, Object>(), new Properties());
74+
} catch (Exception e) {
75+
logger.error("case the exception is 'Cannot load JDBC driver class', please add the driver mysql-connector-java-5.1.36.jar to collector-libs manual");
76+
throw new ShardingjdbcClientException(e.getMessage(), e);
77+
}
78+
}
79+
80+
@Override public void shutdown() {
81+
82+
}
83+
84+
public Connection getConnection() throws SQLException {
85+
return dataSource.getConnection();
86+
}
87+
88+
public void execute(String sql) throws ShardingjdbcClientException {
89+
Connection conn = null;
90+
Statement statement = null;
91+
try {
92+
conn = getConnection();
93+
statement = conn.createStatement();
94+
statement.execute(sql);
95+
} catch (SQLException e) {
96+
throw new ShardingjdbcClientException(e.getMessage(), e);
97+
} finally {
98+
try {
99+
if (statement != null) {
100+
statement.close();
101+
}
102+
if (conn != null) {
103+
conn.close();
104+
}
105+
} catch (SQLException e) {
106+
throw new ShardingjdbcClientException(e.getMessage(), e);
107+
}
108+
}
109+
}
110+
111+
public ResultSet executeQuery(String sql, Object[] params) throws ShardingjdbcClientException {
112+
logger.debug("execute query with result: {}", sql);
113+
ResultSet rs;
114+
PreparedStatement statement;
115+
try {
116+
statement = getConnection().prepareStatement(sql);
117+
if (params != null) {
118+
for (int i = 0; i < params.length; i++) {
119+
statement.setObject(i + 1, params[i]);
120+
}
121+
}
122+
rs = statement.executeQuery();
123+
} catch (SQLException e) {
124+
throw new ShardingjdbcClientException(e.getMessage(), e);
125+
}
126+
return rs;
127+
}
128+
129+
public boolean execute(String sql, Object[] params) throws ShardingjdbcClientException {
130+
logger.debug("execute insert/update/delete: {}", sql);
131+
boolean flag;
132+
Connection conn = null;
133+
PreparedStatement statement = null;
134+
try {
135+
conn = getConnection();
136+
conn.setAutoCommit(true);
137+
statement = conn.prepareStatement(sql);
138+
if (params != null) {
139+
for (int i = 0; i < params.length; i++) {
140+
statement.setObject(i + 1, params[i]);
141+
}
142+
}
143+
flag = statement.execute();
144+
} catch (SQLException e) {
145+
throw new ShardingjdbcClientException(e.getMessage(), e);
146+
} finally {
147+
try {
148+
if (statement != null) {
149+
statement.close();
150+
}
151+
if (conn != null) {
152+
conn.close();
153+
}
154+
} catch (SQLException e) {
155+
throw new ShardingjdbcClientException(e.getMessage(), e);
156+
}
157+
}
158+
return flag;
159+
}
160+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
20+
package org.apache.skywalking.apm.collector.client.shardingjdbc;
21+
22+
import org.apache.skywalking.apm.collector.core.module.ModuleConfig;
23+
24+
/**
25+
* @author linjiaqi
26+
*/
27+
public class ShardingjdbcClientConfig extends ModuleConfig {
28+
29+
private String driverClass;
30+
private String url;
31+
private String userName;
32+
private String password;
33+
34+
public ShardingjdbcClientConfig() {
35+
36+
}
37+
38+
public ShardingjdbcClientConfig(String driverClass, String url, String username, String password) {
39+
this.driverClass = driverClass;
40+
this.url = url;
41+
this.userName = username;
42+
this.password = password;
43+
}
44+
45+
public String getDriverClass() {
46+
return driverClass;
47+
}
48+
49+
public void setDriverClass(String driverClass) {
50+
this.driverClass = driverClass;
51+
}
52+
53+
public String getUrl() {
54+
return url;
55+
}
56+
57+
public void setUrl(String url) {
58+
this.url = url;
59+
}
60+
61+
public String getUserName() {
62+
return userName;
63+
}
64+
65+
public void setUserName(String userName) {
66+
this.userName = userName;
67+
}
68+
69+
public String getPassword() {
70+
return password;
71+
}
72+
73+
public void setPassword(String password) {
74+
this.password = password;
75+
}
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
20+
package org.apache.skywalking.apm.collector.client.shardingjdbc;
21+
22+
import org.apache.skywalking.apm.collector.client.ClientException;
23+
24+
/**
25+
* @author linjiaqi
26+
*/
27+
public class ShardingjdbcClientException extends ClientException {
28+
29+
public ShardingjdbcClientException(String message) {
30+
super(message);
31+
}
32+
33+
public ShardingjdbcClientException(String message, Throwable cause) {
34+
super(message, cause);
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to You under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
~
18+
-->
19+
20+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
<parent>
23+
<artifactId>apm-collector-storage</artifactId>
24+
<groupId>org.apache.skywalking</groupId>
25+
<version>5.0.0-beta2-SNAPSHOT</version>
26+
</parent>
27+
<modelVersion>4.0.0</modelVersion>
28+
29+
<artifactId>collector-storage-shardingjdbc-provider</artifactId>
30+
<packaging>jar</packaging>
31+
32+
<dependencies>
33+
<dependency>
34+
<groupId>org.apache.skywalking</groupId>
35+
<artifactId>collector-storage-define</artifactId>
36+
<version>${project.version}</version>
37+
</dependency>
38+
</dependencies>
39+
</project>

0 commit comments

Comments
 (0)