代码之家  ›  专栏  ›  技术社区  ›  WestFarmer

我的spring云consur演示没有注册到consur服务器

  •  0
  • WestFarmer  · 技术社区  · 7 年前

    我正在学习springcloud和consul作为服务发现的实现,我遵循了一个关于internet的教程。我正在使用eclipse和maven。pom文件由 . 从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);
      }
    
    }
    

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

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

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

    但当我把它作为spring启动应用程序运行时,它从来没有把我的服务注册到consul,我错过了什么吗?

    1 回复  |  直到 7 年前
        1
  •  2
  •   nmyk    7 年前

    尝试用您的 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]
    

    您需要通过从服务器中删除依赖项来禁用Eureka discovery客户端

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

    因为它会加载 spring-cloud-starter-netflix-eureka-client 包含Eureka注册实现的依赖项。