首页 文章

我的spring-cloud-consul演示没有注册到consul服务器

提问于
浏览
0

我正在以 Consul 作为服务发现实现学习spring cloud,我在互联网上学习了一个教程 . 我正在使用eclipse和maven . pom文件由 Spring Initializr 生成 . 从 Spring 季官方文档来看,他们说"@EnableDiscoveryClient"不再需要了,所以我对此进行了评论,但这种说法没有任何区别 .

我的代码如下:

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>

我使用以下命令启动了一个consul服务器:

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

但当我把它作为 Spring 季启动应用程序运行时,它只是从未将我的服务注册到 Consul ,我是否错过任何东西?

1 回答

  • 1

    试图使用 pom.xml 启动应用程序 . 它失败并显示下一条消息:

    ***************************
    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]
    

    您需要通过从 pom.xml 中删除依赖项来禁用Eureka发现客户端:

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

    因为它加载 spring-cloud-starter-netflix-eureka-client 依赖项,其中包含Eureka注册的实现 .

相关问题