启动Spring启动应用程序时不考虑DB端口

时间:2018-02-07 06:11:43

标签: java spring-boot jdbc

我已经获得了一个用于dockerize的Spring启动应用程序。

概述

  • 应用程序是一个Springboot应用程序。
  • 使用Gradle构建。
  • 使用application.yml的应用程序属性。
  • 数据库:DBServer:1435(端口不是默认值1433)。

最初我创建了泊坞窗图像并启动了应用程序 之后,DB端口更改为1435.现在,我已更新位于src \ main \ resources中的application.yml中的配置文件的数据源部分中的JDBC URL。 启动docker镜像时会传递DB_IP和DB_NAME。

spring:
  profiles: dev,staging,prod
  datasource:
    url: "jdbc:sqlserver://${DB_IP}:1435;databaseName=${DB_NAME}"
    username: "${DB_USER}"
    password: "${DB_PASS}"
    initialize: false
  jpa:
    show-sql: true
    generate-ddl: false
    hibernate:
      ddl-auto: none

现在应用程序无法启动而是默认为端口1433.错误如下所示。我不知道为什么它会违约到1433.任何想法?

我尝试从docker机器连接数据库实例,但工作正常  此外,我使用Telnet进行了检查,并且可以连接端口1435.

2018-02-07 16:54:18,118 ERROR main  org.apache.tomcat.jdbc.pool.ConnectionPool - Unable to create initial connections of pool.
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host 10.17.193.240, port 1433 has failed. Error: "Connection refused: no further information. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:206)
    at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:257)
    at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2385)
    at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:567)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1955)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1616)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1447)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:788)
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1187)
    at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:310)
    at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:203)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:735)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:667)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:482)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:154)
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:118)
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:107)
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:131)
    at org.flywaydb.core.internal.util.jdbc.JdbcUtils.openConnection(JdbcUtils.java:51)
    at org.flywaydb.core.Flyway.execute(Flyway.java:1418)
    at org.flywaydb.core.Flyway.migrate(Flyway.java:971)
    at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:66)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:296)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1078)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
    at au.com.outware.swepad.Application.main(Application.java:49)
    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.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
2018-02-07 16:54:18,129 WARN main  org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.internal.dbsupport.FlywaySqlException: 
Unable to obtain Jdbc connection from DataSource
------------------------------------------------
SQL State  : 08S01
Error Code : 0
Message    : The TCP/IP connection to the host 10.17.193.240, port 1433 has failed. Error: "Connection refused: no further information. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".

1 个答案:

答案 0 :(得分:0)

这是一个常见问题,导致许多试图连接到Microsoft SQL Server的Java应用程序出现问题。以下步骤将解决您的问题。

  1. 打开 SQL Server 2014 Configuration Manager
  2. 点击左侧窗格中 SQL Server网络配置下的 SQLEXPRESS 协议。在右侧窗格中,右键单击 TCP / IP ,然后选择属性
  3. 在显示的 TCP / IP 属性对话框中,点击 IP地址标签。
  4. 向下滚动以找到 IPALL 节点。删除 TCP动态端口时的任何值,并为 TCP端口指定 1433
  5. 点击确定
  6. 再次右键单击右侧窗格中的 TCP / IP ,然后选择启用
  7. SQL Server服务节点上,右键单击 SQL Server(SQLEXPRESS),然后选择重新启动
  8. enter image description here

    application.properties中的连接字符串必须如下所示:

    spring.datasource.url=jdbc:sqlserver://localhost;databaseName=YourDBName