我有一个连接到 SQL Server 数据库的 Java Azure 函数,它曾经与“Azure SQL 服务”配合良好。数据库现在正在迁移到 Azure SQL“托管实例”,因此我更改了下面的 DB_SERVER 等连接参数,现在当我部署到 Azure 时,我不断收到以下错误。
- 在我的笔记本电脑上也可以正常使用
- 进行了 VNet 集成
- 同一 DB_SERVER 和端口 1433 的 tcpping 正在运行
- 使用连接字符串在.net 中编写的另一个函数在相同的网络配置中运行。
- 部署 REST 触发的 Spring Boot 应用服务而不是 Azure 函数也是可行的。
有人可以帮忙吗?
示例代码:
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
dataSource.setUrl(String.format("jdbc:sqlserver://%s;databaseName=%s", DB_SERVER, DB_NAME));
dataSource.setUsername(DB_USER_NAME);
dataSource.setPassword(DB_PASSWORD);
jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.query(...);
pom.xml内容:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>7.0.0.jre8</version>
</dependency>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
例外:
SQLServerException: The TCP/IP connection to the host {our server host here}, port 1433 has failed. Error: "connect timed out. 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.".
Stack: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...
Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host {our server host here}, port 1433 has failed. Error: "connect timed out. 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 org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:81)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:371)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:446)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:456)
at
答案1
托管实例只能通过私有 IP 地址访问,因此为了从 Azure 应用服务(或 Azure Functions)访问它,您首先需要在应用程序和托管实例 VNet 之间建立连接。请参阅将您的应用与Azure 虚拟网络。
答案2
微软提供了解决方案(针对 Azure 支持 Java 默认喜欢的 IPv6 的问题的解决方法):
添加 AppSetting
键名称:languageWorkers:java:arguments
值: -Djava.net.preferIPv4Stack=true