Search code examples
spring-bootmavenconsulservice-discovery

Add a consul dependency to existing spring boot application in Intellij


I am trying to add "Service Discovery" using consul in my Spring Boot (Maven) application but unable to use @EnableDiscoveryClient. Since, I am new to Intellij I am not sure what I am missing.

Added pom.

I did the following:

  1. Added consul dependency in my pom.xml

enter image description here

  1. Tried to re-import maven dependencies. See below:

        <?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>
    

Solution

  • You have't specified artifact version for spring-cloud-starter-consul-discovery.

    NOTE: you didn't have to specify versions for Spring Boot artifacts, because the parent pom has dependency for them. Consul starter is part of Spring Cloud and Spring Cloud version needs to be managed separately

    There are 2 ways to do it

    • directly in dependency, via version tag
    • in dependencyManagement tag

    I encourage you to take the latter approach, Spring provides an include with all Spring Cloud dependencies in compatible version. To do it:

    • Add <spring-cloud.version>Hoxton.SR3</spring-cloud.version> in <properties>
    • Add the following in dependencyManagement
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>${spring-cloud.version}</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
    

    I generated the pom with desired dependency on Spring Initializr page selecting Spring Boot version from your pom to obtain matching Spring Cloud version