远程调用和编写数据接入客户端的配置类

master
冷调 2024-08-22 20:47:49 +08:00
parent a663bbfdeb
commit 625d14d0e4
12 changed files with 215 additions and 77 deletions

View File

@ -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>

View File

@ -1,13 +0,0 @@
package com.muyu.JdbcUtils;
/**
* @author Lenovo
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-08-22-15:02
* @ Version1.0
* @ DescriptionJDBC
*/
public class JDBCAbstractClass {
}

View File

@ -1,13 +0,0 @@
package com.muyu.JdbcUtils;
/**
* @author Lenovo
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-08-22-15:04
* @ Version1.0
* @ DescriptionJDBC
*/
public class JDBCConcreteClass {
}

View File

@ -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
* @ ToolIntelliJ IDEA
@ -8,6 +20,50 @@ package com.muyu.config;
* @ Version1.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);
}
}
}

View File

@ -1,12 +0,0 @@
package com.muyu.config;
/**
* @author Lenovo
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-08-22-14:34
* @ Version1.0
* @ Description
*/
public class SourceClientRunner {
}

View File

@ -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
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-08-21-10:50
* @ Version1.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);
}

View File

@ -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;
//
///**
// * @ ToolIntelliJ IDEA
// * @ AuthorCHX
// * @ Date2024-08-21-10:50
// * @ Version1.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
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-08-21-10:50
* @ Version1.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);
}

View File

@ -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
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-08-21-10:50
* @ Version1.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);
}

View File

@ -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
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-08-21-10:54
* @ Version1.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());
}
};
}
}

View File

@ -1,19 +1,26 @@
//package com.muyu.source.remote.factory;
//
//import com.muyu.source.remote.RemoteDataSourceService;
//import org.springframework.cloud.openfeign.FallbackFactory;
//
///**
// * @ ToolIntelliJ IDEA
// * @ AuthorCHX
// * @ Date2024-08-21-10:54
// * @ Version1.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
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-08-21-10:54
* @ Version1.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());
}
};
}
}

View File

@ -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
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-08-21-10:54
* @ Version1.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());
}
};
}
}

View File

@ -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