远程调用和编写数据接入客户端的配置类
parent
a663bbfdeb
commit
625d14d0e4
|
@ -20,7 +20,8 @@
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.muyu</groupId>
|
<groupId>com.muyu</groupId>
|
||||||
<artifactId>muyu-source-common</artifactId>
|
<artifactId>muyu-source-remote</artifactId>
|
||||||
|
<version>3.6.5</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.alibaba</groupId>
|
<groupId>com.alibaba</groupId>
|
||||||
|
|
|
@ -1,13 +0,0 @@
|
||||||
package com.muyu.JdbcUtils;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Lenovo
|
|
||||||
* @ Tool:IntelliJ IDEA
|
|
||||||
* @ Author:CHX
|
|
||||||
* @ Date:2024-08-22-15:02
|
|
||||||
* @ Version:1.0
|
|
||||||
* @ Description:JDBC抽象类
|
|
||||||
*/
|
|
||||||
public class JDBCAbstractClass {
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,13 +0,0 @@
|
||||||
package com.muyu.JdbcUtils;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Lenovo
|
|
||||||
* @ Tool:IntelliJ IDEA
|
|
||||||
* @ Author:CHX
|
|
||||||
* @ Date:2024-08-22-15:04
|
|
||||||
* @ Version:1.0
|
|
||||||
* @ Description:JDBC实体类
|
|
||||||
*/
|
|
||||||
public class JDBCConcreteClass {
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,5 +1,17 @@
|
||||||
package com.muyu.config;
|
package com.muyu.config;
|
||||||
|
|
||||||
|
import com.muyu.common.core.domain.Result;
|
||||||
|
import com.muyu.source.domain.Children;
|
||||||
|
import com.muyu.source.domain.DataSource;
|
||||||
|
import com.muyu.source.domain.DataType;
|
||||||
|
import com.muyu.source.remote.RemoteChildrenService;
|
||||||
|
import com.muyu.source.remote.RemoteDataSourceService;
|
||||||
|
import com.muyu.source.remote.RemoteDataTypeService;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Lenovo
|
* @author Lenovo
|
||||||
* @ Tool:IntelliJ IDEA
|
* @ Tool:IntelliJ IDEA
|
||||||
|
@ -8,6 +20,50 @@ package com.muyu.config;
|
||||||
* @ Version:1.0
|
* @ Version:1.0
|
||||||
* @ Description:数据接入客户端配置类
|
* @ Description:数据接入客户端配置类
|
||||||
*/
|
*/
|
||||||
|
@ComponentScan
|
||||||
public class SourceClientConfig {
|
public class SourceClientConfig {
|
||||||
|
@Resource
|
||||||
|
private RemoteDataTypeService remoteDataTypeService;
|
||||||
|
@Resource
|
||||||
|
private RemoteDataSourceService remoteDataSourceService;
|
||||||
|
@Resource
|
||||||
|
private RemoteChildrenService remoteChildrenService;
|
||||||
|
|
||||||
|
public void getDataModel(Long id){
|
||||||
|
//根据ID查询出数据源的数据
|
||||||
|
Result<DataSource> dataSourceResult =remoteDataSourceService.getDataSource(id);
|
||||||
|
DataSource dataSource = dataSourceResult.getData();
|
||||||
|
//根据ID查询出数据库类型的数据
|
||||||
|
Result<DataType> dataTypeResult = remoteDataTypeService.getDataType(dataSource.getDataType());
|
||||||
|
DataType dataType = dataTypeResult.getData();
|
||||||
|
//根据ID查询出数据源的数据库结构的数据
|
||||||
|
Result<Children> childrenResult =remoteChildrenService.getChildren(dataSource.getId());
|
||||||
|
Children children = childrenResult.getData();
|
||||||
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
Class.forName(dataType.getDriverManager());
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
//获取连接对象 拼接数据库地址
|
||||||
|
Connection connection = DriverManager.getConnection(dataType.getPrefix()+dataSource.getIp()+dataSource.getPort()
|
||||||
|
+"//"+dataSource.getDatabaseName()+"?"+dataSource.getConnectionParam(),dataSource.getUserName(),dataSource.getPassword());
|
||||||
|
//创建执行对象
|
||||||
|
Statement statement = connection.createStatement();
|
||||||
|
//拼接sql语句
|
||||||
|
ResultSet resultSet = statement.executeQuery("SELECT * FROM" + children.getName());
|
||||||
|
//处理返回结果
|
||||||
|
while (resultSet.next()){
|
||||||
|
System.out.println(resultSet.getString(1));
|
||||||
|
}
|
||||||
|
//关闭资源
|
||||||
|
resultSet.close();
|
||||||
|
statement.close();
|
||||||
|
connection.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +0,0 @@
|
||||||
package com.muyu.config;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Lenovo
|
|
||||||
* @ Tool:IntelliJ IDEA
|
|
||||||
* @ Author:CHX
|
|
||||||
* @ Date:2024-08-22-14:34
|
|
||||||
* @ Version:1.0
|
|
||||||
* @ Description:初始化加载
|
|
||||||
*/
|
|
||||||
public class SourceClientRunner {
|
|
||||||
}
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
package com.muyu.source.remote;
|
||||||
|
|
||||||
|
import com.muyu.common.core.constant.ServiceNameConstants;
|
||||||
|
import com.muyu.common.core.domain.Result;
|
||||||
|
import com.muyu.source.domain.Children;
|
||||||
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Lenovo
|
||||||
|
* @ Tool:IntelliJ IDEA
|
||||||
|
* @ Author:CHX
|
||||||
|
* @ Date:2024-08-21-10:50
|
||||||
|
* @ Version:1.0
|
||||||
|
* @ Description:数据源远程调用
|
||||||
|
*/
|
||||||
|
@FeignClient(contextId = "RemoteChildrenService", value = ServiceNameConstants.SOURCE_SERVICE
|
||||||
|
, fallbackFactory = RemoteChildrenService.class, path = "/children")
|
||||||
|
public interface RemoteChildrenService {
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping("/getChildren/{id}")
|
||||||
|
Result<Children> getChildren( @PathVariable Long id);
|
||||||
|
}
|
|
@ -1,18 +1,26 @@
|
||||||
//package com.muyu.source.remote;
|
package com.muyu.source.remote;
|
||||||
//
|
|
||||||
//import com.muyu.source.remote.factory.RemoteDataSourceFactory;
|
import com.muyu.common.core.constant.ServiceNameConstants;
|
||||||
//import org.springframework.cloud.openfeign.FeignClient;
|
import com.muyu.common.core.domain.Result;
|
||||||
//import com.muyu.common.core.constant.ServiceNameConstants;
|
import com.muyu.source.domain.DataSource;
|
||||||
//
|
import com.muyu.source.remote.factory.RemoteDataSourceFactory;
|
||||||
///**
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
// * @ Tool:IntelliJ IDEA
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
// * @ Author:CHX
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
// * @ Date:2024-08-21-10:50
|
|
||||||
// * @ Version:1.0
|
/**
|
||||||
// * @ Description:数据源远程调用
|
* @author Lenovo
|
||||||
// * @author Lenovo
|
* @ Tool:IntelliJ IDEA
|
||||||
// */
|
* @ Author:CHX
|
||||||
//@FeignClient(contextId = "RemoteDataSourceService",value = ServiceNameConstants.SOURCE_SERVICE,fallbackFactory = RemoteDataSourceFactory.class, path = "/dataSource")
|
* @ Date:2024-08-21-10:50
|
||||||
//public interface RemoteDataSourceService {
|
* @ Version:1.0
|
||||||
//
|
* @ Description:数据源远程调用
|
||||||
//}
|
*/
|
||||||
|
@FeignClient(contextId = "RemoteDataSourceService", value = ServiceNameConstants.SOURCE_SERVICE
|
||||||
|
, fallbackFactory = RemoteDataSourceFactory.class, path = "/dataSource")
|
||||||
|
public interface RemoteDataSourceService {
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping("/getDataSourceById/{id}")
|
||||||
|
Result<DataSource> getDataSource(@PathVariable Long id);
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
package com.muyu.source.remote;
|
||||||
|
|
||||||
|
import com.muyu.common.core.constant.ServiceNameConstants;
|
||||||
|
import com.muyu.common.core.domain.Result;
|
||||||
|
import com.muyu.source.domain.DataType;
|
||||||
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Lenovo
|
||||||
|
* @ Tool:IntelliJ IDEA
|
||||||
|
* @ Author:CHX
|
||||||
|
* @ Date:2024-08-21-10:50
|
||||||
|
* @ Version:1.0
|
||||||
|
* @ Description:数据源远程调用
|
||||||
|
*/
|
||||||
|
@FeignClient(contextId = "RemoteDataTypeService", value = ServiceNameConstants.SOURCE_SERVICE
|
||||||
|
, fallbackFactory = RemoteDataTypeService.class, path = "/dataType")
|
||||||
|
public interface RemoteDataTypeService {
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping("/getDataType/{type}")
|
||||||
|
Result<DataType> getDataType( @PathVariable String type);
|
||||||
|
}
|
|
@ -0,0 +1,26 @@
|
||||||
|
package com.muyu.source.remote.factory;
|
||||||
|
|
||||||
|
import com.muyu.common.core.domain.Result;
|
||||||
|
import com.muyu.source.domain.Children;
|
||||||
|
import com.muyu.source.remote.RemoteChildrenService;
|
||||||
|
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Lenovo
|
||||||
|
* @ Tool:IntelliJ IDEA
|
||||||
|
* @ Author:CHX
|
||||||
|
* @ Date:2024-08-21-10:54
|
||||||
|
* @ Version:1.0
|
||||||
|
* @ Description:
|
||||||
|
*/
|
||||||
|
public class RemoteChildrenFactory implements FallbackFactory<RemoteChildrenService> {
|
||||||
|
@Override
|
||||||
|
public RemoteChildrenService create(Throwable cause) {
|
||||||
|
return new RemoteChildrenService() {
|
||||||
|
@Override
|
||||||
|
public Result<Children> getChildren(Long id) {
|
||||||
|
return Result.error(cause.getMessage());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,19 +1,26 @@
|
||||||
//package com.muyu.source.remote.factory;
|
package com.muyu.source.remote.factory;
|
||||||
//
|
|
||||||
//import com.muyu.source.remote.RemoteDataSourceService;
|
import com.muyu.common.core.domain.Result;
|
||||||
//import org.springframework.cloud.openfeign.FallbackFactory;
|
import com.muyu.source.domain.DataSource;
|
||||||
//
|
import com.muyu.source.remote.RemoteDataSourceService;
|
||||||
///**
|
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||||
// * @ Tool:IntelliJ IDEA
|
|
||||||
// * @ Author:CHX
|
/**
|
||||||
// * @ Date:2024-08-21-10:54
|
* @author Lenovo
|
||||||
// * @ Version:1.0
|
* @ Tool:IntelliJ IDEA
|
||||||
// * @ Description:
|
* @ Author:CHX
|
||||||
// * @author Lenovo
|
* @ Date:2024-08-21-10:54
|
||||||
// */
|
* @ Version:1.0
|
||||||
//public class RemoteDataSourceFactory implements FallbackFactory<RemoteDataSourceService> {
|
* @ Description:
|
||||||
// @Override
|
*/
|
||||||
// public RemoteDataSourceService create(Throwable cause) {
|
public class RemoteDataSourceFactory implements FallbackFactory<RemoteDataSourceService> {
|
||||||
// return null;
|
@Override
|
||||||
// }
|
public RemoteDataSourceService create(Throwable cause) {
|
||||||
//}
|
return new RemoteDataSourceService() {
|
||||||
|
@Override
|
||||||
|
public Result<DataSource> getDataSource(Long id) {
|
||||||
|
return Result.error(cause.getMessage());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
package com.muyu.source.remote.factory;
|
||||||
|
|
||||||
|
import com.muyu.common.core.domain.Result;
|
||||||
|
import com.muyu.source.domain.DataType;
|
||||||
|
import com.muyu.source.remote.RemoteDataTypeService;
|
||||||
|
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Lenovo
|
||||||
|
* @ Tool:IntelliJ IDEA
|
||||||
|
* @ Author:CHX
|
||||||
|
* @ Date:2024-08-21-10:54
|
||||||
|
* @ Version:1.0
|
||||||
|
* @ Description:
|
||||||
|
*/
|
||||||
|
public class RemoteDataTypeFactory implements FallbackFactory<RemoteDataTypeService> {
|
||||||
|
@Override
|
||||||
|
public RemoteDataTypeService create(Throwable cause) {
|
||||||
|
return new RemoteDataTypeService() {
|
||||||
|
@Override
|
||||||
|
public Result<DataType> getDataType(String type) {
|
||||||
|
return Result.error(cause.getMessage());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -1 +1,3 @@
|
||||||
#com.muyu.source.remote.factory.RemoteDataSourceFactory
|
com.muyu.source.remote.factory.RemoteDataSourceFactory
|
||||||
|
com.muyu.source.remote.factory.RemoteDataTypeFactory
|
||||||
|
com.muyu.source.remote.factory.RemoteChildrenFactory
|
||||||
|
|
Loading…
Reference in New Issue