【问题标题】:Add a consul dependency to existing spring boot application in Intellij向 Intellij 中现有的 Spring Boot 应用程序添加 Consul 依赖项
【发布时间】:2020-04-05 17:55:18
【问题描述】:

我正在尝试在我的 Spring Boot (Maven) 应用程序中使用 consul 添加“服务发现”,但无法使用 @EnableDiscoveryClient。因为,我是 Intellij 的新手,所以我不确定我错过了什么。

添加了 pom。

我做了以下事情:

  1. 在我的 pom.xml 中添加了 consul 依赖项

  1. 尝试重新导入 maven 依赖项。见下文:

        <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <parent>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-parent</artifactId>
          <version>2.2.6.RELEASE</version>
          <relativePath/> <!-- lookup parent from repository -->
      </parent>
      <groupId>com.mancala</groupId>
      <artifactId>MancalaFrontend</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <name>MancalaFrontend</name>
      <description>Front-end service for Mancala game.</description>
    
      <properties>
          <java.version>1.8</java.version>
          <vaadin.version>14.1.23</vaadin.version>
      </properties>
    
      <dependencies>
          <!-- Actuator -->
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-actuator</artifactId>
          </dependency>
    
          <!-- Web Starter -->
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-web</artifactId>
          </dependency>
    
          <!-- Consul  -->
          <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-starter-consul-discovery</artifactId>
          </dependency>
    
          <!-- Vaadin -->
          <dependency>
              <groupId>com.vaadin</groupId>
              <artifactId>vaadin-spring-boot-starter</artifactId>
          </dependency>
    
          <!-- Test -->
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-test</artifactId>
              <scope>test</scope>
              <exclusions>
                  <exclusion>
                      <groupId>org.junit.vintage</groupId>
                      <artifactId>junit-vintage-engine</artifactId>
                  </exclusion>
              </exclusions>
          </dependency>
      </dependencies>
    
      <!-- Vaadin Dependency Management-->
      <dependencyManagement>
          <dependencies>
              <dependency>
                  <groupId>com.vaadin</groupId>
                  <artifactId>vaadin-bom</artifactId>
                  <version>${vaadin.version}</version>
                  <type>pom</type>
                  <scope>import</scope>
              </dependency>
          </dependencies>
      </dependencyManagement>
    
      <build>
          <plugins>
              <plugin>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-maven-plugin</artifactId>
              </plugin>
          </plugins>
      </build>
    
    </project>
    

【问题讨论】:

  • 不要使用图片,最好使用发布的文本。此外,您的项目是否建立在普通命令行上?
  • 我不确定我是否完全理解“在普通命令行上构建”
  • 您是否尝试过通过mvn clean package 在普通控制台中构建?
  • 你是怎么做到的? @khmarbaise
  • 没有神器版本。您是否为 spring-cloud 添加了依赖管理?如果您需要更多帮助,请分享您的整个 pom。

标签: spring-boot maven consul service-discovery


【解决方案1】:

您尚未为 spring-cloud-starter-consul-discovery 指定工件版本。

注意:您不必为 Spring Boot 工件指定版本,因为父 pom 对它们具有依赖关系。 Consul starter 是 Spring Cloud 的一部分,Spring Cloud 版本需要单独管理

有两种方法可以做到这一点

  • 直接依赖,通过version标签
  • 在dependencyManagement标签中

我鼓励您采用后一种方法,Spring 在兼容版本中提供了包含所有 Spring Cloud 依赖项的包含。 这样做:

  • 在&lt;properties&gt; 中添加&lt;spring-cloud.version&gt;Hoxton.SR3&lt;/spring-cloud.version&gt;
  • 在dependencyManagement中添加以下内容
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>${spring-cloud.version}</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

我在 Spring Initializr 页面上生成了具有所需依赖关系的 pom,从您的 pom 中选择 Spring Boot 版本以获得匹配的 Spring Cloud 版本

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-07
    • 2018-12-01
    • 2020-12-12
    • 1970-01-01
    • 2017-01-30
    • 2023-03-10
    相关资源
    最近更新 更多