我们希望通过Tomcat 8.5将我们的应用程序(.war Files(部署到一个k8s基础设施中,该基础设施具有依赖于Google Cloud SQL(MySQL(的数据库。
Google提供MySQL连接器库(请参阅https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory/blob/main/docs/jdbc-mysql.md&https://cloud.google.com/sql/docs/mysql/connect-connectors)遗憾的是,只有关于java应用程序本身实现的文档。出于公司战略原因,我们不想那样做。
我们已经尝试使用一个包含mysql连接器所有依赖项的fat-jar,并将其(与连接器J/8.jar一起(放入tomcat/libs/目录中,并在server.xml中配置资源,如下所示:
<Resource name="jdbc/cloudSQL" auth="Container" type="javax.sql.DataSource" maxTotal="100" maxIdle="30" maxWaitMillis="10000"
factory="com.google.cloud.sql.mysql.SocketFactory" driverClassName="com.mysql.cj.jdbc.Driver" removeAbandonedOnBorrow="true" removeAbandonedTimeout="60" logAbandoned="true"
username="${db.user}" password="${db.pass}" url="jdbc:mysql:///${db.name}?cloudSqlInstance=${db.instance}&autoReconnect=true"
testOnBorrow="true" validationQuery="SELECT 1" />
工作不太差,但也不成功:
javax.naming.NamingException: Could not create resource factory instance
[Root exception is java.lang.ClassCastException: com.google.cloud.sql.mysql.SocketFactory cannot be cast to javax.naming.spi.ObjectFactory]
at org.apache.naming.factory.FactoryBase.getObjectInstance(FactoryBase.java:86)
at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:332)
at org.apache.naming.NamingContext.lookup(NamingContext.java:846)
at org.apache.naming.NamingContext.lookup(NamingContext.java:157)
at org.apache.naming.NamingContextBindingsEnumeration.nextElementInternal(NamingContextBindingsEnumeration.java:115)
at org.apache.naming.NamingContextBindingsEnumeration.next(NamingContextBindingsEnumeration.java:69)
at org.apache.naming.NamingContextBindingsEnumeration.next(NamingContextBindingsEnumeration.java:32)
at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.createMBeans(GlobalResourcesLifecycleListener.java:138)
at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.createMBeans(GlobalResourcesLifecycleListener.java:145)
at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.createMBeans(GlobalResourcesLifecycleListener.java:112)
at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.lifecycleEvent(GlobalResourcesLifecycleListener.java:87)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:123)
at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:423)
at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:366)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:787)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.startup.Catalina.start(Catalina.java:695)
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:498)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:345)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:476)
Caused by: java.lang.ClassCastException: com.google.cloud.sql.mysql.SocketFactory cannot be cast to javax.naming.spi.ObjectFactory
at org.apache.naming.factory.FactoryBase.getObjectInstance(FactoryBase.java:75) ... 22 more
如果你知道如何解决这个问题,我将不胜感激。
我在Google Cloud上复制了这个问题,然后我可以按照Tomcat的说明使用Connector/J连接到Cloud SQL,而不是使用Google Cloud中的库(https://tomcat.apache.org/tomcat-8.5-doc/jndi-datasource-examples-howto.html)