提交 4a33c26c authored 作者: 廖宁波's avatar 廖宁波

first commit

上级
流水线 #1277 已取消 于阶段
差异被折叠。
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
/*
* Copyright 2007-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.net.*;
import java.io.*;
import java.nio.channels.*;
import java.util.Properties;
public class MavenWrapperDownloader {
private static final String WRAPPER_VERSION = "0.5.6";
/**
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
*/
private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/"
+ WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar";
/**
* Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
* use instead of the default one.
*/
private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
".mvn/wrapper/maven-wrapper.properties";
/**
* Path where the maven-wrapper.jar will be saved to.
*/
private static final String MAVEN_WRAPPER_JAR_PATH =
".mvn/wrapper/maven-wrapper.jar";
/**
* Name of the property which should be used to override the default download url for the wrapper.
*/
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
public static void main(String args[]) {
System.out.println("- Downloader started");
File baseDirectory = new File(args[0]);
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());
// If the maven-wrapper.properties exists, read it and check if it contains a custom
// wrapperUrl parameter.
File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
String url = DEFAULT_DOWNLOAD_URL;
if (mavenWrapperPropertyFile.exists()) {
FileInputStream mavenWrapperPropertyFileInputStream = null;
try {
mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
Properties mavenWrapperProperties = new Properties();
mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
} catch (IOException e) {
System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
} finally {
try {
if (mavenWrapperPropertyFileInputStream != null) {
mavenWrapperPropertyFileInputStream.close();
}
} catch (IOException e) {
// Ignore ...
}
}
}
System.out.println("- Downloading from: " + url);
File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
if (!outputFile.getParentFile().exists()) {
if (!outputFile.getParentFile().mkdirs()) {
System.out.println(
"- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'");
}
}
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
try {
downloadFileFromURL(url, outputFile);
System.out.println("Done");
System.exit(0);
} catch (Throwable e) {
System.out.println("- Error downloading");
e.printStackTrace();
System.exit(1);
}
}
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) {
String username = System.getenv("MVNW_USERNAME");
char[] password = System.getenv("MVNW_PASSWORD").toCharArray();
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
}
URL website = new URL(urlString);
ReadableByteChannel rbc;
rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(destination);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
}
}
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.3/apache-maven-3.8.3-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar
差异被折叠。
@REM ----------------------------------------------------------------------------
@REM Licensed to the Apache Software Foundation (ASF) under one
@REM or more contributor license agreements. See the NOTICE file
@REM distributed with this work for additional information
@REM regarding copyright ownership. The ASF licenses this file
@REM to you under the Apache License, Version 2.0 (the
@REM "License"); you may not use this file except in compliance
@REM with the License. You may obtain a copy of the License at
@REM
@REM https://www.apache.org/licenses/LICENSE-2.0
@REM
@REM Unless required by applicable law or agreed to in writing,
@REM software distributed under the License is distributed on an
@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@REM KIND, either express or implied. See the License for the
@REM specific language governing permissions and limitations
@REM under the License.
@REM ----------------------------------------------------------------------------
@REM ----------------------------------------------------------------------------
@REM Maven Start Up Batch script
@REM
@REM Required ENV vars:
@REM JAVA_HOME - location of a JDK home dir
@REM
@REM Optional ENV vars
@REM M2_HOME - location of maven2's installed home dir
@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands
@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending
@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven
@REM e.g. to debug Maven itself, use
@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files
@REM ----------------------------------------------------------------------------
@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on'
@echo off
@REM set title of command window
title %0
@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on'
@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO%
@REM set %HOME% to equivalent of $HOME
if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%")
@REM Execute a user defined script before this one
if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre
@REM check for pre script, once with legacy .bat ending and once with .cmd ending
if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat"
if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd"
:skipRcPre
@setlocal
set ERROR_CODE=0
@REM To isolate internal variables from possible post scripts, we use another setlocal
@setlocal
@REM ==== START VALIDATION ====
if not "%JAVA_HOME%" == "" goto OkJHome
echo.
echo Error: JAVA_HOME not found in your environment. >&2
echo Please set the JAVA_HOME variable in your environment to match the >&2
echo location of your Java installation. >&2
echo.
goto error
:OkJHome
if exist "%JAVA_HOME%\bin\java.exe" goto init
echo.
echo Error: JAVA_HOME is set to an invalid directory. >&2
echo JAVA_HOME = "%JAVA_HOME%" >&2
echo Please set the JAVA_HOME variable in your environment to match the >&2
echo location of your Java installation. >&2
echo.
goto error
@REM ==== END VALIDATION ====
:init
@REM Find the project base dir, i.e. the directory that contains the folder ".mvn".
@REM Fallback to current working directory if not found.
set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR%
IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir
set EXEC_DIR=%CD%
set WDIR=%EXEC_DIR%
:findBaseDir
IF EXIST "%WDIR%"\.mvn goto baseDirFound
cd ..
IF "%WDIR%"=="%CD%" goto baseDirNotFound
set WDIR=%CD%
goto findBaseDir
:baseDirFound
set MAVEN_PROJECTBASEDIR=%WDIR%
cd "%EXEC_DIR%"
goto endDetectBaseDir
:baseDirNotFound
set MAVEN_PROJECTBASEDIR=%EXEC_DIR%
cd "%EXEC_DIR%"
:endDetectBaseDir
IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig
@setlocal EnableExtensions EnableDelayedExpansion
for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a
@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS%
:endReadAdditionalConfig
SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe"
set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar"
set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO (
IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B
)
@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central
@REM This allows using the maven wrapper in projects that prohibit checking in binary data.
if exist %WRAPPER_JAR% (
if "%MVNW_VERBOSE%" == "true" (
echo Found %WRAPPER_JAR%
)
) else (
if not "%MVNW_REPOURL%" == "" (
SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
)
if "%MVNW_VERBOSE%" == "true" (
echo Couldn't find %WRAPPER_JAR%, downloading it ...
echo Downloading from: %DOWNLOAD_URL%
)
powershell -Command "&{"^
"$webclient = new-object System.Net.WebClient;"^
"if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^
"$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^
"}"^
"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^
"}"
if "%MVNW_VERBOSE%" == "true" (
echo Finished downloading %WRAPPER_JAR%
)
)
@REM End of extension
@REM Provide a "standardized" way to retrieve the CLI args that will
@REM work with both Windows and non-Windows executions.
set MAVEN_CMD_LINE_ARGS=%*
%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %*
if ERRORLEVEL 1 goto error
goto end
:error
set ERROR_CODE=1
:end
@endlocal & set ERROR_CODE=%ERROR_CODE%
if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost
@REM check for post script, once with legacy .bat ending and once with .cmd ending
if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat"
if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd"
:skipRcPost
@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on'
if "%MAVEN_BATCH_PAUSE%" == "on" pause
if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE%
exit /B %ERROR_CODE%
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lnb</groupId>
<artifactId>multidatasource</artifactId>
<version>1.0.1-SNAPSHOT</version>
<name>multidatasource</name>
<description>multidatasource</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
<scope>runtime</scope>
</dependency>
<!--德鲁伊数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
<version>2.3.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5</version>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.lnb.multidatasourcedemo.constant;
import org.apache.commons.lang3.StringUtils;
/**
* 常量配置
* created by liaoningbo on 2021/11/24
*/
public class Constant {
/*path分隔符*/
public static final String PATH_SPLIT = "/";
public static final String DBCONFIG_ROOT = "/dynamicDataSource/dbconfig";
public static final String READ = "slave";
public static final String WARITE = "master";
public static final String DRIVER_CLASS_NAME = "com.mysql.cj.jdbc.Driver";
public static String getAppDB(String appName) {
if (StringUtils.isEmpty(appName))
return DBCONFIG_ROOT;
return DBCONFIG_ROOT + PATH_SPLIT + appName;
}
}
package com.lnb.multidatasourcedemo.curator;
import com.google.common.collect.Maps;
import com.lnb.multidatasourcedemo.constant.Constant;
import com.lnb.multidatasourcedemo.datasource.DynamicDataSource;
import com.lnb.multidatasourcedemo.listener.AppListener;
import com.lnb.multidatasourcedemo.listener.DbconfigListener;
import com.lnb.multidatasourcedemo.listener.RoutingKeyListener;
import com.lnb.multidatasourcedemo.listener.ZKConnectionStateListener;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.retry.RetryUntilElapsed;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* zookeeper客户端配置
* created by liaoningbo on 2021/11/24
*/
@Component
@Slf4j
public class CuratorClient {
@Autowired
public ZkProperties zkProperties;
private CuratorFramework curator;
// 指定连接超时和session超时,时间为100秒
private final int CONNECT_TIMEOUT = 100000;
private final int RETRY_TIME = Integer.MAX_VALUE;
// 指定重连间隔为6分钟
private final int RETRY_INTERVAL = 360000;
// 监听器列表
public Map<String, PathChildrenCache> listenerList = Maps.newConcurrentMap();
@PostConstruct
public void initCuratorClient() {
// 基础参数校验
if (!zkProperties.validateProperties())
throw new IllegalStateException("Properties config error!");
String zkServers = zkProperties.getZkServer();
if (curator == null) {
curator = newCurator(zkServers);
}
}
private CuratorFramework newCurator(String zkServers) {
CuratorFramework client = CuratorFrameworkFactory.builder().connectString(zkServers)
// 指定最大超时时间和重连时间间隔,直到成功为止
.retryPolicy(new RetryUntilElapsed(RETRY_TIME, RETRY_INTERVAL)).connectionTimeoutMs(CONNECT_TIMEOUT)
.sessionTimeoutMs(CONNECT_TIMEOUT).build();
client.start();
return client;
}
/**
* 注册/dynamicDataSource/dbconfig这一级监听器,用于监听App的增加、删除
* @param dynamicDataSource
*/
public void registerAppListenter(DynamicDataSource dynamicDataSource) throws Exception {
if (!listenerList.containsKey(Constant.DBCONFIG_ROOT)) {
log.info("Register app Listener start");
//创建监听
PathChildrenCache childrenCache = new PathChildrenCache(curator, Constant.DBCONFIG_ROOT, false);
AppListener appListener = new AppListener(dynamicDataSource, this);
childrenCache.getListenable().addListener(appListener);
childrenCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
//添加到监听列表
listenerList.put(Constant.DBCONFIG_ROOT, childrenCache);
log.info("Register app Listener finished successfully");
} else
log.info("The listener already exist! path:[" + Constant.DBCONFIG_ROOT + "].");
}
/**
* 注册/dynamicDataSource/dbconfig/xxx(appname)这一级监听器,用于监听数据库实例的增加、删除
*/
public void registerDbconfigListenter(DynamicDataSource dynamicDataSource, String app) throws Exception {
Set<String> apps = new HashSet<>();
if (StringUtils.isBlank(app)) {
apps.addAll(zkProperties.getAppName());
} else {
apps.add(app);
}
for (String appName : apps) {
if (StringUtils.isNotBlank(appName)) {
String appDB = Constant.getAppDB(appName);
if (!listenerList.containsKey(appDB)) {
log.info("Registering app DB:{} Listener", appDB);
//创建监听
PathChildrenCache childrenCache = new PathChildrenCache(curator, appDB, false);
DbconfigListener dbconfigListener = new DbconfigListener(dynamicDataSource, this);
childrenCache.getListenable().addListener(dbconfigListener);
childrenCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
//添加到监听列表
listenerList.put(appDB, childrenCache);
log.info("Register app DB:{} Listener finished successfully", appDB);
} else
log.info("The listener already exist! path:[" + appDB + "].");
}
}
}
/**
* 注册/dynamicDataSource/dbconfig/xxx(appname)/xxx这一级监听器,用于监听数据库routingKey的增加、删除
*/
public void registerRoutingKeyListenter(DynamicDataSource dynamicDataSource, String path, String app)
throws Exception {
Set<String> apps = new HashSet<>();
if (StringUtils.isBlank(app)) {
apps.addAll(zkProperties.getAppName());
} else {
apps.add(app);
}
for (String appName : apps) {
if (StringUtils.isNotBlank(appName)) {
path = Constant.getAppDB(appName) + Constant.PATH_SPLIT + path;
if (!listenerList.containsKey(path)) {
log.info("Registering routingKey Listener path:{}", path);
//创建监听
PathChildrenCache childrenCache = new PathChildrenCache(curator, path, false);
childrenCache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
RoutingKeyListener routingKeyListener = new RoutingKeyListener(dynamicDataSource, this);
childrenCache.getListenable().addListener(routingKeyListener);
//添加到监听列表
listenerList.putIfAbsent(path, childrenCache);
log.info("Register routingKey Listener finished successfully path:{}", path);
} else
log.info("The listener already exist! path:[" + path + "].");
}
}
}
/**
* 监听与ZK的连接状态
*/
public void registerZKstateListenter(DynamicDataSource dynamicDataSource) throws Exception {
curator.getConnectionStateListenable()
.addListener(new ZKConnectionStateListener(dynamicDataSource, this, zkProperties));
log.info("Register ZKstateListenter successfully!");
}
/**
* 根据path获取值
* @param path
* @return
* @throws Exception
*/
public String read(String path) throws Exception {
if (curator.checkExists().forPath(path) == null) {
log.debug("path [{}] is not exists,return null", path);
throw new IllegalArgumentException("Path:" + path + " content is empty, error!");
} else {
log.debug("read from zookeeper,path=" + path);
byte[] b = curator.getData().forPath(path);
String value = new String(b, "utf-8");
return value;
}
}
public List<String> getChildren(String path) throws Exception {
//检查路径是否存在
if (curator.checkExists().forPath(path) == null) {
log.debug("path [{}] is not exists,return null", path);
return null;
} else {
List<String> children = curator.getChildren().forPath(path);
return children;
}
}
public List<String> getDBconfig(String appName) throws Exception {
List<String> result;
result = getChildren(Constant.getAppDB(appName));
if (result.size() == 0 || result == null)
throw new IllegalStateException(appName + " Dbconfig is empty!");
return result;
}
/**
* 获取路径
* @param paths
* @return
* @throws Exception
*/
public String readWithPath(String... paths) throws Exception {
if (paths.length == 0)
return null;
String path = null;
for (String temp : paths) {
if (path == null)
path = Constant.PATH_SPLIT + temp;
else
path = path + Constant.PATH_SPLIT + temp;
}
path = path.replaceAll("//", "/");
return read(path);
}
}
package com.lnb.multidatasourcedemo.curator;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Set;
/**
* zookeeper配置文件
* created by liaoningbo on 2021/11/24
*/
@Data
@Component
@ConfigurationProperties(prefix = "spring.common")
public class ZkProperties {
/**zk集群地址*/
@Value("zkServer")
private String zkServer;
@Value("appName")
private Set<String> appName;
@Value("adminDB")
private String adminDB;
@Value("adminUser")
private String adminUser;
@Value("adminPwd")
private String adminPwd;
/**控制链接主库还是从库,数据库中配置的都是主库的链接信息,true:链接从库,false:链接主库*/
@Value("readOnly")
private String readOnly;
/**链接哪个从库,只有当readOnly为true的时候才需要赋值*/
@Value("slot")
private String slot;
public boolean validateProperties() {
if (StringUtils.isEmpty(zkServer))
return false;
return true;
}
}
package com.lnb.multidatasourcedemo.exception;
/**
* 异常
* Created by liaoningbo on 2021.11.24
*/
public class MultiDatasourceException extends RuntimeException {
public MultiDatasourceException(String msg) {
super("MultiDatasourceException : [" + msg + "]");
}
}
package com.lnb.multidatasourcedemo.holder;
import lombok.extern.slf4j.Slf4j;
/**
* 数据源线程上下文对象.
* Created by liaoningbo on 2021/11/24
*/
@Slf4j
public class DataSourceContextHolder {
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
public static String getDataSourceType() {
log.info("Service get datasource currently is : [" + contextHolder.get() + "]");
return contextHolder.get();
}
public static void setDataSourceType(String groupId) {
contextHolder.set(groupId);
}
public static void clearDataSourceType() {
contextHolder.remove();
}
}
package com.lnb.multidatasourcedemo.listener;
import com.lnb.multidatasourcedemo.curator.CuratorClient;
import com.lnb.multidatasourcedemo.datasource.DynamicDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
/**
* 路由监听
* Created by liaoningbo on 2021/11/24
*/
@Slf4j
public class AppListener implements PathChildrenCacheListener {
private DynamicDataSource dynamicDataSource;
private CuratorClient curatorClient;
public AppListener(DynamicDataSource dynamicDataSource, CuratorClient curatorClient) {
this.dynamicDataSource = dynamicDataSource;
this.curatorClient = curatorClient;
}
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
log.info("AppListener event type : [" + event.getType() + "] ");
switch (event.getType()) {
case CHILD_ADDED:
case CHILD_UPDATED:
registerApp(event);
break;
case CHILD_REMOVED:
break;
default:
break;
}
}
private void registerApp(PathChildrenCacheEvent event) throws Exception {
String app = getApp(event.getData().getPath());
//添加节点时内容可能为空,这时注册节点下路径监听器
if (PathChildrenCacheEvent.Type.CHILD_ADDED.equals(event.getType()))
curatorClient.registerDbconfigListenter(dynamicDataSource, app);
}
private String getApp(String path) {
return path.substring(path.lastIndexOf("/") + 1, path.length());
}
}
package com.lnb.multidatasourcedemo.listener;
import com.lnb.multidatasourcedemo.curator.CuratorClient;
import com.lnb.multidatasourcedemo.datasource.DynamicDataSource;
import com.lnb.multidatasourcedemo.model.DatasourceModel;
import com.lnb.multidatasourcedemo.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
/**
* 数据库配置监听
* Created by liaoningbo on 2021/11/24
*/
@Slf4j
public class DbconfigListener implements PathChildrenCacheListener {
private DynamicDataSource dynamicDataSource;
private CuratorClient curatorClient;
public DbconfigListener(DynamicDataSource dynamicDataSource, CuratorClient curatorClient) {
this.dynamicDataSource = dynamicDataSource;
this.curatorClient = curatorClient;
}
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
log.info("DbconfigListener event type : [" + event.getType() + "] ");
switch (event.getType()) {
case CHILD_ADDED:
case CHILD_UPDATED:
registerDatasource(event);
break;
case CHILD_REMOVED:
removeDatasource(event);
break;
default:
break;
}
}
private void registerDatasource(PathChildrenCacheEvent event) throws Exception {
String dbconfig = curatorClient.read(event.getData().getPath());
//添加节点时内容可能为空,这时注册节点下路径监听器
if (PathChildrenCacheEvent.Type.CHILD_ADDED.equals(event.getType())) {
curatorClient.registerRoutingKeyListenter(dynamicDataSource, getDB(event.getData().getPath()), getApp(event.getData().getPath()));
}
if (StringUtils.isBlank(dbconfig)) {
return;
}
DatasourceModel datasourceModel = JsonUtil.json2object(dbconfig, DatasourceModel.class);
dynamicDataSource.registerDataSource(datasourceModel);
}
private void removeDatasource(PathChildrenCacheEvent event) throws Exception {
String path = event.getData().getPath();
String key = getDB(path);
dynamicDataSource.removeDatasource(key);
}
private String getDB(String path) {
return path.substring(path.lastIndexOf("/") + 1, path.length());
}
private String getApp(String path) {
String[] splits = path.split("/");
if (splits.length >= 4)
return splits[3];
return "";
}
}
package com.lnb.multidatasourcedemo.listener;
import com.lnb.multidatasourcedemo.curator.CuratorClient;
import com.lnb.multidatasourcedemo.datasource.DynamicDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
import java.util.ArrayList;
import java.util.List;
/**
* 路由监听.
* Created by liaoningbo on 2021/11/24
*/
@Slf4j
public class RoutingKeyListener implements PathChildrenCacheListener {
private DynamicDataSource dynamicDataSource;
private CuratorClient curatorClient;
public RoutingKeyListener(DynamicDataSource dynamicDataSource, CuratorClient curatorClient) {
this.dynamicDataSource = dynamicDataSource;
this.curatorClient = curatorClient;
}
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
log.debug("RoutingKeyListener event type : [" + event.getType() + "] ");
switch (event.getType()) {
case INITIALIZED:
List<ChildData> childDataList = event.getInitialData();
if(childDataList == null || childDataList.isEmpty()){
break;
}
String path = childDataList.get(0).getPath();
log.info("APP:{} DB:{} RoutingKey count:{} INITIALIZED finished", getApp(path), getDB(path), childDataList.size());
break;
case CHILD_ADDED:
case CHILD_UPDATED:
registerRouting(event);
break;
case CHILD_REMOVED:
removeRouting(event);
break;
default:
break;
}
}
private void registerRouting(PathChildrenCacheEvent event) throws Exception {
String path = event.getData().getPath();
List<String> routingKeyList = new ArrayList<String>();
routingKeyList.add(getRoutingKey(path));
dynamicDataSource.registerRoutingKey(getDB(path), routingKeyList, getApp(path));
}
private void removeRouting(PathChildrenCacheEvent event) throws Exception {
String path = event.getData().getPath();
dynamicDataSource.removeRoutingKey(getRoutingKey(path));
curatorClient.listenerList.remove(path);
}
private String getApp(String path) {
String[] split = path.split("/");
if (split.length >= 4) {
return split[3];
} else
throw new IllegalStateException("App url is empty!");
}
private String getDB(String path) {
String[] split = path.split("/");
if (split.length >= 5) {
return split[4];
} else
throw new IllegalStateException("DB url is empty!");
}
private String getRoutingKey(String path) {
String[] split = path.split("/");
if (split.length >= 5) {
return split[5];
} else
throw new IllegalStateException("DB routingKey is empty!");
}
}
package com.lnb.multidatasourcedemo.listener;
import com.lnb.multidatasourcedemo.curator.CuratorClient;
import com.lnb.multidatasourcedemo.curator.ZkProperties;
import com.lnb.multidatasourcedemo.datasource.DynamicDataSource;
import com.lnb.multidatasourcedemo.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.state.ConnectionState;
import org.apache.curator.framework.state.ConnectionStateListener;
/**
* zk链接监听
* Created by liaoningbo on 2021/11/24
*/
@Slf4j
public class ZKConnectionStateListener implements ConnectionStateListener {
private DynamicDataSource dynamicDataSource;
private CuratorClient curatorClient;
private ZkProperties zkProperties;
public ZKConnectionStateListener(DynamicDataSource dynamicDataSource, CuratorClient curatorClient, ZkProperties zkProperties) {
this.dynamicDataSource = dynamicDataSource;
this.curatorClient = curatorClient;
this.zkProperties = zkProperties;
}
@Override
public void stateChanged(CuratorFramework curatorFramework, ConnectionState connectionState) {
log.info("ZKConnectionStateListener connectionState : [" + connectionState + "] ");
if (connectionState == ConnectionState.LOST || connectionState == ConnectionState.SUSPENDED) {
//连接丢失,报警
log.warn("Lost session with zookeeper at[" + CommonUtils.getDateByDefault() + "].");
} else if (connectionState == ConnectionState.CONNECTED) {
//连接新建
log.info("Connected with zookeeper");
} else if (connectionState == ConnectionState.RECONNECTED) {
log.info("Reconnected with zookeeper, don't care.");
}
}
}
package com.lnb.multidatasourcedemo.model;
import lombok.Data;
/**
* 数据库链接信息实体.
* Created by liaoningbo on 2021/11/24
*/
@Data
public class DatasourceModel {
private String username;
private String pwd;
private String url;
private String commonDB;
private String urlPrefix;
private String initialSize;
private String maxActive;
private String minIdle;
private String maxWait;
private String testOnBorrow;
private String testOnReturn;
private String testWhileIdle;
private String timeBetweenEvictionRunsMillis;
private String minEvictableIdleTimeMillis;
private String removeAbandoned;
private String removeAbandonedTimeout;
private String logAbandoned;
private String validationQuery;
public boolean validateDatasourceModel() {
if ("".equals(username) || null == username)
return false;
if ("".equals(pwd) || null == pwd)
return false;
if ("".equals(url) || null == url)
return false;
if ("".equals(commonDB) || null == commonDB)
return false;
if ("".equals(urlPrefix) || null == urlPrefix)
return false;
return true;
}
}
package com.lnb.multidatasourcedemo.model;
import lombok.Data;
/**
* 路由实体.
* Created by liaoningbo on 2021/11/24
*/
@Data
public class RoutingKey2DB {
private String url;
private String databaseName;
public RoutingKey2DB(String url, String databaseName) {
this.url = url;
this.databaseName = databaseName;
}
@Override
public String toString() {
StringBuilder buf = new StringBuilder();
buf.append("{");
buf.append("\n\turl:");
buf.append(this.getUrl());
buf.append(",\n\tdatabaseName:");
buf.append(this.getDatabaseName());
buf.append("\n\t}");
return buf.toString();
}
}
package com.lnb.multidatasourcedemo.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by liaoningbo on 2021/11/24
*/
public class CommonUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(CommonUtils.class);
/**
* 获取当前日期,默认格式为"yyyy-MM-dd HH:mm:ss"
*/
public static String getDateByDefault() {
return getDateByFormat("yyyy-MM-dd HH:mm:ss");
}
/**
* 获取当前日期,按照所传格式
*/
public static String getDateByFormat(String format) {
Date date = new Date();
DateFormat df = new SimpleDateFormat(format);
String time = df.format(date);
return time;
}
public static String getLocalIP() throws UnknownHostException {
InetAddress addr = InetAddress.getLocalHost();
String ip = addr.getHostAddress().toString();//获得本机IP  
return ip;
}
public static String getAbsolutePath() {
URL url = CommonUtils.class.getProtectionDomain().getCodeSource().getLocation();
String filePath = null;
try {
filePath = java.net.URLDecoder.decode(url.getPath(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
if (filePath.endsWith(".jar"))
filePath = filePath.substring(0, filePath.lastIndexOf("/") + 1);
File file = new File(filePath);
filePath = file.getAbsolutePath();
return filePath;
}
public static String filterString(String str) {
return str.replaceAll("//", "/");
}
}
package com.lnb.multidatasourcedemo.utils;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.joda.JodaModule;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
@Slf4j
public class JsonUtil {
static ObjectMapper objectMapper = new ObjectMapper();
static {
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.registerModule(new JodaModule());
}
@SuppressWarnings({"unchecked", "rawtypes"})
public static <T> T json2object(String _json, Class cls) {
try {
if (_json == null || _json.equals(""))
_json = "{}";
return (T) objectMapper.readValue(_json, cls);
} catch (IOException e) {
log.warn("json2object error", e);
}
return null;
}
public static String object2json(Object obj) {
try {
return objectMapper.writeValueAsString(obj);
} catch (IOException e) {
log.warn("object2json error", e);
}
return null;
}
public static String object2jsonWithoutNull(Object obj) {
String json = "";
try {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
json = mapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return json;
}
public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
return objectMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
}
}
差异被折叠。
@REM ----------------------------------------------------------------------------
@REM Licensed to the Apache Software Foundation (ASF) under one
@REM or more contributor license agreements. See the NOTICE file
@REM distributed with this work for additional information
@REM regarding copyright ownership. The ASF licenses this file
@REM to you under the Apache License, Version 2.0 (the
@REM "License"); you may not use this file except in compliance
@REM with the License. You may obtain a copy of the License at
@REM
@REM https://www.apache.org/licenses/LICENSE-2.0
@REM
@REM Unless required by applicable law or agreed to in writing,
@REM software distributed under the License is distributed on an
@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@REM KIND, either express or implied. See the License for the
@REM specific language governing permissions and limitations
@REM under the License.
@REM ----------------------------------------------------------------------------
@REM ----------------------------------------------------------------------------
@REM Maven Start Up Batch script
@REM
@REM Required ENV vars:
@REM JAVA_HOME - location of a JDK home dir
@REM
@REM Optional ENV vars
@REM M2_HOME - location of maven2's installed home dir
@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands
@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending
@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven
@REM e.g. to debug Maven itself, use
@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files
@REM ----------------------------------------------------------------------------
@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on'
@echo off
@REM set title of command window
title %0
@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on'
@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO%
@REM set %HOME% to equivalent of $HOME
if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%")
@REM Execute a user defined script before this one
if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre
@REM check for pre script, once with legacy .bat ending and once with .cmd ending
if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat"
if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd"
:skipRcPre
@setlocal
set ERROR_CODE=0
@REM To isolate internal variables from possible post scripts, we use another setlocal
@setlocal
@REM ==== START VALIDATION ====
if not "%JAVA_HOME%" == "" goto OkJHome
echo.
echo Error: JAVA_HOME not found in your environment. >&2
echo Please set the JAVA_HOME variable in your environment to match the >&2
echo location of your Java installation. >&2
echo.
goto error
:OkJHome
if exist "%JAVA_HOME%\bin\java.exe" goto init
echo.
echo Error: JAVA_HOME is set to an invalid directory. >&2
echo JAVA_HOME = "%JAVA_HOME%" >&2
echo Please set the JAVA_HOME variable in your environment to match the >&2
echo location of your Java installation. >&2
echo.
goto error
@REM ==== END VALIDATION ====
:init
@REM Find the project base dir, i.e. the directory that contains the folder ".mvn".
@REM Fallback to current working directory if not found.
set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR%
IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir
set EXEC_DIR=%CD%
set WDIR=%EXEC_DIR%
:findBaseDir
IF EXIST "%WDIR%"\.mvn goto baseDirFound
cd ..
IF "%WDIR%"=="%CD%" goto baseDirNotFound
set WDIR=%CD%
goto findBaseDir
:baseDirFound
set MAVEN_PROJECTBASEDIR=%WDIR%
cd "%EXEC_DIR%"
goto endDetectBaseDir
:baseDirNotFound
set MAVEN_PROJECTBASEDIR=%EXEC_DIR%
cd "%EXEC_DIR%"
:endDetectBaseDir
IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig
@setlocal EnableExtensions EnableDelayedExpansion
for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a
@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS%
:endReadAdditionalConfig
SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe"
set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar"
set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO (
IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B
)
@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central
@REM This allows using the maven wrapper in projects that prohibit checking in binary data.
if exist %WRAPPER_JAR% (
if "%MVNW_VERBOSE%" == "true" (
echo Found %WRAPPER_JAR%
)
) else (
if not "%MVNW_REPOURL%" == "" (
SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
)
if "%MVNW_VERBOSE%" == "true" (
echo Couldn't find %WRAPPER_JAR%, downloading it ...
echo Downloading from: %DOWNLOAD_URL%
)
powershell -Command "&{"^
"$webclient = new-object System.Net.WebClient;"^
"if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^
"$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^
"}"^
"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^
"}"
if "%MVNW_VERBOSE%" == "true" (
echo Finished downloading %WRAPPER_JAR%
)
)
@REM End of extension
@REM Provide a "standardized" way to retrieve the CLI args that will
@REM work with both Windows and non-Windows executions.
set MAVEN_CMD_LINE_ARGS=%*
%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %*
if ERRORLEVEL 1 goto error
goto end
:error
set ERROR_CODE=1
:end
@endlocal & set ERROR_CODE=%ERROR_CODE%
if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost
@REM check for post script, once with legacy .bat ending and once with .cmd ending
if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat"
if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd"
:skipRcPost
@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on'
if "%MAVEN_BATCH_PAUSE%" == "on" pause
if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE%
exit /B %ERROR_CODE%
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lnb</groupId>
<artifactId>multi-datasource-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>multi-datasource-demo</name>
<description>multi-datasource-demo</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.lnb</groupId>
<artifactId>multidatasource</artifactId>
<version>1.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.lnb.multidatasourcedemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages={"com.lnb.multidatasourcedemo", "com.lnb.multidatasource"})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
package com.lnb.multidatasourcedemo.controller;
import com.lnb.multidatasourcedemo.service.RouterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RouterController {
@Autowired
private RouterService routerService;
@GetMapping("/goodsale")
public Object goodsale(@RequestParam String tenantID){
try {
String obj = routerService.goodsale(tenantID);
return "城市名称:" + obj;
}catch (Exception e){
return "tenantID:" + tenantID + " 没找到路由";
}
}
@GetMapping("/order")
public Object order(@RequestParam String tenantID){
try{
String obj = routerService.order(tenantID);
return "商品名称:" + obj;
}catch (Exception e){
return "tenantID:" + tenantID + " 没找到路由";
}
}
@GetMapping("/stock")
public Object stock(@RequestParam String tenantID){
try{
String obj = routerService.stock(tenantID);
return "供应商名称:" + obj;
}catch (Exception e){
return "tenantID:" + tenantID + " 没找到路由";
}
}
}
package com.lnb.multidatasourcedemo.dao;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface RouterDao {
String goodsale();
String order();
String stock();
}
package com.lnb.multidatasourcedemo.service;
import com.lnb.multidatasourcedemo.dao.RouterDao;
import com.lnb.multidatasourcedemo.holder.DataSourceContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class RouterService {
@Autowired
private RouterDao routerDao;
public String goodsale(String tenantID) throws Exception {
String appName = "goodsale";
String key = appName + "-tenantID-" + tenantID;
DataSourceContextHolder.setDataSourceType(key);
String name = routerDao.goodsale();
return name;
}
public String order(String tenantID) throws Exception {
String appName = "order";
String key = appName + "-tenantID-" + tenantID;
DataSourceContextHolder.setDataSourceType(key);
String str = routerDao.order();
return str;
}
public String stock(String tenantID) throws Exception {
String appName = "stock";
String key = appName + "-tenantID-" + tenantID;
DataSourceContextHolder.setDataSourceType(key);
String str = routerDao.stock();
return str;
}
}
spring:
common:
zkServer: 127.0.0.1:2181
appName: goodsale,order,stock
adminDB: jdbc:mysql://127.0.0.1:3306/lnb?autoReconnect=true&useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&useSSL=false
adminUser: root
adminPwd: lnb199199
readOnly: false
slot:
mybatis:
configuration:
map-underscore-to-camel-case: true
mapper-locations: classpath:mapper/*.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lnb.multidatasourcedemo.dao.RouterDao">
<select id="goodsale" resultType="java.lang.String">
select name
from city
where id = 1
</select>
<select id="order" resultType="java.lang.String">
select name
from goods
where id = 1035
</select>
<select id="stock" resultType="java.lang.String">
select supplier_name
from supplier_info
where id = 1
</select>
</mapper>
\ No newline at end of file
package com.lnb.multidatasourcedemo;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class MultiDatasourceDemoApplicationTests {
@Test
void contextLoads() {
}
}
spring:
common:
zkServer: 127.0.0.1:2181
appName: goodsale,order,stock
adminDB: jdbc:mysql://127.0.0.1:3306/lnb?autoReconnect=true&useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&useSSL=false
adminUser: root
adminPwd: lnb199199
readOnly: false
slot:
mybatis:
configuration:
map-underscore-to-camel-case: true
mapper-locations: classpath:mapper/*.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lnb.multidatasourcedemo.dao.RouterDao">
<select id="goodsale" resultType="java.lang.String">
select name
from city
where id = 1
</select>
<select id="order" resultType="java.lang.String">
select name
from goods
where id = 1035
</select>
<select id="stock" resultType="java.lang.String">
select supplier_name
from supplier_info
where id = 1
</select>
</mapper>
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论