无法注册多个尤里卡实例

时间:2017-11-02 10:49:57

标签: spring spring-boot docker-compose netflix-eureka

我正在尝试创建两个Eureka服务器实例。但是当我运行它时,它们无法互相注册。我得到的错误是:

  

com.netflix.discovery.DiscoveryClient:从eureka服务器获取所有实例注册表信息   2017-11-02 15:51:14.125 ERROR 34 --- [main] c.n.d.s.t.d.RedirectingEurekaHttpClient:请求执行错误

     

com.sun.jersey.api.client.ClientHandlerException:java.net.ConnectException:拒绝连接(拒绝连接)       在com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:187)〜[jersey-apache-client4-1.19.1.jar!/:1.19.1]

主要申请文件是:

@SpringBootApplication
@EnableEurekaServer
public class RegistrationServer {
	public static void main(String[] args) {
		System.setProperty("spring.config.name", "registration-server");

		SpringApplication.run(RegistrationServer.class, args);
	}
}

registration-server.yml是:

---
spring.profiles: server-1
    
eureka:
  instance:
    hostname: localhost #eureka-server
  server:
    enableSelfPreservation: false
  client:
    register-with-eureka: true
    fetch-registry: true
    serviceUrl:
     defaultZone: http://server-2:2424/eureka/
     

server:
  port: 2323   # HTTP (Tomcat) port
  
  
  
spring:
 application:
  name: eureka-server1
---

spring.profiles: server-2
    
eureka:
  instance:
    hostname: localhost #eureka-server
  server:
    enableSelfPreservation: false
  client:
    register-with-eureka: true
    fetch-registry: true
    serviceUrl:
     defaultZone: http://server-1:2323/eureka/
     

server:
  port: 2424   # HTTP (Tomcat) port
  
  
  
spring:
 application:
  name: eureka-server2
---

docker-compose文件是:

version: '2'
services:
  lb:
   image: dockercloud/haproxy
   volumes:
     - /var/run/docker.sock:/var/run/docker.sock
   ports:
     - "80:80"
     - "1936:1936"

  eureka-service-1:
    image: qaregistry.test.com/registration-server:0.0.2
    ports:
     - "2323:2323"
    environment:
     - APPBINARY=registration-server.jar
     - "SPRING_PROFILES_ACTIVE=server-1"
    extra_hosts:
     - "server-1:127.0.0.1"
     - "server-2:127.0.0.1"
    entrypoint:
     - /usr/bin/jarrun.sh
     - QA

  eureka-service-2:
    image: qaregistry.test.com/registration-server:0.0.2
    ports:
     - "2424:2424"
    environment:
     - APPBINARY=registration-server.jar
     - "SPRING_PROFILES_ACTIVE=server-2"
    extra_hosts:
     - "server-1:127.0.0.1"
     - "server-2:127.0.0.1"
    entrypoint:
     - /usr/bin/jarrun.sh
     - QA
pox.xml是:

<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>com.test</groupId>
   <artifactId>registration-server</artifactId>
    <version>0.0.2</version>

  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.7</java.version>
    </properties>

    <dependencies>
		<dependency>
			<!-- Setup Spring Boot -->
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<!-- Setup Spring MVC & REST, use Embedded Tomcat -->
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		
		<dependency>
			<!-- Spring Cloud starter -->
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter</artifactId>
		</dependency>
		<dependency>
			<!-- Eureka service registration -->
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka-server</artifactId>
		</dependency>
		
	</dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Brixton.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            
        </dependencies>
    </dependencyManagement>

    <build>  
     <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

                     <!-- added by linux team for docker integration with maven -->
                        <plugin>
                                <groupId>com.spotify</groupId>
                                <artifactId>docker-maven-plugin</artifactId>
                                <version>0.4.13</version>
                                <configuration>
                                        <serverId>dockerhub</serverId>
                                        <imageName>qaregistry.test.com/${project.artifactId}:${project.version}</imageName>
                                         <pullOnBuild>true</pullOnBuild>
                                         <registryUrl>https://qaregistry.test.com</registryUrl>
                                        <baseImage>qaregistry.test.com/test-jdk:8u121</baseImage>
                                        <maintainer>USER user@test.com</maintainer>
                                        <labels>
                                                <label>ProductName=${project.build.finalName}</label>
                                        </labels>
                                        <user>test</user>
                                        <entryPoint>["/usr/bin/jarrun.sh"]</entryPoint>
                                        <resources>
                                           <resource>
                                                  <targetPath>/data/test/run/</targetPath>
                                                  <directory>${project.build.directory}</directory>
                                                  <include>${project.build.finalName}.jar</include>
                                          </resource>
                                        </resources>        
                                </configuration>
                        </plugin>

        </plugins>
    </build>
  
 
 
</project>

eureka的两个实例,即server-1和server-2都正常运行。但他们没有相互注册。

  

附上屏幕截图以获取更多信息:server-1
  server-2

4 个答案:

答案 0 :(得分:1)

尝试修改您的registration-server.yml,如下所示:您在defaultZone之前错过了两个.yml文件的 ONE 空间。顺便说一句,这些文件中仍有许多语法错误。

对于eureka-server-1:

eureka:
  ...
  client:
    ...
    serviceUrl:
      defaultZone: http://server-2:2424/eureka/

对于eureka-server-2:

eureka:
  ...
  client:
    ...
    serviceUrl:
      defaultZone: http://server-1:2323/eureka/

这是我的一个尤里卡服务器设置,它可以很好地使两个服务器相互注册:

spring:
  application:
    name: registry

server:
  port: 2323

eureka:
  server:
    eviction-interval-timer-in-ms: 15000
  client:
    fetch-registry: true
    register-with-eureka: true
    serviceUrl:
      defaultZone: ${eureka_url:http://localhost:2424/eureka}
  instance:  
    instance-id: ${spring.application.name}:${random.value} 
    hostname: ${eureka_host:localhost} 
    secure-port: ${server.port}
    non-secure-port-enabled: true
    non-secure-port: ${server.port}

答案 1 :(得分:0)

我不确定Eureka是否可以注册另一个Eureka,但概念上看起来它也是一个正在运行的服务。因此可以充当服务和服务器(尤里卡)。但我不完全确定。

无论如何,我认为你应该在你的Eurekaserverapplication中试试这个

@SpringBootApplication
@EnableEurekaClient
@EnableEurekaServer
public class RegistrationServer {
    public static void main(String[] args) {
        System.setProperty("spring.config.name", "registration-server");

        SpringApplication.run(RegistrationServer.class, args);
    }
}

尝试将@EnableEurekaClient@EnableDiscoveryClient放入启动应用程序。由于您的应用程序应该充当客户端和服务器。 所以,我认为那些注释很重要。

答案 2 :(得分:0)

不确定,尝试更改

eureka:
  instance:
    hostname

到服务器-1&#39;和&#39; server-2&#39;相应

答案 3 :(得分:0)