我们的业务晚上就没了导致与mysql断开。taskmanager需要重启。虽然可以写定时任务重启。但是这样写总是比较low。
所以重写了 flink-connector-jdbc
/*
* 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
*
* http://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.
*/
package org.apache.flink.connector.jdbc.internal.connection;
import org.apache.flink.connector.jdbc.JdbcConnectionOptions;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.concurrent.NotThreadSafe;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
/** Simple JDBC connection provider. */
@NotThreadSafe
public class SimpleJdbcConnectionProvider implements JdbcConnectionProvider, Serializable {
private static final Logger LOG = LoggerFactory.getLogger(SimpleJdbcConnectionProvider.class);
private static final long serialVersionUID = 1L;
private final JdbcConnectionOptions jdbcOptions;
private transient Driver loadedDriver;
private transient DruidDataSource dataSource;
private transient DruidPooledConnection connection;
static {
// Load DriverManager first to avoid deadlock between DriverManager's
// static initialization block and specific driver class's static
// initialization block when two different driver classes are loading
// concurrently using Class.forName while DriverManager is uninitialized
// before.
//
// This could happen in JDK 8 but not above as driver loading has been
// moved out of DriverManager's static initialization block since JDK 9.
DriverManager.getDrivers();
}
public SimpleJdbcConnectionProvider(JdbcConnectionOptions jdbcOptions) {
this.jdbcOptions = jdbcOptions;
}
@Override
public Connection getConnection() {
try {
return dataSource.getConnection();
} catch (SQLException e) {
return null;
}
}
@Override
public boolean isConnectionValid() throws SQLException {
return connection != null
&& !connection.isClosed()
&& !connection.isAbandonded()
&& connection.isValid(5);
}
@Override
public Connection getOrEstablishConnection() throws SQLException, ClassNotFoundException {
if (dataSource == null) {
dataSource =
Druid.dataSource(
jdbcOptions.getDbURL(),
jdbcOptions.getUsername().orElse(null),
jdbcOptions.getPassword().orElse(null));
}
// 超过3秒就不要了
connection = dataSource.getConnection(3000);
return connection;
}
@Override
public void closeConnection() {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
connection = null;
}
@Override
public Connection reestablishConnection() throws SQLException, ClassNotFoundException {
closeConnection();
return getOrEstablishConnection();
}
}
package org.apache.flink.connector.jdbc.internal.connection;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.util.JdbcUtils;
import java.sql.SQLException;
public class Druid {
private static int initialSize = 1;
private static int minIdle = 2;
private static int maxActive = 3;
private static int maxWait = 10000;
private static int timeBetweenEvictionRunsMillis = 60000;
private static int minEvictableIdleTimeMillis = 1800000;
private static String validationQuery = "select 1";
private static boolean testWhileIdle = true;
private static boolean testOnBorrow = false;
private static boolean testOnReturn = false;
private static boolean poolPreparedStatements = true;
private static int maxPoolPreparedStatementPerConnectionSize = 20;
public static DruidDataSource dataSource(String url, String user, String password)
throws SQLException {
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(url);
datasource.setUsername(user);
datasource.setPassword(password);
// 设置超时就回收
datasource.setRemoveAbandoned(true);
datasource.setRemoveAbandonedTimeout(25);
datasource.setLogAbandoned(true);
datasource.setDriverClassName(JdbcUtils.getDriverClassName(url));
datasource.setDbType(JdbcUtils.getDbType(url, null));
datasource.setValidationQueryTimeout(1);
// pool configuration
datasource.setInitialSize(initialSize);
datasource.setMinIdle(minIdle);
datasource.setMaxActive(maxActive);
datasource.setPassword(password);
datasource.setMaxWait(maxWait);
datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
datasource.setMaxEvictableIdleTimeMillis(1800000); // 半个小时
datasource.setValidationQuery(validationQuery);
datasource.setTestWhileIdle(testWhileIdle);
datasource.setTestOnBorrow(testOnBorrow);
datasource.setTestOnReturn(testOnReturn);
datasource.setValidationQueryTimeout(3);
// datasource.setConnectionProperties("hiveconf:hive.support.quoted.identifiers=none");
datasource.setPoolPreparedStatements(poolPreparedStatements);
datasource.setMaxPoolPreparedStatementPerConnectionSize(
maxPoolPreparedStatementPerConnectionSize);
return datasource;
}
}
目前运行良好
pom
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
运行的时候别忘了把druid.jar放上去