客户端不向Eureka服务器注册

时间:2018-04-23 15:35:39

标签: java spring netflix-eureka spring-cloud-netflix service-discovery

我让我的Eureka服务器(注册表)upend正在运行。但是,它表明它已注册,但不可用。

我离开了教程here,它说要关闭日志记录并阻止服务器自行注册。

编辑:已确认问题与服务器无关。它现在可以注册自己。另外,我使用了这个演示中的客户端(在一个完全独立的项目中),它使用我的服务器注册自己。

我的客户现在不运行。以下是pom文件中的依赖项:

<dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0.2</version>
        </dependency>

        <dependency>
            <groupId>org.nt.springframework.data</groupId>
            <artifactId>nt-spring-data-marklogic</artifactId>
            <version>1.1.8.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-commons</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream</artifactId>
            <version>2.0.0.RC3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-stream-binder-kafka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-kafka</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</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-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <scope>test</scope>
        </dependency>

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-servlet</artifactId>
    <version>1.11</version>
</dependency>

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
    <version>1.11</version>
</dependency>

这是客户主文件:

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.marklogic.repository.config.EnableMarkLogicRepositories;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;


@EnableDiscoveryClient
@SpringBootApplication
@EnableMarkLogicRepositories("com.example.repository")
@ComponentScan({"com.id.example"})
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class ClientApp {

    public static void main(String[] args) {
        System.out.println("Starting app...");
        SpringApplication.run(ClientApp.class, args);    

    }
}

这是错误:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v1.5.10.RELEASE)

[2018-04-23 16:27:44.811] - 91242 INFO [main] --- org.apache.catalina.core.StandardService: Starting service [Tomcat]
[2018-04-23 16:27:44.816] - 91242 INFO [main] --- org.apache.catalina.core.StandardEngine: Starting Servlet Engine: Apache Tomcat/8.5.27
[2018-04-23 16:27:45.163] - 91242 INFO [localhost-startStop-1] --- org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/]: Initializing Spring embedded WebApplicationContext
[2018-04-23 16:28:04.271] - 91242 INFO [main] --- org.apache.catalina.core.StandardService: Stopping service [Tomcat]
Exception in thread "main" org.springframework.context.ApplicationContextException: Failed to start bean 'eurekaAutoServiceRegistration'; nested exception is java.lang.NullPointerException
    at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178)
    at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:50)
    at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:348)
    at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:151)
    at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:114)
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:880)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
    at com.ibm.tsd.TradeServiceDemoApplication.main(TradeServiceDemoApplication.java:21)
Caused by: java.lang.NullPointerException
    at org.springframework.cloud.netflix.eureka.serviceregistry.EurekaServiceRegistry.maybeInitializeClient(EurekaServiceRegistry.java:56)
    at org.springframework.cloud.netflix.eureka.serviceregistry.EurekaServiceRegistry.register(EurekaServiceRegistry.java:37)
    at org.springframework.cloud.netflix.eureka.serviceregistry.EurekaAutoServiceRegistration.start(EurekaAutoServiceRegistration.java:80)
    at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:175)
    ... 14 more

任何帮助将不胜感激!

3 个答案:

答案 0 :(得分:1)

您必须将“服务器应用程序”(称为EurekaServerApplication)与“客户端应用程序”(称为EurekaClientApplication)分开:

在服务器应用程序中添加eureka-server依赖项:

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

您的服务器应用程序必须使用@EnableEurekaServer批注装饰

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {..}

将'eureka-client'依赖项添加到您的客户端应用程序的pom.xml中:

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

在客户application.propertiesapplication.yml中,您必须具有:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

端口值8761当然必须与服务器应用程序中的端口值相同:

server:
  port: ${PORT:8761}

注意:您无需使用@EnableEurekaClient@EnableDiscoveryClient注释修饰ClientEurekaApplication,即可从ServerEurekaApplication或从ServerEurekaApplication发现您的客户端。

答案 1 :(得分:0)

我正在为这样的客户使用配置(yml):

server:
  port: 8080
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: clientname

您可以继续使用application.properties代替yml。只需添加spring.application.name=applicationname

即可

请检查您是否忘记设置applicationname。 在开始discovery-service之前,请确保client已启动。

要查看eureka-service,您需要转到http://localhost:8761。 要访问client使用http://localhost:8761/clientname

答案 2 :(得分:-1)

我在一个必须升级的项目中遇到此错误,并且修复了该错误,从而消除或删除了Maven中的某些库依赖项。在为杰克逊5或泽西岛相关内容选择正确的库时,这会导致问题。

查看您的依赖性层次结构,然后开始播放或订购商品。