此章節具體介紹一下淘寶TDDL具體配置和使用java
1. Spring配置文件配置:
================spring-mybatis.xml 中配置============= <bean id="dataSource" class="com.taobao.tddl.group.jdbc.TGroupDataSource" init-method="init" destroy-method="destroyDataSource"> <!-
appName gome_market_search_index :應用名稱 名字隨意起 有意義便可
dbGroupKey gome_market_search_index_group_0 :db組 名字隨意起有意義便可 主要做用: 能夠配置多個數據源 做爲一組
->
<property name="appName" value="gome_market_search_index" /> <property name="dbGroupKey" value="gome_market_search_index_group_0" /> </bean>
2.1 用一張圖瞭解TDDL配置
2.2 TDDL須要結合diamond使用,如下是diamond須要的相關配置,必須配置到默認組下 DEFAULT_GROUP
2.3===========配置數據源(ip端口等)===========
##com.taobao.tddl.atom.global.數據源名稱 (gome_market_search_index_db_0) com.taobao.tddl.atom.global.gome_market_search_index_db_0
##數據庫ip地址 ip=10.144.43.141
#數據庫端口 port=3306
##數據庫名稱 dbName=gome_market_prod
##數據庫類型 dbType=mysql
#數據庫狀態 dbStatus=RW 2.4===========配置數據源(鏈接池)==============
##com.taobao.tddl.atom.app.應用名稱.數據源名 com.taobao.tddl.atom.app.gome_market_search_index.gome_market_search_index_db_0
#數據庫用戶 userName=root
#最小鏈接數 minPoolSize=50
#最大鏈接數 maxPoolSize=100
#鏈接的最大空閒時間 idleTimeout=10
#等待鏈接的最大時間 blockingTimeout=5000
#預處理緩存大小 preparedStatementCacheSize=50
#數據庫鏈接屬性 connectionProperties=autoReconnect=true&useUnicode=true&characterEncoding=UTF-8 2.5===========配置數據庫(com.taobao.tddl.atom.passwd.數據庫名.mysql.用戶名)========== com.taobao.tddl.atom.passwd.gome_market_prod.mysql.root ##數據庫密碼 root encPasswd=-64d29910cc13d220ea2e89c490b1e4bf encKey=f97xK9qh3BSXv5iy 2.6============配置dbGroup===================== com.taobao.tddl.jdbc.group_V2.4.1_gome_market_search_index_group_0 #w0r1 0個讀庫鏈接 1個讀庫鏈接 gome_market_search_index_db_0:w0r1
上記2.5章節encPasswd 須要加密操做,根據encKey加密mysql
SecureIdentityLoginModule.java 將代碼拷貝到工程運行spring
package service; import java.math.BigInteger; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.lang.StringUtils; public class SecureIdentityLoginModule { private static byte[] ENC_KEY_BYTES = "This is a finger".getBytes(); private String userName; private String password; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getDecodedPassword() throws Exception { return new String(decode(password)); } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((password == null) ? 0 : password.hashCode()); result = prime * result + ((userName == null) ? 0 : userName.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; SecureIdentityLoginModule other = (SecureIdentityLoginModule) obj; if (password == null) { if (other.password != null) return false; } else if (!password.equals(other.password)) return false; if (userName == null) { if (other.userName != null) return false; } else if (!userName.equals(other.userName)) return false; return true; } public static String encode(String encKey, String secret) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { byte[] kbytes = SecureIdentityLoginModule.ENC_KEY_BYTES; if (StringUtils.isNotBlank(encKey)) { kbytes = encKey.getBytes(); } SecretKeySpec key = new SecretKeySpec(kbytes, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] encoding = cipher.doFinal(secret.getBytes()); BigInteger n = new BigInteger(encoding); return n.toString(16); } public static String encode(String secret) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { return SecureIdentityLoginModule.encode(null, secret); } public static String decode(String encKey, String secret) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { byte[] kbytes = SecureIdentityLoginModule.ENC_KEY_BYTES; if (StringUtils.isNotBlank(encKey)) { kbytes = encKey.getBytes(); } SecretKeySpec key = new SecretKeySpec(kbytes, "AES"); BigInteger n = new BigInteger(secret, 16); byte[] encoding = n.toByteArray(); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, key); byte[] decode = cipher.doFinal(encoding); return new String(decode); } public static char[] decode(String secret) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { return SecureIdentityLoginModule.decode(null, secret).toCharArray(); } public static void main(String[] args) throws Exception { System.out.println("Encoded password: " + new String(SecureIdentityLoginModule.encode("f97xK9qh3BSXv5iy","root"))); System.out.println("decoded password: " + new String(SecureIdentityLoginModule.decode("5a826c8121945c969bf9844437e00e28"))); } }
若是沒有配置encKey 須要在diamond配置 一個dms組 dataid:decryptPasswordUrl 須要配置密碼驗證策略 建議不使用sql
3.工程pom文件中引入依賴 注意:此依賴不做爲公共依賴數據庫
<dependency> <groupId>com.taobao.tddl</groupId> <artifactId>tddl-matrix</artifactId> <version>5.1.7.g1-SNAPSHOT</version> <exclusions> <exclusion> <artifactId>tddl-repo-demo</artifactId> <groupId>com.taobao.tddl</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.taobao.tddl</groupId> <artifactId>tddl-config-diamond</artifactId> <version>5.1.7.g1-SNAPSHOT</version> </dependency>
4.使用中遇到的問題解決 maven工程排除此版本的依賴便可apache