淘宝TDDL配置以及使用

此章节具体介绍一下淘宝TDDL具体配置和使用

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加密

SecureIdentityLoginModule.java  将代码拷贝到工程运行

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   需要配置密码验证策略  建议不使用

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工程排除此版本的依赖即可

原文地址:https://www.cnblogs.com/shoutn/p/8664519.html