在Discovery Service中注册多个IP

时间:2019-05-01 11:04:43

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

我们正在当前项目中使用Netflix OSS,该项目将在几周内上线。直到上周一切都很好。服务已在发现中注册,网关能够将流量路由到所需的服务,门户运行正常。但是,当在每台服务器中都安装了备份NIC卡时。重新启动服务时,服务已使用第二个nat IP地址(10.1.220.125)而不是主机名(以前是主机名)注册。我搜索了google,发现了许多配置,但是当我申请时我没有得到成功/预期的结果。一种这样的配置如下:

spring:
  cloud:
    inetutils:
      ignoredInterfaces:
        - p2p.*

这是我的发现服务器的application.yml文件。

spring:
  application:
    name: discovery-service
server:
  port: 8761

eureka:
  client:
    registerWithEureka: true
    fetchRegistry: true
    healthcheck:
      enabled: true
    serviceUrl:
      defaultZone: http://10.1.115.8:8761/eureka/
  instance:
    registryFetchIntervalSeconds: 30
    leaseRenewalIntervalInSeconds: 10
    leaseExpirationDurationInSeconds: 30

zuul.sensitiveHeaders:

hystrix:
  command:
    default:
      execution:
        isolation:
          strategy: THREAD
          thread:
            timeoutInMilliseconds: 500000
spring:
  cloud:
    inetutils:
      ignoredInterfaces:
        - p2p.*

当我看到日志时,发现发现服务正在注册,如下所示:

Registered instance DISCOVERY-SERVICE/10.1.220.125:discovery-service:8761 with status UP (replication=false)
DiscoveryClient_DISCOVERY-SERVICE/10.1.220.125:discovery-service:8761 - registration status: 204
Registered instance DISCOVERY-SERVICE/10.1.220.125:discovery-service:8761 with status UP (replication=true)

其中10.1.220.125是新的nat IP。

以前,我以前只有一个nat IP(10.1.115.8)而没有备用IP时才在日志中获取消息:

Registered instance DISCOVERY-SERVICE/KOLSRV1:discovery-service:8761 with status UP (replication=false)
DiscoveryClient_DISCOVERY-SERVICE/KOLSRV1:discovery-service:8761 - registration status: 204
Registered instance DISCOVERY-SERVICE/KOLSRV1:discovery-service:8761 with status UP (replication=true)

其中KOLSRV1是服务器主机名,具有IP 10.1.115.8和10.1.220.125。

如何使用主机名而不是IP注册我的服务?

我们正在使用Dalston.SR1版本的Spring Cloud。

  

编辑:使用版本Greenwich.RC2

server:
  port: 8761

eureka:
  environment: prod
  client:
    registerWithEureka: true
    fetchRegistry: true
    healthcheck:
      enabled: true
    serviceUrl:
      defaultZone: http://10.1.115.8:8761/eureka/
  instance:
    registryFetchIntervalSeconds: 30
    leaseRenewalIntervalInSeconds: 10
    leaseExpirationDurationInSeconds: 30
    hostname: KOLSRV1
    ip-address: 10.1.115.8
    preferIpAddress: false
    #preferredNic: bond0
    #forceHostAddress: 10.1.115.8

zuul.sensitiveHeaders:

hystrix:
  command:
    default:
      execution:
        isolation:
          strategy: THREAD
          thread:
            timeoutInMilliseconds: 500000

spring:
  cloud:
    inetutils:
      ignoredInterfaces:
        - p2p.*

我收到以下错误:

Request execution failed with message: A message body reader for Java class com.netflix.appinfo.InstanceInfo, and Java type class com.netflix.appinfo.InstanceInfo, and MIME media type text/html; charset=utf-8 was not found
DiscoveryClient_DISCOVERY-SERVICE/10.1.220.125:discovery-service:8761 - was unable to send heartbeat!

供您参考,我提供服务器的ifconfig详细信息。

[user@KOLSRV1 ~]$ ifconfig
bond0: flags=5187<UP,BROADCAST,RUNNING,MASTER,MULTICAST>  mtu 1500
        inet 10.1.115.8  netmask 255.255.255.128  broadcast 10.1.115.127
        ether 00:0a:f7:e7:88:40  txqueuelen 1000  (Ethernet)
        RX packets 976056244  bytes 249957809358 (232.7 GiB)
        RX errors 0  dropped 0  overruns 0  frame 193559
        TX packets 937501062  bytes 243749426487 (227.0 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

em1: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
        ether 00:0a:f7:e7:88:40  txqueuelen 1000  (Ethernet)
        RX packets 975667166  bytes 249931945975 (232.7 GiB)
        RX errors 0  dropped 0  overruns 0  frame 193559
        TX packets 937501062  bytes 243749426487 (227.0 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

em2: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether 00:0a:f7:e7:88:41  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

em3: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether 00:0a:f7:e7:88:3e  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 16

em4: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether 00:0a:f7:e7:88:3f  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 17

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 23915308  bytes 7603519514 (7.0 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 23915308  bytes 7603519514 (7.0 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

p2p1: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
        ether 00:0a:f7:e7:88:40  txqueuelen 1000  (Ethernet)
        RX packets 389078  bytes 25863383 (24.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

p2p2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.1.220.125  netmask 255.255.254.0  broadcast 10.1.221.255
        ether 00:0a:f7:f3:03:01  txqueuelen 1000  (Ethernet)
        RX packets 81021  bytes 14512406 (13.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 196820  bytes 265515182 (253.2 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:ad:3c:27  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[user@KOLSRV1 ~]$
  

更新2

pom.xml

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath/>
    </parent>
    <artifactId>discovery-service</artifactId>
    <name>discovery-service</name>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.RC2</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-eureka-server</artifactId>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <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>
        <finalName>discovery-service</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>application.yml</exclude>
                    <exclude>*.xml</exclude>
                    <exclude>*.properties</exclude>
                </excludes>
                <filtering>false</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.sample.DiscoveryServiceApplication</mainClass>
                    <addResources>true</addResources>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <argLine>${argLine} -noverify</argLine>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>
</project>

bootstrap.yml

spring:
  application:
    name: discovery-service

MainApplication

@SpringBootApplication
@EnableEurekaServer
public class DiscoveryServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(DiscoveryServiceApplication.class, args);
    }
}

1 个答案:

答案 0 :(得分:1)

@spencergib,我找到了解决方案。

在/ etc / hosts文件中,我输入类似

KOLSRV1   10.1.220.125

现在,这些服务已使用名称而不是IP在发现服务中注册。

Registered instance DISCOVERY-SERVICE/KOLSRV1:discovery-service:8761 with status UP (replication=false)
DiscoveryClient_DISCOVERY-SERVICE/KOLSRV1:discovery-service:8761 - registration status: 204
Registered instance DISCOVERY-SERVICE/KOLSRV1:discovery-service:8761 with status UP (replication=true)

谢谢..