简体   繁体   English

启动Spring Boot应用程序时不考虑数据库端口

[英]DB Port not considered while booting up Spring boot application

I have been given a Spring boot application to dockerize. 我得到了一个Spring Boot应用程序以进行dockerize。

Overview 总览

  • Application is a Springboot application. 应用程序是一个Springboot应用程序。
  • Built using Gradle. 使用Gradle构建。
  • Application Properties using application.yml. 使用application.yml的应用程序属性。
  • Database : DBServer:1435 ( Port is not default 1433). 数据库:DBServer:1435(端口不是默认的1433)。

Initially I created the docker image and got the application up. 最初,我创建了docker映像并启动了应用程序。
Later the DB port was changed to 1435. Now I have updated the JDBC url in datasource section of profiles in application.yml located in src\\main\\resources. 后来,数据库端口更改为1435。现在,我已更新位于src \\ main \\ resources中的application.yml中的配置文件的datasource部分中的JDBC url。 DB_IP and DB_NAME are passed when booting up docker image. 启动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

Now application won't boot and instead defaulting to port 1433. Error is as below. 现在应用程序将无法启动,而是默认为端口1433。错误如下。 I am not sure why it is defaulting to 1433. Any idea? 我不确定为什么默认为1433。有什么想法吗?

I tried connecting to DB instance from the docker machine and it works fine. 我尝试从docker机器连接到数据库实例,并且工作正常。
Further I checked using Telnet and port 1435 can be connected. 进一步我检查使用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.".

This is a common problem that trips up many Java apps trying to connect to Microsoft SQL Server. 这是一个常见问题,许多Java应用程序尝试连接到Microsoft SQL Server时都会跳闸。 Following steps will solve your issue. 请按照以下步骤解决您的问题。

  1. Open SQL Server 2014 Configuration Manager . 打开SQL Server 2014 Configuration Manager
  2. Click Protocol for SQLEXPRESS under SQL Server Network Configuration on the left pane. 在左窗格的“ SQL Server网络配置 下,单击 SQLEXPRESS 协议” On the right pane, right- click TCP/IP , and select Properties . 在右窗格中,右键单击“ TCP / IP” ,然后选择“ 属性”
  3. On the TCP/IP Properties dialog box that appears, click the IP Addresses tab. 在出现的“ TCP / IP属性”对话框中,单击“ IP地址”选项卡。
  4. Scroll down to locate the IPALL node. 向下滚动以找到IPALL节点。 Remove any value, if present for TCP Dynamic Ports and specify 1433 for TCP Port . 删除任何值(如果存在于TCP动态端口),并为TCP Port指定1433
  5. Click OK . 单击确定
  6. Again right-click TCP/IP on the right pane, and select Enable . 再次右键单击右窗格中的TCP / IP ,然后选择启用
  7. On the SQL Server Services node, right-click SQL Server (SQLEXPRESS) , and select Restart . 在“ SQL Server服务”节点上,右键单击“ SQL Server(SQLEXPRESS)” ,然后选择“ 重新启动”

在此处输入图片说明

Your connection string in application.properties must look like following: 您的application.properties中的连接字符串必须如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM