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

first commit

上级
流水线 #1277 已取消 于阶段
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="web" name="Web">
<configuration>
<webroots />
<sourceRoots>
<root url="file://$MODULE_DIR$/src/main/java" />
<root url="file://$MODULE_DIR$/src/main/resources" />
</sourceRoots>
</configuration>
</facet>
<facet type="Spring" name="Spring">
<configuration />
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:2.3.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:2.3.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-context:5.2.9.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-autoconfigure:2.3.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-logging:2.3.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: ch.qos.logback:logback-classic:1.2.3" level="project" />
<orderEntry type="library" name="Maven: ch.qos.logback:logback-core:1.2.3" level="project" />
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-to-slf4j:2.13.3" level="project" />
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-api:2.13.3" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:jul-to-slf4j:1.7.30" level="project" />
<orderEntry type="library" name="Maven: jakarta.annotation:jakarta.annotation-api:1.3.5" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-core:5.2.9.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-jcl:5.2.9.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.26" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-web:2.3.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-json:2.3.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.11.2" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.11.2" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.11.2" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.module:jackson-module-parameter-names:2.11.2" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-tomcat:2.3.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-core:9.0.38" level="project" />
<orderEntry type="library" name="Maven: org.glassfish:jakarta.el:3.0.3" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-websocket:9.0.38" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-web:5.2.9.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-beans:5.2.9.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-webmvc:5.2.9.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-aop:5.2.9.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-expression:5.2.9.RELEASE" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-starter-test:2.3.4.RELEASE" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-test:2.3.4.RELEASE" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-test-autoconfigure:2.3.4.RELEASE" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: com.jayway.jsonpath:json-path:2.4.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: net.minidev:json-smart:2.3" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: net.minidev:accessors-smart:1.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.ow2.asm:asm:5.0.4" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.30" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: jakarta.xml.bind:jakarta.xml.bind-api:2.3.3" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: jakarta.activation:jakarta.activation-api:1.2.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.assertj:assertj-core:3.16.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest:2.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter:5.6.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-api:5.6.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.opentest4j:opentest4j:1.2.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-commons:1.6.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-params:5.6.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-engine:5.6.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.vintage:junit-vintage-engine:5.6.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.apiguardian:apiguardian-api:1.1.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-engine:1.6.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.13" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-core:3.3.3" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: net.bytebuddy:byte-buddy:1.10.14" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: net.bytebuddy:byte-buddy-agent:1.10.14" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.objenesis:objenesis:2.6" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-junit-jupiter:3.3.3" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.skyscreamer:jsonassert:1.5.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: com.vaadin.external.google:android-json:0.0.20131108.vaadin1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.springframework:spring-test:5.2.9.RELEASE" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.xmlunit:xmlunit-core:2.7.0" level="project" />
<orderEntry type="library" name="Maven: org.projectlombok:lombok:1.18.12" level="project" />
<orderEntry type="library" name="Maven: org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.0" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-jdbc:2.3.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: com.zaxxer:HikariCP:3.4.5" level="project" />
<orderEntry type="library" name="Maven: org.mybatis.spring.boot:mybatis-spring-boot-autoconfigure:2.0.0" level="project" />
<orderEntry type="library" name="Maven: org.mybatis:mybatis:3.5.0" level="project" />
<orderEntry type="library" name="Maven: org.mybatis:mybatis-spring:2.0.0" level="project" />
<orderEntry type="module" module-name="multidatasource" />
<orderEntry type="library" scope="RUNTIME" name="Maven: mysql:mysql-connector-java:8.0.21" level="project" />
<orderEntry type="library" name="Maven: com.alibaba:druid-spring-boot-starter:1.1.10" level="project" />
<orderEntry type="library" name="Maven: com.alibaba:druid:1.1.10" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.10" level="project" />
<orderEntry type="library" name="Maven: org.apache.curator:curator-recipes:2.7.0" level="project" />
<orderEntry type="library" name="Maven: org.apache.curator:curator-framework:2.7.0" level="project" />
<orderEntry type="library" name="Maven: org.apache.curator:curator-client:2.7.0" level="project" />
<orderEntry type="library" name="Maven: org.apache.zookeeper:zookeeper:3.4.6" level="project" />
<orderEntry type="library" name="Maven: log4j:log4j:1.2.16" level="project" />
<orderEntry type="library" name="Maven: jline:jline:0.9.94" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty:3.7.0.Final" level="project" />
<orderEntry type="library" name="Maven: com.google.guava:guava:16.0.1" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-jdbc:5.2.9.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-tx:5.2.9.RELEASE" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-joda:2.11.2" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.11.2" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.11.2" level="project" />
<orderEntry type="library" name="Maven: joda-time:joda-time:2.9.9" level="project" />
</component>
</module>
\ No newline at end of file
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
#!/bin/sh
# ----------------------------------------------------------------------------
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
# ----------------------------------------------------------------------------
# ----------------------------------------------------------------------------
# Maven Start Up Batch script
#
# Required ENV vars:
# ------------------
# JAVA_HOME - location of a JDK home dir
#
# Optional ENV vars
# -----------------
# M2_HOME - location of maven2's installed home dir
# MAVEN_OPTS - parameters passed to the Java VM when running Maven
# e.g. to debug Maven itself, use
# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
# MAVEN_SKIP_RC - flag to disable loading of mavenrc files
# ----------------------------------------------------------------------------
if [ -z "$MAVEN_SKIP_RC" ] ; then
if [ -f /etc/mavenrc ] ; then
. /etc/mavenrc
fi
if [ -f "$HOME/.mavenrc" ] ; then
. "$HOME/.mavenrc"
fi
fi
# OS specific support. $var _must_ be set to either true or false.
cygwin=false;
darwin=false;
mingw=false
case "`uname`" in
CYGWIN*) cygwin=true ;;
MINGW*) mingw=true;;
Darwin*) darwin=true
# Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home
# See https://developer.apple.com/library/mac/qa/qa1170/_index.html
if [ -z "$JAVA_HOME" ]; then
if [ -x "/usr/libexec/java_home" ]; then
export JAVA_HOME="`/usr/libexec/java_home`"
else
export JAVA_HOME="/Library/Java/Home"
fi
fi
;;
esac
if [ -z "$JAVA_HOME" ] ; then
if [ -r /etc/gentoo-release ] ; then
JAVA_HOME=`java-config --jre-home`
fi
fi
if [ -z "$M2_HOME" ] ; then
## resolve links - $0 may be a link to maven's home
PRG="$0"
# need this for relative symlinks
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG="`dirname "$PRG"`/$link"
fi
done
saveddir=`pwd`
M2_HOME=`dirname "$PRG"`/..
# make it fully qualified
M2_HOME=`cd "$M2_HOME" && pwd`
cd "$saveddir"
# echo Using m2 at $M2_HOME
fi
# For Cygwin, ensure paths are in UNIX format before anything is touched
if $cygwin ; then
[ -n "$M2_HOME" ] &&
M2_HOME=`cygpath --unix "$M2_HOME"`
[ -n "$JAVA_HOME" ] &&
JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
[ -n "$CLASSPATH" ] &&
CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
fi
# For Mingw, ensure paths are in UNIX format before anything is touched
if $mingw ; then
[ -n "$M2_HOME" ] &&
M2_HOME="`(cd "$M2_HOME"; pwd)`"
[ -n "$JAVA_HOME" ] &&
JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`"
fi
if [ -z "$JAVA_HOME" ]; then
javaExecutable="`which javac`"
if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then
# readlink(1) is not available as standard on Solaris 10.
readLink=`which readlink`
if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then
if $darwin ; then
javaHome="`dirname \"$javaExecutable\"`"
javaExecutable="`cd \"$javaHome\" && pwd -P`/javac"
else
javaExecutable="`readlink -f \"$javaExecutable\"`"
fi
javaHome="`dirname \"$javaExecutable\"`"
javaHome=`expr "$javaHome" : '\(.*\)/bin'`
JAVA_HOME="$javaHome"
export JAVA_HOME
fi
fi
fi
if [ -z "$JAVACMD" ] ; then
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD="$JAVA_HOME/jre/sh/java"
else
JAVACMD="$JAVA_HOME/bin/java"
fi
else
JAVACMD="`which java`"
fi
fi
if [ ! -x "$JAVACMD" ] ; then
echo "Error: JAVA_HOME is not defined correctly." >&2
echo " We cannot execute $JAVACMD" >&2
exit 1
fi
if [ -z "$JAVA_HOME" ] ; then
echo "Warning: JAVA_HOME environment variable is not set."
fi
CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher
# traverses directory structure from process work directory to filesystem root
# first directory with .mvn subdirectory is considered project base directory
find_maven_basedir() {
if [ -z "$1" ]
then
echo "Path not specified to find_maven_basedir"
return 1
fi
basedir="$1"
wdir="$1"
while [ "$wdir" != '/' ] ; do
if [ -d "$wdir"/.mvn ] ; then
basedir=$wdir
break
fi
# workaround for JBEAP-8937 (on Solaris 10/Sparc)
if [ -d "${wdir}" ]; then
wdir=`cd "$wdir/.."; pwd`
fi
# end of workaround
done
echo "${basedir}"
}
# concatenates all lines of a file
concat_lines() {
if [ -f "$1" ]; then
echo "$(tr -s '\n' ' ' < "$1")"
fi
}
BASE_DIR=`find_maven_basedir "$(pwd)"`
if [ -z "$BASE_DIR" ]; then
exit 1;
fi
##########################################################################################
# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central
# This allows using the maven wrapper in projects that prohibit checking in binary data.
##########################################################################################
if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then
if [ "$MVNW_VERBOSE" = true ]; then
echo "Found .mvn/wrapper/maven-wrapper.jar"
fi
else
if [ "$MVNW_VERBOSE" = true ]; then
echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..."
fi
if [ -n "$MVNW_REPOURL" ]; then
jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
else
jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
fi
while IFS="=" read key value; do
case "$key" in (wrapperUrl) jarUrl="$value"; break ;;
esac
done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties"
if [ "$MVNW_VERBOSE" = true ]; then
echo "Downloading from: $jarUrl"
fi
wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar"
if $cygwin; then
wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"`
fi
if command -v wget > /dev/null; then
if [ "$MVNW_VERBOSE" = true ]; then
echo "Found wget ... using wget"
fi
if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then
wget "$jarUrl" -O "$wrapperJarPath"
else
wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath"
fi
elif command -v curl > /dev/null; then
if [ "$MVNW_VERBOSE" = true ]; then
echo "Found curl ... using curl"
fi
if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then
curl -o "$wrapperJarPath" "$jarUrl" -f
else
curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f
fi
else
if [ "$MVNW_VERBOSE" = true ]; then
echo "Falling back to using Java to download"
fi
javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java"
# For Cygwin, switch paths to Windows format before running javac
if $cygwin; then
javaClass=`cygpath --path --windows "$javaClass"`
fi
if [ -e "$javaClass" ]; then
if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then
if [ "$MVNW_VERBOSE" = true ]; then
echo " - Compiling MavenWrapperDownloader.java ..."
fi
# Compiling the Java class
("$JAVA_HOME/bin/javac" "$javaClass")
fi
if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then
# Running the downloader
if [ "$MVNW_VERBOSE" = true ]; then
echo " - Running MavenWrapperDownloader.java ..."
fi
("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR")
fi
fi
fi
fi
##########################################################################################
# End of extension
##########################################################################################
export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"}
if [ "$MVNW_VERBOSE" = true ]; then
echo $MAVEN_PROJECTBASEDIR
fi
MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS"
# For Cygwin, switch paths to Windows format before running java
if $cygwin; then
[ -n "$M2_HOME" ] &&
M2_HOME=`cygpath --path --windows "$M2_HOME"`
[ -n "$JAVA_HOME" ] &&
JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
[ -n "$CLASSPATH" ] &&
CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
[ -n "$MAVEN_PROJECTBASEDIR" ] &&
MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"`
fi
# Provide a "standardized" way to retrieve the CLI args that will
# work with both Windows and non-Windows executions.
MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@"
export MAVEN_CMD_LINE_ARGS
WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
exec "$JAVACMD" \
$MAVEN_OPTS \
-classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \
"-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@"
@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.datasource;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.lnb.multidatasourcedemo.constant.Constant;
import com.lnb.multidatasourcedemo.curator.ZkProperties;
import com.lnb.multidatasourcedemo.model.DatasourceModel;
import com.lnb.multidatasourcedemo.model.RoutingKey2DB;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@Component
@Slf4j
public class DbAdapter {
@Autowired
private ZkProperties properties;
private DruidDataSource dataSource;
/* 配置jdbc url */
public static final String SOCKET = "socketTimeout=";
/* 配置jdbc socket timeout 60s */
public static final String TIMEOUT = "60000";
private Map<String, DbUrl> db_ID = new HashMap<>();
/**
* 初始化数据源
* @param map
*/
public void initDatasource(Map<String, DruidDataSource> map) {
log.info("initDatasource start");
DruidPooledConnection druidConn = null;
Connection conn = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
int index = 1;
//获取配置文件中的appName,然后查询对应的数据库链接信息
Set<String> appNameSet = properties.getAppName();
String query = "SELECT dbID, appName, address, port, dbName, username, password FROM tbl_sys_db";
String condition = " where appName in (";
String order = " ORDER BY dbID;";
String queryCombine = createDbQuery(query, condition, order, appNameSet);
try {
druidConn = dataSource.getConnection();
conn = druidConn.getConnection();
preparedStatement = conn.prepareStatement(queryCombine);
for (String appName : appNameSet) {
preparedStatement.setString(index, appName);
index++;
}
resultSet = preparedStatement.executeQuery();
DbUrl dbUrl;
while (resultSet.next()) {
String dbID = resultSet.getString("dbID");
String address = resultSet.getString("address");
String port = resultSet.getString("port");
String username = resultSet.getString("username");
String password = resultSet.getString("password");
String dbName = resultSet.getString("dbName");
String url = address + ":" + port;
DatasourceModel datasourceModel = new DatasourceModel();
datasourceModel.setUrl(url);
datasourceModel.setCommonDB("?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&rewriteBatchedStatements=true");
datasourceModel.setUrlPrefix("jdbc:mysql://");
datasourceModel.setUsername(username);
datasourceModel.setPwd(password);
putDataSource(map, datasourceModel, properties);
//把appName和数据库连接地址的关系存起来,处理routingKeys的时候获取他两之间的关系
dbUrl = new DbUrl();
dbUrl.setDbName(dbName);
dbUrl.setUrl(url);
db_ID.put(dbID, dbUrl);
}
log.info("initDatasource finished successfully");
} catch (SQLException e) {
log.error("read tbl_sys_db hit error!", e);
} finally {
close(druidConn, conn, preparedStatement, resultSet);
}
}
/**
* 关闭数据库连接
* @param druidConn
* @param conn
* @param statement
* @param resultSet
*/
private void close(DruidPooledConnection druidConn, Connection conn, PreparedStatement statement, ResultSet resultSet) {
try {
if (resultSet != null)
resultSet.close();
if (statement != null)
statement.close();
if (conn != null)
conn.close();
if (druidConn != null)
druidConn.close();
} catch (SQLException e) {
log.error("close datasource resource hit error!", e);
}
}
/**
* 给数据源的map中赋值
*
* @param dsMap
* @param newDs
* @param properties
*/
public static void putDataSource(Map<String, DruidDataSource> dsMap, DatasourceModel newDs, ZkProperties properties) {
if (dsMap != null) {
if (dsMap.isEmpty() || !dsMap.containsKey(newDs.getUrl())) {
DruidDataSource dataSource = DynamicDataSource.buildDatasource(newDs, properties);
dsMap.put(newDs.getUrl(), dataSource);
} else {
if (!compareDs(dsMap.get(newDs.getUrl()), newDs, properties)) {
DruidDataSource newItem = DynamicDataSource.buildDatasource(newDs, properties);
dsMap.get(newDs.getUrl()).close();
dsMap.remove(newDs.getUrl());
dsMap.put(newDs.getUrl(), newItem);
log.info("=======CAS new ds, close and remove old ds:{}========", newDs.getUrl());
}
}
}
}
/**
* 比较数据源是否一致
* @param oldDs
* @param newDs
* @param properties
* @return
*/
public static boolean compareDs(DruidDataSource oldDs, DatasourceModel newDs, ZkProperties properties) {
if (oldDs == null && newDs != null)
return false;
if (oldDs != null && newDs != null) {
if (!oldDs.getUsername().equals(newDs.getUsername()))
return false;
if (!oldDs.getPassword().equals(newDs.getPwd()))
return false;
String url = DynamicDataSource.makeDsUrl(newDs, properties);
if (!oldDs.getUrl().equals(url))
return false;
}
return true;
}
/**
* 组装查询条件
* @param query
* @param condition
* @param order
* @param appNameSet
* @return
*/
private static String createDbQuery(String query, String condition, String order, Set<String> appNameSet) {
StringBuilder queryBuilder = new StringBuilder(query);
if (!appNameSet.isEmpty()) {
queryBuilder.append(condition);
for (int i = 0; i < appNameSet.size(); i++) {
queryBuilder.append(" ?");
if (i != appNameSet.size() - 1)
queryBuilder.append(",");
}
queryBuilder.append(")");
}
queryBuilder.append(order);
return queryBuilder.toString();
}
/**
* 对JDBC url进行增强
* @param url
* @return
*/
public static String makeJdbcUrl(String url) {
if (StringUtils.isNotBlank(url)) {
if (!url.contains(SOCKET)) {
String s = "";
if (url.contains("?")) {
s = "&";
} else {
s = "?";
}
url += s + SOCKET + TIMEOUT;
}
}
return url;
}
/**
* 初始化routingKeys
* @param routingKeys
*/
public void initRouting(Map<String, RoutingKey2DB> routingKeys) {
log.info("initRouting start");
DruidPooledConnection druidConn = null;
Connection conn = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
int index = 1;
//从数据库中查询路由关系
Set<String> appNameSet = properties.getAppName();
String query = "SELECT tbl_sys_db.dbID as dbID, tbl_sys_db.appName as dbAppName, tbl_sys_db_route.routeKey as routeKey "
+ "FROM tbl_sys_db JOIN tbl_sys_db_route ON tbl_sys_db.dbID=tbl_sys_db_route.dbID "
+ "AND tbl_sys_db_route.action != 2";
String condition = " AND tbl_sys_db.appName in (";
String order = " ORDER BY tbl_sys_db.dbID, tbl_sys_db_route.routeKey;";
String queryCombine = createDbQuery(query, condition, order, appNameSet);
try {
druidConn = dataSource.getConnection();
conn = druidConn.getConnection();
preparedStatement = conn.prepareStatement(queryCombine);
for (String appName : appNameSet) {
preparedStatement.setString(index, appName);
index++;
}
resultSet = preparedStatement.executeQuery();
RoutingKey2DB routingKey2DB;
while (resultSet.next()) {
String dbID = resultSet.getString("dbID");
String routeKey = resultSet.getString("routeKey");
String dbAppName = resultSet.getString("dbAppName");
//给routingKeys中赋值
DbUrl dbUrl = db_ID.get(dbID);
routingKey2DB = new RoutingKey2DB(dbUrl.getUrl(), dbUrl.getDbName());
routingKeys.put(dbAppName + "-tenantID-" + routeKey, routingKey2DB);
}
log.info("initRouting finished successfully");
} catch (SQLException e) {
log.error("read routing key hit error!", e);
} finally {
close(druidConn, conn, preparedStatement, resultSet);
}
}
/**
* 启动的时候初始化数据库连接
*/
@PostConstruct
public void init() {
dataSource = new DruidDataSource();
dataSource.setDriverClassName(Constant.DRIVER_CLASS_NAME);
dataSource.setUsername(properties.getAdminUser());
dataSource.setPassword(properties.getAdminPwd());
dataSource.setUrl(makeJdbcUrl(properties.getAdminDB()));
dataSource.setInitialSize(2);
dataSource.setMinIdle(1);
dataSource.setMaxActive(5);
dataSource.setMaxWait(120000);
dataSource.setValidationQuery("SELECT 1");
// 设置检测空闲最大时长
dataSource.setTimeBetweenEvictionRunsMillis(60000);
// 空闲时间超过上值时长,测试该连接是否可用
dataSource.setTestWhileIdle(true);
// 长时间空闲连接回收
dataSource.setRemoveAbandoned(true);
// 设置超过5分钟未使用的连接强制关闭
dataSource.setRemoveAbandonedTimeout(300);
// 在取出连接时检查connection的可用性,可保证连接的绝对可用性,如不可用则换其他connection,但是较影响性能
dataSource.setTestOnBorrow(true);
}
}
@Data
class DbUrl {
private String url;
private String dbName;
}
package com.lnb.multidatasourcedemo.datasource;
import com.alibaba.druid.pool.DruidDataSource;
import com.google.common.collect.Lists;
import com.lnb.multidatasourcedemo.constant.Constant;
import com.lnb.multidatasourcedemo.curator.CuratorClient;
import com.lnb.multidatasourcedemo.curator.ZkProperties;
import com.lnb.multidatasourcedemo.exception.MultiDatasourceException;
import com.lnb.multidatasourcedemo.holder.DataSourceContextHolder;
import com.lnb.multidatasourcedemo.model.DatasourceModel;
import com.lnb.multidatasourcedemo.model.RoutingKey2DB;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.AbstractDataSource;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
@Slf4j
@Primary
@Component
public class DynamicDataSource extends AbstractDataSource {
@Autowired
private DbAdapter dbAdapter;
@Autowired
private CuratorClient curatorClient;
@Autowired
private ZkProperties zkProperties;
private static Map<String, RoutingKey2DB> routingKeys = new ConcurrentHashMap<>();
private static Map<String, DruidDataSource> dataSources = new ConcurrentHashMap<>();
private static Object monitorDataSource = new Object();
/**
* 启动时路由加载主流程
*/
@PostConstruct
public void initDynamicDataSource() {
try {
// 初始化数据源,结构是:{url, 数据库连接信息}
dbAdapter.initDatasource(dataSources);
//初始化routingkeys,结构是:{租户ID, url}
dbAdapter.initRouting(routingKeys);
// 通过读ZK节点添加监听器
initListener(true);
log.info("init Dynamic DataSource done!");
} catch (Exception e) {
log.error("Init zookeeper error!", e);
throw new IllegalStateException("Init zookeeper error!");
}
}
/**
* 通过读ZK节点添加监听器
*/
public void initListener(boolean isInit) throws Exception {
log.info("init all zookeeper node Listener start!");
//监听
curatorClient.registerAppListenter(this);
List<AppEntity> allAppDbLists = Lists.newArrayList();
//获取/dynamicDataSource/dbconfig节点下的所有子节点
List<String> apps = curatorClient.getChildren(Constant.DBCONFIG_ROOT);
for (String appName : apps) {
if (checkAppName(appName, zkProperties.getAppName())) {
// 获取所有数据库实例
List<String> dbs = curatorClient.getDBconfig(appName);
for (String db : dbs) {
AppEntity appEntity = new AppEntity(appName, db);
allAppDbLists.add(appEntity);
}
curatorClient.registerDbconfigListenter(this, appName);
}
}
log.info("register RoutingKey Listener start");
int allAppDbListSize = allAppDbLists.size();
CountDownLatch countDownLatch = new CountDownLatch(allAppDbListSize);
for (AppEntity appDb : allAppDbLists) {
Thread thread = new Thread(new Participant(curatorClient, this, appDb.getDbName(), appDb.getAppName(), countDownLatch));
thread.start();
}
countDownLatch.await();
log.info("register RoutingKey Listener finished successfully");
if (isInit) {
curatorClient.registerZKstateListenter(this);
}
log.info("init all zookeeper node Listener finished successfully!");
}
/**
* 只订阅properties配置的appName,不配则订阅全部
*
* @param name zk上DBCONFIG_ROOT下的appName
* @return
*/
public static boolean checkAppName(String name, Set<String> setting) {
boolean r = true;
if (StringUtils.isBlank(name)) {
r = false;
} else {
if (setting != null && !setting.isEmpty() && !setting.contains(name)) {
r = false;
}
}
return r;
}
/**
* 最终执行查询时连接数据库
* @return
* @throws SQLException
*/
@Override
public Connection getConnection() throws SQLException {
DataSource dataSource;
String dbName;
log.debug("start to get connection.......");
String dataSourceType = DataSourceContextHolder.getDataSourceType();
log.debug("Current datasource type is [" + dataSourceType + "]");
if (StringUtils.isBlank(dataSourceType)) {
throw new MultiDatasourceException("Please set datasource type first!");
}
if (!routingKeys.containsKey(dataSourceType)) {
throw new MultiDatasourceException("Datasource type is error! key:" + dataSourceType);
}
RoutingKey2DB key = routingKeys.get(dataSourceType);
dbName = key.getDatabaseName();
dataSource = dataSources.get(key.getUrl());
if (dataSource == null)
throw new MultiDatasourceException("Local key " + dataSourceType + " not exist!");
Connection connection = dataSource.getConnection();
// 切换数据库实例下实际DB名称
connection.setCatalog(dbName);
log.debug("end to get connection!");
return connection;
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
throw new UnsupportedOperationException("Could not get datasource by username!");
}
/**
* 创建一个数据源
* @param dataSourceModel
* @param properties
* @return
*/
public static DruidDataSource buildDatasource(DatasourceModel dataSourceModel, ZkProperties properties) {
DruidDataSource ds = new DruidDataSource();
if (!dataSourceModel.validateDatasourceModel()) {
log.error("dataSourceModel is not valid:{}", dataSourceModel);
throw new MultiDatasourceException("Datasource config has empty value! Please check again.");
}
// 取设置的默认值
ds.setDriverClassName(Constant.DRIVER_CLASS_NAME);
ds.setUsername(dataSourceModel.getUsername());
//根据配置判断连接从库还是主库,得到最终的连接url
String url = makeDsUrl(dataSourceModel, properties);
log.info("build datasource ! url:{}", url);
ds.setUrl(url);
ds.setPassword(dataSourceModel.getPwd());
ds.setInitialSize(
dataSourceModel.getInitialSize() != null ? Integer.valueOf(dataSourceModel.getInitialSize()) : 5);
ds.setMaxActive(dataSourceModel.getMaxActive() != null ? Integer.valueOf(dataSourceModel.getMaxActive()) : 80);
ds.setMinIdle(dataSourceModel.getMinIdle() != null ? Integer.valueOf(dataSourceModel.getMinIdle()) : 10);
// 配置从连接池获取连接等待超时的时间
ds.setMaxWait(dataSourceModel.getMaxWait() != null ? Integer.valueOf(dataSourceModel.getMaxWait()) : 10000);
ds.setValidationQuery(
(dataSourceModel.getValidationQuery() != null && !"".equals(dataSourceModel.getValidationQuery()))
? dataSourceModel.getValidationQuery() : "SELECT 1");
ds.setValidationQueryTimeout(1000);
// 配置一个连接在池中最大空闲时间,单位是毫秒
ds.setMinEvictableIdleTimeMillis(60000);
// 设置检测空闲最大时长
// 配置间隔多久启动一次 DestroyThread,对连接池内的连接才进行一次检测,单位是毫秒。
// 检测时:
// 1.如果连接空闲并且超过minIdle以外的连接,如果空闲时间超过minEvictableIdleTimeMillis设置的值则直接物理关闭。
// 2.在minIdle以内的不处理。
ds.setTimeBetweenEvictionRunsMillis(60000);
// 空闲时间超过上值时长,测试该连接是否可用
// 设置从连接池获取连接时是否检查连接有效性,true 时,如果连接空闲时间超过 minEvictableIdleTimeMillis 进行检查,否则不检查; false时,不检查
ds.setTestWhileIdle(true);
// 长时间空闲连接回收
ds.setRemoveAbandoned(true);
// 设置超过多长时间未使用的连接强制关闭,秒
ds.setRemoveAbandonedTimeout(150);
// 关闭abanded连接时输出错误日志,这样出现连接泄露时可以通过错误日志定位忘记关闭连接的位置
ds.setLogAbandoned(true);
// 在取出连接时检查connection的可用性,可保证连接的绝对可用性,如不可用则换其他connection,但是较影响性能
ds.setTestOnBorrow(true);
// 设置往连接池归还连接时是否检查连接有效性,true 时,每次都检查;false 时,不检查
ds.setTestOnReturn(false);
// 打开后,增强timeBetweenEvictionRunsMillis的周期性连接检查,minIdle内的空闲连接,每次检查强制验证连接有效性.
// 参考:https://github.com/alibaba/druid/wiki/KeepAlive_cn
// ds.setKeepAlive(true);
return ds;
}
/**
* 主从切换
* @param dataSourceModel
* @param properties
* @return
*/
public static String makeDsUrl(DatasourceModel dataSourceModel, ZkProperties properties) {
String url;
if (!StringUtils.isBlank(properties.getReadOnly()) && "true".equalsIgnoreCase(properties.getReadOnly()))
url = dataSourceModel.getUrl().replace(Constant.WARITE,
Constant.READ + (StringUtils.isBlank(properties.getSlot()) ? "" : properties.getSlot()));
else
url = dataSourceModel.getUrl();
url = DbAdapter.makeJdbcUrl(dataSourceModel.getUrlPrefix() + url + dataSourceModel.getCommonDB());
return url;
}
static class Participant implements Runnable {
private DynamicDataSource dynamicDataSource;
private String dbName;
private String appName;
private CuratorClient curatorClient;
private CountDownLatch countDownLatch;
public Participant(CuratorClient curatorClient, DynamicDataSource dynamicDataSource, String dbName, String appName, CountDownLatch countDownLatch) {
this.dynamicDataSource = dynamicDataSource;
this.curatorClient = curatorClient;
this.dbName = dbName;
this.appName = appName;
this.countDownLatch = countDownLatch;
}
public void run() {
try {
curatorClient.registerRoutingKeyListenter(dynamicDataSource, dbName, appName);
} catch (Exception e) {
e.printStackTrace();
} finally {
countDownLatch.countDown();
}
}
}
/**
* 监听到的数据添加到routingkeys中
* @param dbUrl
* @param routingKeyList
* @param appName
* @throws Exception
*/
public void registerRoutingKey(String dbUrl, List<String> routingKeyList, String appName) throws Exception {
for (String routingKey : routingKeyList) {
String dbName = curatorClient.readWithPath(Constant.getAppDB(appName), dbUrl, routingKey);
if (StringUtils.isBlank(dbName))
return;
RoutingKey2DB routingKey2DB = new RoutingKey2DB(dbUrl, dbName);
synchronized (monitorDataSource) {
routingKeys.put(routingKey, routingKey2DB);
}
log.debug("Add routingKey [" + routingKey + "] DB [" + routingKey2DB + "]");
}
}
/**
* 从routingkeys中删除数据
* @param routingKey
*/
public void removeRoutingKey(String routingKey) {
if (!routingKeys.containsKey(routingKey))
return;
synchronized (monitorDataSource) {
Iterator<Map.Entry<String, RoutingKey2DB>> rs = routingKeys.entrySet().iterator();
while (rs.hasNext()) {
Map.Entry<String, RoutingKey2DB> next = rs.next();
if (routingKey.equalsIgnoreCase(next.getKey())) {
logger.info("Remove routing key : [" + next.getKey() + "]");
rs.remove();
}
}
}
}
/**
* 往dataSources中添加数据
* @param dataSourceModel
*/
public void registerDataSource(DatasourceModel dataSourceModel) {
synchronized (monitorDataSource) {
String key = dataSourceModel.getUrl();
if (dataSources.containsKey(key)) {
logger.info("dataSourceKey [" + key + "] have existed ");
removeDatasource(key);
}
DruidDataSource ds = buildDatasource(dataSourceModel, zkProperties);
dataSources.put(key, ds);
logger.info("put [" + key + "] datasource [" + ds + "]");
}
}
/**
* 从datasource中删除数据
* @param key
*/
public void removeDatasource(String key) {
if (!dataSources.containsKey(key))
return;
if (zkProperties.getAppName() == null && zkProperties.getAppName().isEmpty()) {
log.info("listen all appName, don't remove datasource!");
return;
}
synchronized (monitorDataSource) {
Iterator<Map.Entry<String, DruidDataSource>> ds = dataSources.entrySet().iterator();
while (ds.hasNext()) {
Map.Entry<String, DruidDataSource> next = ds.next();
if (key.equalsIgnoreCase(next.getKey())) {
logger.info("Remove datasource key : [" + next.getKey() + "]");
next.getValue().close();
ds.remove();
break;
}
}
}
}
}
@Data
class AppEntity {
public AppEntity(String appName, String dbName) {
this.appName = appName;
this.dbName = dbName;
}
private String appName;
private String dbName;
}
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);
}
}
#!/bin/sh
# ----------------------------------------------------------------------------
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
# ----------------------------------------------------------------------------
# ----------------------------------------------------------------------------
# Maven Start Up Batch script
#
# Required ENV vars:
# ------------------
# JAVA_HOME - location of a JDK home dir
#
# Optional ENV vars
# -----------------
# M2_HOME - location of maven2's installed home dir
# MAVEN_OPTS - parameters passed to the Java VM when running Maven
# e.g. to debug Maven itself, use
# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
# MAVEN_SKIP_RC - flag to disable loading of mavenrc files
# ----------------------------------------------------------------------------
if [ -z "$MAVEN_SKIP_RC" ] ; then
if [ -f /etc/mavenrc ] ; then
. /etc/mavenrc
fi
if [ -f "$HOME/.mavenrc" ] ; then
. "$HOME/.mavenrc"
fi
fi
# OS specific support. $var _must_ be set to either true or false.
cygwin=false;
darwin=false;
mingw=false
case "`uname`" in
CYGWIN*) cygwin=true ;;
MINGW*) mingw=true;;
Darwin*) darwin=true
# Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home
# See https://developer.apple.com/library/mac/qa/qa1170/_index.html
if [ -z "$JAVA_HOME" ]; then
if [ -x "/usr/libexec/java_home" ]; then
export JAVA_HOME="`/usr/libexec/java_home`"
else
export JAVA_HOME="/Library/Java/Home"
fi
fi
;;
esac
if [ -z "$JAVA_HOME" ] ; then
if [ -r /etc/gentoo-release ] ; then
JAVA_HOME=`java-config --jre-home`
fi
fi
if [ -z "$M2_HOME" ] ; then
## resolve links - $0 may be a link to maven's home
PRG="$0"
# need this for relative symlinks
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG="`dirname "$PRG"`/$link"
fi
done
saveddir=`pwd`
M2_HOME=`dirname "$PRG"`/..
# make it fully qualified
M2_HOME=`cd "$M2_HOME" && pwd`
cd "$saveddir"
# echo Using m2 at $M2_HOME
fi
# For Cygwin, ensure paths are in UNIX format before anything is touched
if $cygwin ; then
[ -n "$M2_HOME" ] &&
M2_HOME=`cygpath --unix "$M2_HOME"`
[ -n "$JAVA_HOME" ] &&
JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
[ -n "$CLASSPATH" ] &&
CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
fi
# For Mingw, ensure paths are in UNIX format before anything is touched
if $mingw ; then
[ -n "$M2_HOME" ] &&
M2_HOME="`(cd "$M2_HOME"; pwd)`"
[ -n "$JAVA_HOME" ] &&
JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`"
fi
if [ -z "$JAVA_HOME" ]; then
javaExecutable="`which javac`"
if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then
# readlink(1) is not available as standard on Solaris 10.
readLink=`which readlink`
if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then
if $darwin ; then
javaHome="`dirname \"$javaExecutable\"`"
javaExecutable="`cd \"$javaHome\" && pwd -P`/javac"
else
javaExecutable="`readlink -f \"$javaExecutable\"`"
fi
javaHome="`dirname \"$javaExecutable\"`"
javaHome=`expr "$javaHome" : '\(.*\)/bin'`
JAVA_HOME="$javaHome"
export JAVA_HOME
fi
fi
fi
if [ -z "$JAVACMD" ] ; then
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD="$JAVA_HOME/jre/sh/java"
else
JAVACMD="$JAVA_HOME/bin/java"
fi
else
JAVACMD="`which java`"
fi
fi
if [ ! -x "$JAVACMD" ] ; then
echo "Error: JAVA_HOME is not defined correctly." >&2
echo " We cannot execute $JAVACMD" >&2
exit 1
fi
if [ -z "$JAVA_HOME" ] ; then
echo "Warning: JAVA_HOME environment variable is not set."
fi
CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher
# traverses directory structure from process work directory to filesystem root
# first directory with .mvn subdirectory is considered project base directory
find_maven_basedir() {
if [ -z "$1" ]
then
echo "Path not specified to find_maven_basedir"
return 1
fi
basedir="$1"
wdir="$1"
while [ "$wdir" != '/' ] ; do
if [ -d "$wdir"/.mvn ] ; then
basedir=$wdir
break
fi
# workaround for JBEAP-8937 (on Solaris 10/Sparc)
if [ -d "${wdir}" ]; then
wdir=`cd "$wdir/.."; pwd`
fi
# end of workaround
done
echo "${basedir}"
}
# concatenates all lines of a file
concat_lines() {
if [ -f "$1" ]; then
echo "$(tr -s '\n' ' ' < "$1")"
fi
}
BASE_DIR=`find_maven_basedir "$(pwd)"`
if [ -z "$BASE_DIR" ]; then
exit 1;
fi
##########################################################################################
# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central
# This allows using the maven wrapper in projects that prohibit checking in binary data.
##########################################################################################
if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then
if [ "$MVNW_VERBOSE" = true ]; then
echo "Found .mvn/wrapper/maven-wrapper.jar"
fi
else
if [ "$MVNW_VERBOSE" = true ]; then
echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..."
fi
if [ -n "$MVNW_REPOURL" ]; then
jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
else
jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
fi
while IFS="=" read key value; do
case "$key" in (wrapperUrl) jarUrl="$value"; break ;;
esac
done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties"
if [ "$MVNW_VERBOSE" = true ]; then
echo "Downloading from: $jarUrl"
fi
wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar"
if $cygwin; then
wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"`
fi
if command -v wget > /dev/null; then
if [ "$MVNW_VERBOSE" = true ]; then
echo "Found wget ... using wget"
fi
if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then
wget "$jarUrl" -O "$wrapperJarPath"
else
wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath"
fi
elif command -v curl > /dev/null; then
if [ "$MVNW_VERBOSE" = true ]; then
echo "Found curl ... using curl"
fi
if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then
curl -o "$wrapperJarPath" "$jarUrl" -f
else
curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f
fi
else
if [ "$MVNW_VERBOSE" = true ]; then
echo "Falling back to using Java to download"
fi
javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java"
# For Cygwin, switch paths to Windows format before running javac
if $cygwin; then
javaClass=`cygpath --path --windows "$javaClass"`
fi
if [ -e "$javaClass" ]; then
if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then
if [ "$MVNW_VERBOSE" = true ]; then
echo " - Compiling MavenWrapperDownloader.java ..."
fi
# Compiling the Java class
("$JAVA_HOME/bin/javac" "$javaClass")
fi
if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then
# Running the downloader
if [ "$MVNW_VERBOSE" = true ]; then
echo " - Running MavenWrapperDownloader.java ..."
fi
("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR")
fi
fi
fi
fi
##########################################################################################
# End of extension
##########################################################################################
export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"}
if [ "$MVNW_VERBOSE" = true ]; then
echo $MAVEN_PROJECTBASEDIR
fi
MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS"
# For Cygwin, switch paths to Windows format before running java
if $cygwin; then
[ -n "$M2_HOME" ] &&
M2_HOME=`cygpath --path --windows "$M2_HOME"`
[ -n "$JAVA_HOME" ] &&
JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
[ -n "$CLASSPATH" ] &&
CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
[ -n "$MAVEN_PROJECTBASEDIR" ] &&
MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"`
fi
# Provide a "standardized" way to retrieve the CLI args that will
# work with both Windows and non-Windows executions.
MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@"
export MAVEN_CMD_LINE_ARGS
WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
exec "$JAVACMD" \
$MAVEN_OPTS \
-classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \
"-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@"
@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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论