初始代码
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.yunzhupaas</groupId>
|
||||
<artifactId>yunzhupaas-cloud-common</artifactId>
|
||||
<version>5.2.0-RELEASE</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>yunzhupaas-common-cloudshardingsphere</artifactId>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.yunzhupaas</groupId>
|
||||
<artifactId>yunzhupaas-common-shardingsphere</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.yunzhupaas.config;
|
||||
|
||||
import com.alibaba.cloud.nacos.NacosConfigManager;
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
|
||||
import com.yunzhupaas.constant.GlobalConst;
|
||||
import org.apache.shardingsphere.driver.api.yaml.YamlShardingSphereDataSourceFactory;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* @author 云筑产品开发平台组
|
||||
* @user N
|
||||
* @copyright 深圳市乐程软件有限公司
|
||||
* @date 2024/11/23 16:52
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass(name = "org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient")
|
||||
@ConditionalOnProperty(prefix = "config", name = "sharding-sphere-enabled", havingValue = "true")
|
||||
public class CloudShardingSphereAutoConfig {
|
||||
|
||||
public CloudShardingSphereAutoConfig() {
|
||||
System.out.println("启用 ShardingSphere");
|
||||
}
|
||||
|
||||
public static final String PREFIX = "shardingsphere";
|
||||
|
||||
@Bean
|
||||
public Object initShardingSphereDataSource(@Qualifier("dataSourceSystem") DynamicRoutingDataSource dataSource,
|
||||
NacosConfigManager nacosConfigManager) throws SQLException, IOException, NacosException {
|
||||
String shardingContent = nacosConfigManager.getConfigService().getConfig("sharding-sphere.yaml",
|
||||
"DEFAULT_GROUP", 3000L);
|
||||
if (shardingContent != null) {
|
||||
DataSource SSDataSource = YamlShardingSphereDataSourceFactory
|
||||
.createDataSource(shardingContent.getBytes(GlobalConst.DEFAULT_CHARSET));
|
||||
dataSource.addDataSource(PREFIX, SSDataSource);
|
||||
} else {
|
||||
System.out.println("ShardingSphere 加载失败,缺少 sharding-sphere.yaml 配置文件");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user