远程调用和编写数据接入客户端的配置类
parent
a663bbfdeb
commit
625d14d0e4
|
@ -20,7 +20,8 @@
|
|||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>muyu-source-common</artifactId>
|
||||
<artifactId>muyu-source-remote</artifactId>
|
||||
<version>3.6.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<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;
|
||||
|
||||
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
|
||||
* @ Tool:IntelliJ IDEA
|
||||
|
@ -8,6 +20,50 @@ package com.muyu.config;
|
|||
* @ Version:1.0
|
||||
* @ Description:数据接入客户端配置类
|
||||
*/
|
||||
@ComponentScan
|
||||
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;
|
||||
//
|
||||
//import com.muyu.source.remote.factory.RemoteDataSourceFactory;
|
||||
//import org.springframework.cloud.openfeign.FeignClient;
|
||||
//import com.muyu.common.core.constant.ServiceNameConstants;
|
||||
//
|
||||
///**
|
||||
// * @ Tool:IntelliJ IDEA
|
||||
// * @ Author:CHX
|
||||
// * @ Date:2024-08-21-10:50
|
||||
// * @ Version:1.0
|
||||
// * @ Description:数据源远程调用
|
||||
// * @author Lenovo
|
||||
// */
|
||||
//@FeignClient(contextId = "RemoteDataSourceService",value = ServiceNameConstants.SOURCE_SERVICE,fallbackFactory = RemoteDataSourceFactory.class, path = "/dataSource")
|
||||
//public interface RemoteDataSourceService {
|
||||
//
|
||||
//}
|
||||
package com.muyu.source.remote;
|
||||
|
||||
import com.muyu.common.core.constant.ServiceNameConstants;
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.source.domain.DataSource;
|
||||
import com.muyu.source.remote.factory.RemoteDataSourceFactory;
|
||||
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 = "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;
|
||||
//
|
||||
//import com.muyu.source.remote.RemoteDataSourceService;
|
||||
//import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
//
|
||||
///**
|
||||
// * @ Tool:IntelliJ IDEA
|
||||
// * @ Author:CHX
|
||||
// * @ Date:2024-08-21-10:54
|
||||
// * @ Version:1.0
|
||||
// * @ Description:
|
||||
// * @author Lenovo
|
||||
// */
|
||||
//public class RemoteDataSourceFactory implements FallbackFactory<RemoteDataSourceService> {
|
||||
// @Override
|
||||
// public RemoteDataSourceService create(Throwable cause) {
|
||||
// return null;
|
||||
// }
|
||||
//}
|
||||
package com.muyu.source.remote.factory;
|
||||
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.source.domain.DataSource;
|
||||
import com.muyu.source.remote.RemoteDataSourceService;
|
||||
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 RemoteDataSourceFactory implements FallbackFactory<RemoteDataSourceService> {
|
||||
@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