@@ -6,11 +6,17 @@ import * as generateDbParams from "./generate-db-params";
66import logger from "../../common/logger" ;
77import { OTS_INSTANCE_NAME } from "../../constants" ;
88
9+ enum OtsAccess {
10+ OTS = 'ots' ,
11+ TABLE_STORE = 'tablestore' ,
12+ }
13+
914export default class Ots {
1015 client : Client ;
1116 popClient : any ;
1217 dbConfig : any ;
1318 envConfig : any ;
19+ endpoint : string ;
1420
1521 constructor ( envConfig : IProps ) {
1622 this . envConfig = envConfig ;
@@ -20,15 +26,7 @@ export default class Ots {
2026 endpoint : `https://ots.${ envConfig . REGION } .aliyuncs.com` ,
2127 apiVersion : "2016-06-20" ,
2228 } ) ;
23-
24- this . client = new Client ( {
25- accessKeyId : envConfig . ACCESS_KEY_ID ,
26- accessKeySecret : envConfig . ACCESS_KEY_SECRET ,
27- // securityToken: credentials.SecurityToken,
28- endpoint : `https://${ envConfig . OTS_INSTANCE_NAME } .${ envConfig . REGION } .ots.aliyuncs.com` ,
29- instancename : envConfig . OTS_INSTANCE_NAME ,
30- maxRetries : 20 , // 默认20次重试,可以省略此参数。
31- } ) ;
29+ this . makeClient ( ) ;
3230
3331 this . dbConfig = [
3432 {
@@ -62,6 +60,20 @@ export default class Ots {
6260 ] ;
6361 }
6462
63+ makeClient ( access : OtsAccess = OtsAccess . OTS ) {
64+ const envConfig = this . envConfig ;
65+ this . endpoint = `https://${ envConfig . OTS_INSTANCE_NAME } .${ envConfig . REGION } .${ access } .aliyuncs.com` ;
66+
67+ this . client = new Client ( {
68+ accessKeyId : envConfig . ACCESS_KEY_ID ,
69+ accessKeySecret : envConfig . ACCESS_KEY_SECRET ,
70+ // securityToken: credentials.SecurityToken,
71+ endpoint : this . endpoint ,
72+ instancename : envConfig . OTS_INSTANCE_NAME ,
73+ maxRetries : 20 , // 默认20次重试,可以省略此参数。
74+ } ) ;
75+ }
76+
6577 // 初始化实例
6678 async initInstance ( ) {
6779 const popClient = this . popClient ;
@@ -103,7 +115,17 @@ export default class Ots {
103115 for ( const { name, indexName, genTableParams, genIndexParams } of this
104116 . dbConfig ) {
105117 logger . debug ( `handler ${ name } start` ) ;
106- await this . handlerTable ( name , genTableParams ( name , this . envConfig ) ) ;
118+ try {
119+ await this . handlerTable ( name , genTableParams ( name , this . envConfig ) ) ;
120+ } catch ( ex ) {
121+ logger . debug ( `handler table error: ${ ex . message } ` ) ;
122+ if ( ex ?. code === 'NetworkingError' && ex ?. message . startsWith ( 'getaddrinfo ENOTFOUND' ) ) {
123+ this . makeClient ( OtsAccess . TABLE_STORE ) ;
124+ await this . handlerTable ( name , genTableParams ( name , this . envConfig ) ) ;
125+ } else {
126+ throw ex ;
127+ }
128+ }
107129
108130 if ( indexName ) {
109131 await this . handlerIndex (
0 commit comments