首先配置spring中配置c3p0
================================================
import java.beans.PropertyVetoException;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import com.mchange.v2.c3p0.ComboPooledDataSource;
@Configuration
public class ConnPoolConfig {
@Value("${DriverClass}")
private String driverClass;
@Value("${maxIdleTime}")
private Integer maxIdleTime;
@Value("${JdbcUrl}")
private String jdbcUrl;
@Value("${AcquireIncrement}")
private Integer acquireIncrement;
@Value("${MinPoolSize}")
private Integer minPoolSize;
@Value("${MaxPoolSize}")
private Integer maxPoolSize;
@Bean(name = "baseDataSource")
@Primary
public DataSource dataSource() {
// @Primary 注解作用是当程序选择dataSource时选择被注解的这个
ComboPooledDataSource cpds = new ComboPooledDataSource();
try {
cpds.setDriverClass(driverClass);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
cpds.setJdbcUrl(jdbcUrl);
cpds.setMaxIdleTime(maxIdleTime);
cpds.setMinPoolSize(minPoolSize);
cpds.setAcquireIncrement(acquireIncrement);
cpds.setMaxPoolSize(minPoolSize);
return cpds;
}
}
==============================================
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Optional;
import java.util.function.Function;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class ImpalaUtil {
@Autowired
DataSource baseDataSource;
/**
* 执行语句有返回值
* @param sql
* @param handler
* @param
* @return
*/
public
ResultSet resultSet = null;
Connection connection = null;
Statement statement = null;
try {
connection = baseDataSource.getConnection();
statement = connection.createStatement();
resultSet = statement.executeQuery(sql);
return Optional.of(handler.apply(resultSet));
} catch (SQLException e) {
releaseResource(connection,statement,resultSet);
e.printStackTrace();
Object log;
//log.error("执行语句时出错了e={}",e);
}finally {
releaseResource(connection,statement,resultSet);
}
return Optional.empty();
}
/**
* 关闭连接,释放资源
* @param conn
* @param statement
* @param rs
*/
public static void releaseResource(Connection conn, Statement statement, ResultSet rs) {
try {
Object log;
if (rs != null) {
try {
rs.close();
} catch (Throwable e) {
e.printStackTrace();
//log.error("出错了e={},关闭rs失败",e);
}
}
if (statement != null) {
try {
statement.close();
} catch (Throwable e) {
e.printStackTrace();
//log.error("出错了e={},关闭statement失败",e);
}
}
if (conn != null) {
try {
conn.close();
} catch (Throwable e) {
e.printStackTrace();
//log.error("出错了e={},关闭conn失败",e);
}
}
} catch (Throwable e) {
e.printStackTrace();
}
}
}
===========================================
方法调用
con = baseDataSource.getConnection();
String crjson = null;
ResultSet rs = null;
String sql = " select * from table";
try {
rs = JDBCutil.executeQuery(con, sql);
while (rs.next()) {
crjson = rs.getString(1);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
ImpalaUtil.releaseResource(con,null,rs);
}
========================
nginx 配置
由于我的服务器不能连接外网,所以先配置代理服务
配置网络代理服务
export http_proxy=10.1.48.62:8118;export https_proxy=10.1.48.62:8118;export proxy=10.1.48.62:8118
先安装nginx的yum源
http://nginx.org/en/linux_packages.html#stable 找到链接,安装:
rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
查看:
yum info nginx
安装:
yum install nginx
安装完成后nginx就已经启动了,可以查看进程:ps -ef |grep nginx
启动nginx:
service nginx start
service nginx status
service nginx stop
查看nginx路径: whereis nginx
[root@hadoop11 ~]# whereis nginx
nginx: /usr/sbin/nginx /etc/nginx /usr/lib64/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz
cd /etc/nginx
vi /etc/nginx/nginx.conf
# ***在文件末尾增加如下配置***
stream{
#impala-jdbc 负载均衡算法声明与服务器配置
upstream impala-jdbc {
#路由策略:当在upstream配置块中没有指定使用的负载均衡算法时,默认使用的是加权轮询。
#down 表示单前的server暂时不参与负载
server 10.1.32.13:21050 down;
#weight 默认为1.weight越大,负载的权重就越大。
#Nginx每收到6个客户端的请求,会把其中的3个转发给bigdata-poc-shtz-3,把其中的2个转发给bigdata-poc-shtz-4:21050,把其中的1个转发给bigdata-poc-shtz-5。
server hadoop4:21050 weight=2;
server hadoop5:21050 weight=2;
server hadoop8:21050 weight=2;
server hadoop9:21050 weight=2;
server hadoop10:21050 weight=2;
}
#impala-jdbc 负载均衡
server{
listen 25004;
proxy_pass impala-jdbc;
}
#前端调用接口
#factory_interface 负载均衡算法声明与服务器配置
upstream factory_interface {
#路由策略:当在upstream配置块中没有指定使用的负载均衡算法时,默认使用的是加权轮询。
#weight 默认为1.weight越大,负载的权重就越大。
server hadoop10:80 weight=5;
server hadoop9:80 weight=5;
}
#factory_interface 负载均衡
server{
listen 8080;
proxy_pass factory_interface;
}
}