【问题标题】:Failed to configure a DataSource: 'url' while using ActiveDirectoryMSI, MSSQL and SQLServerDataSource使用 ActiveDirectoryMSI、MSSQL 和 SQLServerDataSource 时无法配置数据源:“url”
【发布时间】:2021-04-10 14:42:47
【问题描述】:

我在 Azure 上部署了一个使用 MSSQL 的 Springboot 应用程序。它使用 ActiveDirectoryMSI 进行身份验证。 Data Source Config 类如下所示

@Configuration
@Slf4j
public class DataSourceConfig {

    @Value("${sql.databaseName}")
    private String sqlDbName;

    @Value("${sql.serverName}")
    private String sqlServer;

    @Value("${sql.msiClientId}")
    private String sqlIdentity;

    @Bean
    public void connectToDb() {

        SQLServerDataSource ds = new SQLServerDataSource();
        ds.setServerName(sqlServer + ".database.windows.net");
        ds.setDatabaseName(sqlDbName);
        ds.setMSIClientId(sqlIdentity);
        ds.setAuthentication("ActiveDirectoryMSI");

        try (Connection connection = ds.getConnection()) {
            log.info("Connected to database using my MSI");
        } catch (SQLServerException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

这些变量的所有值都存储在 KeyVault 中。问题是在构建之后,当我尝试部署应用程序时,它需要 application.yml 中的 url,我没有,因为所有这些信息都应该来自 azure 上的 Keyvault。所以它给了我以下错误。我不能给它一个 url,因为这种 MSI 方式不需要 url

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
        If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
        If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

这是我的 pom.xml。我没有在 application.yml 中放任何与 Spring.datasource 相关的东西

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.modelmapper</groupId>
            <artifactId>modelmapper</artifactId>
            <version>2.3.8</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit-jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-engine</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
    </dependencies>

知道如何解决这个问题吗?谢谢

【问题讨论】:

  • 嗨,有趣,也许这可能会引起您的兴趣stackoverflow.com/questions/51221777/…
  • @IronMan 我认为问题在于它需要 application.yml 中的 url 但如果我给它一个,它将尝试连接到它而不是使用的数据课程配置。我需要以某种方式告诉应用不要担心数据源变量

标签: azure spring-boot datasource azure-keyvault mssql-jdbc


【解决方案1】:

想通了。必须将 h2 依赖项的范围从测试更改为运行时

【讨论】:

    猜你喜欢
    • 2020-10-14
    • 1970-01-01
    • 1970-01-01
    • 2023-02-21
    • 2022-07-21
    • 2017-02-18
    • 2021-05-14
    • 2019-03-25
    • 2019-08-24
    相关资源
    最近更新 更多