spring-bootconsulspring-cloud-consul

my spring-cloud-consul demo not register to consul server


I am learning spring cloud with consul as service discovery implementation, I followed a tutorial on internet. I am using eclipse and maven. The pom file is generated by Spring Initializr. From spring official documentation, they says "@EnableDiscoveryClient" is no more needed, so I commented it, but with this annoatation makes no difference.

my code is below:

package org.codingfarm.ailurus;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
//@CONFIGURATION
//@ENABLEAUTOCONFIGURATION
//@EnableDiscoveryClient
@RestController
public class AccountsApplication {

  @RequestMapping("/")
  public String home() {
    return "Hello World";
  }

  public static void main(String[] args) {
    SpringApplication.run(AccountsApplication.class, args);
  }

}

maven pom:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.codingfarm.ailurus</groupId>
    <artifactId>ailurus-accounts</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>ailurus-accounts</name>
    <description>账户服务</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud-task.version>2.0.0.RELEASE</spring-cloud-task.version>
        <spring-cloud.version>Finchley.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</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-integration</artifactId> -->
<!--        </dependency> -->
<!--        <dependency> -->
<!--                <groupId>org.springframework.cloud</groupId> -->
<!--            <artifactId>spring-cloud-stream-binder-kafka-core</artifactId> -->
<!--        </dependency> -->
<!--        <dependency> -->
<!--            <groupId>org.springframework.cloud</groupId> -->
<!--            <artifactId>spring-cloud-stream-binder-kafka</artifactId> -->
<!--        </dependency> -->
<!--        <dependency> -->
<!--            <groupId>org.springframework.boot</groupId> -->
<!--            <artifactId>spring-boot-starter-webflux</artifactId> -->
<!--        </dependency> -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>
<!--        <dependency> -->
<!--            <groupId>org.apache.kafka</groupId> -->
<!--            <artifactId>kafka-streams</artifactId> -->
<!--        </dependency> -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
<!--        <dependency> -->
<!--            <groupId>org.springframework.cloud</groupId> -->
<!--            <artifactId>spring-cloud-bus</artifactId> -->
<!--        </dependency> -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-commons</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
        </dependency>
<!--        <dependency> -->
<!--            <groupId>org.springframework.cloud</groupId> -->
<!--            <artifactId>spring-cloud-starter-netflix-turbine-stream</artifactId> -->
<!--        </dependency> -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-oauth2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-task</artifactId>
        </dependency>
<!--        <dependency> -->
<!--            <groupId>org.springframework.cloud</groupId> -->
<!--            <artifactId>spring-cloud-stream</artifactId> -->
<!--        </dependency> -->
<!--        <dependency> -->
<!--            <groupId>org.springframework.cloud</groupId> -->
<!--            <artifactId>spring-cloud-stream-binder-kafka</artifactId> -->
<!--        </dependency> -->
<!--        <dependency> -->
<!--            <groupId>org.springframework.cloud</groupId> -->
<!--            <artifactId>spring-cloud-stream-binder-kafka-streams</artifactId> -->
<!--        </dependency> -->
<!--        <dependency> -->
<!--            <groupId>org.springframework.kafka</groupId> -->
<!--            <artifactId>spring-kafka</artifactId> -->
<!--        </dependency> -->

<!--        <dependency> -->
<!--            <groupId>mysql</groupId> -->
<!--            <artifactId>mysql-connector-java</artifactId> -->
<!--            <scope>runtime</scope> -->
<!--        </dependency> -->

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
<!--        <dependency> -->
<!--            <groupId>org.springframework.cloud</groupId> -->
<!--            <artifactId>spring-cloud-stream-test-support</artifactId> -->
<!--            <scope>test</scope> -->
<!--        </dependency> -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-task-dependencies</artifactId>
                <version>${spring-cloud-task.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.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>

I started a consul server using following command:

./consul agent -server -bootstrap-expect=1 -data-dir=/tmp/consul -ui -bind=127.0.0.1

but when I run it as spring boot application, it just never register my service to consul, did I miss anything?


Solution

  • Tried to launch application with your pom.xml. It fails with next message:

    ***************************
    APPLICATION FAILED TO START
    ***************************
    
    Description:
    
    Field registration in org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration$ServiceRegistryEndpointConfiguration required a single bean, but 2 were found:
        - eurekaRegistration: defined in BeanDefinition defined in class path resource [org/springframework/cloud/netflix/eureka/EurekaClientAutoConfiguration$RefreshableEurekaClientConfiguration.class]
        - consulRegistration: defined by method 'consulRegistration' in class path resource [org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationAutoConfiguration.class]
    

    You need to disable Eureka discovery client by removing dependency from your pom.xml:

    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
    </dependency>
    

    Because it loads spring-cloud-starter-netflix-eureka-client dependency which contains implementation for Eureka registration.