Merge remote-tracking branch 'origin/master'
commit
4d9d137be2
|
@ -23,5 +23,10 @@
|
|||
<groupId>com.muyu</groupId>
|
||||
<artifactId>muyu-common-redis</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>muyu-data-source-common</artifactId>
|
||||
<version>3.6.3</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
|
@ -1,41 +1,41 @@
|
|||
//package com.muyu.common.cache;
|
||||
//
|
||||
///**
|
||||
// * 提供了一个连接池的准则
|
||||
// *
|
||||
// * @author AnNan.Wang
|
||||
// * @Date 2024/4/29 029 14:41
|
||||
// */
|
||||
//public interface BasePool<T> {
|
||||
// /**
|
||||
// * 初始化
|
||||
// */
|
||||
// public void init();
|
||||
//
|
||||
// /**
|
||||
// * 获取连接
|
||||
// */
|
||||
//
|
||||
// public T getConn();
|
||||
//
|
||||
// /**
|
||||
// * 归还连接
|
||||
// * @param conn
|
||||
// */
|
||||
//
|
||||
// public void reaplase(T conn);
|
||||
//
|
||||
// /**
|
||||
// * 创建连接
|
||||
// * @return
|
||||
// */
|
||||
//
|
||||
// public T creatConnection();
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * 关闭连接
|
||||
// */
|
||||
// public void closeConn();
|
||||
//
|
||||
//}
|
||||
package com.muyu.common.cache;
|
||||
|
||||
/**
|
||||
* 提供了一个连接池的准则
|
||||
*
|
||||
* @author AnNan.Wang
|
||||
* @Date 2024/4/29 029 14:41
|
||||
*/
|
||||
public interface BasePool<T> {
|
||||
/**
|
||||
* 初始化
|
||||
*/
|
||||
public void init();
|
||||
|
||||
/**
|
||||
* 获取连接
|
||||
*/
|
||||
|
||||
public T getConn();
|
||||
|
||||
/**
|
||||
* 归还连接
|
||||
* @param conn
|
||||
*/
|
||||
|
||||
public void reaplase(T conn);
|
||||
|
||||
/**
|
||||
* 创建连接
|
||||
* @return
|
||||
*/
|
||||
|
||||
public T creatConnection();
|
||||
|
||||
|
||||
/**
|
||||
* 关闭连接
|
||||
*/
|
||||
public void closeConn();
|
||||
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package com.muyu.source.clinet.pool.config;
|
||||
package com.muyu.common.cache;
|
||||
|
||||
|
||||
import com.muyu.common.cache.data.MysqlPrefix;
|
||||
import com.muyu.data.source.domain.DataSource;
|
||||
import com.muyu.source.clinet.MysqlPrefix;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
|
@ -1,11 +1,14 @@
|
|||
package com.muyu.source.clinet.pool;
|
||||
package com.muyu.common.cache.abs;
|
||||
|
||||
|
||||
import com.muyu.source.clinet.BasePool;
|
||||
import com.muyu.source.clinet.MysqlPrefix;
|
||||
import com.muyu.source.clinet.pool.config.MysqlPoolConfig;
|
||||
import com.muyu.common.cache.BasePool;
|
||||
|
||||
|
||||
import com.muyu.common.cache.MysqlPoolConfig;
|
||||
import com.muyu.common.cache.data.MysqlPrefix;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
@ -48,6 +51,7 @@ public class MysqlPool implements BasePool<Connection> {
|
|||
/**
|
||||
* 连接参数
|
||||
*/
|
||||
@Autowired
|
||||
public MysqlPoolConfig mysqlPoolConfig;
|
||||
|
||||
|
|
@ -1,13 +1,13 @@
|
|||
//package com.muyu.common.cache.data;
|
||||
//
|
||||
///**
|
||||
// * Mysql超时异常
|
||||
// *
|
||||
// * @author WangLei
|
||||
// * @Date 2024/4/29 029 15:17
|
||||
// */
|
||||
//public class MysqlConnException extends RuntimeException{
|
||||
// public MysqlConnException(String message) {
|
||||
// super(message);
|
||||
// }
|
||||
//}
|
||||
package com.muyu.common.cache.data;
|
||||
|
||||
/**
|
||||
* Mysql超时异常
|
||||
*
|
||||
* @author WangLei
|
||||
* @Date 2024/4/29 029 15:17
|
||||
*/
|
||||
public class MysqlConnException extends RuntimeException{
|
||||
public MysqlConnException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,24 +1,24 @@
|
|||
//package com.muyu.common.cache.data;
|
||||
//
|
||||
///**
|
||||
// * 基础定值
|
||||
// *
|
||||
// * @author WangLei
|
||||
// * @Date 2024/4/29 029 14:30
|
||||
// */
|
||||
//
|
||||
//public class MysqlPrefix {
|
||||
// /**
|
||||
// * mysql连接前缀
|
||||
// */
|
||||
//
|
||||
// public static final String MYSQLDUMP = "jdbc:mysql://";
|
||||
// public static final String POSTGRESQL ="jdbc:postgresql://";
|
||||
// public static void driver(String driverName){
|
||||
// try {
|
||||
// Class.forName(driverName);
|
||||
// } catch (ClassNotFoundException e) {
|
||||
// throw new RuntimeException(e);
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
package com.muyu.common.cache.data;
|
||||
|
||||
/**
|
||||
* 基础定值
|
||||
*
|
||||
* @author WangLei
|
||||
* @Date 2024/4/29 029 14:30
|
||||
*/
|
||||
|
||||
public class MysqlPrefix {
|
||||
/**
|
||||
* mysql连接前缀
|
||||
*/
|
||||
|
||||
public static final String MYSQLDUMP = "jdbc:mysql://";
|
||||
public static final String POSTGRESQL ="jdbc:postgresql://";
|
||||
public static void driver(String driverName){
|
||||
try {
|
||||
Class.forName(driverName);
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
<artifactId>druid</artifactId>
|
||||
<version>1.2.6</version> <!-- 使用最新的 Druid 版本 -->
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
|
|
|
@ -1,41 +0,0 @@
|
|||
package com.muyu.source.clinet;
|
||||
|
||||
/**
|
||||
* 提供了一个连接池的准则
|
||||
*
|
||||
* @author AnNan.Wang
|
||||
* @Date 2024/4/29 029 14:41
|
||||
*/
|
||||
public interface BasePool<T> {
|
||||
/**
|
||||
* 初始化
|
||||
*/
|
||||
public void init();
|
||||
|
||||
/**
|
||||
* 获取连接
|
||||
*/
|
||||
|
||||
public T getConn();
|
||||
|
||||
/**
|
||||
* 归还连接
|
||||
* @param conn
|
||||
*/
|
||||
|
||||
public void reaplase(T conn);
|
||||
|
||||
/**
|
||||
* 创建连接
|
||||
* @return
|
||||
*/
|
||||
|
||||
public T creatConnection();
|
||||
|
||||
|
||||
/**
|
||||
* 关闭连接
|
||||
*/
|
||||
public void closeConn();
|
||||
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
package com.muyu.source.clinet;
|
||||
|
||||
/**
|
||||
* Mysql超时异常
|
||||
*
|
||||
* @author WangLei
|
||||
* @Date 2024/4/29 029 15:17
|
||||
*/
|
||||
public class MysqlConnException extends RuntimeException{
|
||||
public MysqlConnException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
|
@ -1,24 +0,0 @@
|
|||
package com.muyu.source.clinet;
|
||||
|
||||
/**
|
||||
* 基础定值
|
||||
*
|
||||
* @author WangLei
|
||||
* @Date 2024/4/29 029 14:30
|
||||
*/
|
||||
|
||||
public class MysqlPrefix {
|
||||
/**
|
||||
* mysql连接前缀
|
||||
*/
|
||||
|
||||
public static final String MYSQLDUMP = "jdbc:mysql://";
|
||||
public static final String POSTGRESQL ="jdbc:postgresql://";
|
||||
public static void driver(String driverName){
|
||||
try {
|
||||
Class.forName(driverName);
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,12 +1,12 @@
|
|||
package com.muyu.source.clinet.config;
|
||||
|
||||
import com.muyu.source.clinet.pool.MysqlPool;
|
||||
import com.muyu.common.cache.MysqlPoolConfig;
|
||||
import com.muyu.common.cache.abs.MysqlPool;
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.common.core.web.page.TableDataInfo;
|
||||
import com.muyu.data.source.domain.DataSource;
|
||||
import com.muyu.data.source.domain.req.DataSourceQueryReq;
|
||||
import com.muyu.data.source.remote.RemoteDataManagerService;
|
||||
import com.muyu.source.clinet.pool.config.MysqlPoolConfig;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
|
@ -45,38 +45,4 @@ public class DataSourceClinetRunner implements ApplicationRunner {
|
|||
}
|
||||
log.info(rows.size());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//druid(德鲁伊) 连接池
|
||||
// DataSourceSingleton dataSourceSingleton = DataSourceSingleton.getInstance();
|
||||
// DruidDataSource druidDataSource = new DruidDataSource();
|
||||
// try {
|
||||
// String dataSourceKey = row.getId() + row.getDatabaseName();
|
||||
//
|
||||
// if (row.getDataAccessTypeId() == 3) {
|
||||
// configureDruidDataSource(druidDataSource, "jdbc:mysql://" + row.getHostAddress() + ":" + row.getHostPort() + "/" + row.getDatabaseName(),
|
||||
// row.getDatabaseUserName(), row.getDatabaseUserPassword(), row.getInitialQuantity(), row.getMaximumQuantity(), row.getMaximumFrequency(), row.getMaximumTime());
|
||||
// } else if (row.getDataAccessTypeId() == 5) {
|
||||
// configureDruidDataSource(druidDataSource, "jdbc:postgresql://" + row.getHostAddress() + ":" + row.getHostPort() + "/" + row.getDatabaseName(),
|
||||
// row.getDatabaseUserName(), row.getDatabaseUserPassword(), row.getInitialQuantity(), row.getMaximumQuantity(), row.getMaximumFrequency(), row.getMaximumTime());
|
||||
// }
|
||||
//
|
||||
//
|
||||
//
|
||||
// // 将数据源添加到 DataSourceSingleton 中
|
||||
// } catch (Exception e) {
|
||||
// log.error("Failed to configure data source for {}", row.getDatabaseName(), e);
|
||||
// }
|
||||
// private void configureDruidDataSource(DruidDataSource dataSource, String url, String username, String password,
|
||||
// int initialSize, int maxActive, int minIdle, int maxWait) {
|
||||
// dataSource.setUrl(url);
|
||||
// dataSource.setUsername(username);
|
||||
// dataSource.setPassword(password);
|
||||
// dataSource.setInitialSize(initialSize);
|
||||
// dataSource.setMaxActive(maxActive);
|
||||
// dataSource.setMinIdle(minIdle);
|
||||
// dataSource.setMaxWait(maxWait);
|
||||
// }
|
||||
}
|
||||
|
|
|
@ -8,8 +8,8 @@ import java.util.HashMap;
|
|||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
public class DataSourceSingleton implements Serializable {
|
||||
public static Map<String, DataSource> dataSourceMap = new HashMap<>();
|
||||
public class DataSourceSingleton{
|
||||
public Map<String, DataSource> dataSourceMap = new HashMap<>();
|
||||
|
||||
public static volatile DataSourceSingleton instance;
|
||||
|
||||
|
@ -26,12 +26,12 @@ public class DataSourceSingleton implements Serializable {
|
|||
}
|
||||
|
||||
// 添加数据源
|
||||
public static void addDataSource(String key, DataSource dataSource) {
|
||||
public void addDataSource(String key, DataSource dataSource) {
|
||||
dataSourceMap.put(key, dataSource);
|
||||
}
|
||||
|
||||
// 获取数据源
|
||||
public static DataSource getDataSource(String key) {
|
||||
public DataSource getDataSource(String key) {
|
||||
return dataSourceMap.get(key);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,81 @@
|
|||
package com.muyu.source.clinet.pool;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.muyu.source.clinet.factory.DataSourceSingleton;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
/**
|
||||
* druid(德鲁伊)的详细信息
|
||||
*
|
||||
* @ClassName DruidDetails
|
||||
* @Author AnNan.Wang
|
||||
* @Date 2024/5/11 15:52
|
||||
*/
|
||||
|
||||
@Log4j2
|
||||
public class DruidDetails implements ApplicationRunner {
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
DruidDataSource source = new DruidDataSource();
|
||||
String url = "jdbc:mysql://101.34.243.166:3306/test_day01";
|
||||
String UserName = "root";
|
||||
String password = "wan@123";
|
||||
|
||||
//设置数据源连接的 URL
|
||||
source.setUrl(url);
|
||||
//用户名
|
||||
source.setUsername(UserName);
|
||||
//密码
|
||||
source.setPassword(password);
|
||||
|
||||
//初始连接数
|
||||
source.setInitialSize(3);
|
||||
//最大活动连接数
|
||||
source.setMaxActive(10);
|
||||
//最小空闲连接数
|
||||
source.setMinIdle(1);
|
||||
//最大等待时间
|
||||
source.setMaxWait(3000);
|
||||
|
||||
//获取数据连接
|
||||
Connection connection=null;
|
||||
|
||||
try {
|
||||
connection=source.getConnection();
|
||||
|
||||
//执行查询操作
|
||||
Statement statement = connection.createStatement();
|
||||
ResultSet resultSet = statement.executeQuery("SELECT * FROM t_user");
|
||||
|
||||
while (resultSet.next()) {
|
||||
int userId = resultSet.getInt("user_id");
|
||||
String name = resultSet.getString("name");
|
||||
int age = resultSet.getInt("age");
|
||||
|
||||
log.info("user_id:{},name:{},age:{}",userId,name,age);
|
||||
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
//关闭连接
|
||||
if (connection != null) {
|
||||
try {
|
||||
connection.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,2 +1,3 @@
|
|||
com.muyu.source.clinet.config.DataSourceClinetConfig
|
||||
com.muyu.source.clinet.config.DataSourceClinetRunner
|
||||
com.muyu.source.clinet.pool.DruidDetails
|
||||
|
|
Loading…
Reference in New Issue