Phoenix与SpringBoot冲突问题
项目服务器框架基于SpringBoot。我们希望在服务器用JDBC连接ZDH集群的phoenix时,遇上冲突:
Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.slf4j.impl.Log4jLoggerFactory loaded from file:/C:/Users/Lenovo/.m2/repository/com/abchina/zdh/r7b1/phoenix/phoenix/4.14.1-zdh8.7.1-client/phoenix-4.14.1-zdh8.7.1-client.jar). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml: org.slf4j.impl.Log4jLoggerFactory
at org.springframework.util.Assert.instanceCheckFailed(Assert.java:637)
at org.springframework.util.Assert.isInstanceOf(Assert.java:537)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.getLoggerContext(LogbackLoggingSystem.java:286)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.beforeInitialize(LogbackLoggingSystem.java:102)
at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationStartingEvent(LoggingApplicationListener.java:191)
at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:170)
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127)
at org.springframework.boot.context.event.EventPublishingRunListener.starting(EventPublishingRunListener.java:68)
at org.springframework.boot.SpringApplicationRunListeners.starting(SpringApplicationRunListeners.java:48)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:293)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1242)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1230)
at com.abchina.isp.IsprestApplication.main(IsprestApplication.java:19)
... 5 more
经查询,是phoenix-client内置的log4j与springboot的日志系统冲突,并且由于springboot的日志是与我们的监控系统配套的,我们不方便替换。而phoenix的日志依赖打在jar包内,exclusion不能解决冲突。尝试了网上的许多解决方案,都不能奏效。
于是,我查到用phoenix-queryserver-client服务,不容易产生冲突。phoenix-queryserver-client服务较轻,但只有查询功能,如果springboot只用于查询不用于入库,可以使用该服务。具体使用如下:
引入maven依赖
<dependency>
<groupId>org.apache.phoenix</groupId>
<artifactId>phoenix-queryserver-client</artifactId>
<version>4.14.3-HBase-1.4</version>
</dependency>
JDBC连接工具类
import org.springframework.stereotype.Componet;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@Componet
public class PhoenixOperator {
public Connection getConnection(){
try {
Class.forName("org.apache.phoenix.queryserver.client.Driver");
return DriverManager.getConnection("jdbc:phoenix:thin:url=http://ip:8765;serialization=PROTOBUF");
}catch (Exception e){
e.printStackTrace();
return null;
}
}
public ResultSet query(String sql){
Connection connection = null;
try {
connection = getConnection();
if (connection == null){
return null;
}
PreparedStatement ps = connection.preparedStatement(sql);
ResultSet rs = ps.executeQuery();
return rs;
}catch (Exception e){
e.printStackTrace();
}finally {
if (connection != null){
try {
connection.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
return null;
}
}
问题解决!