Spring Cloud配置ip注册找不到${spring.cloud.client.ipAddress}

前言

多台windows机器部署Spring Cloud项目,Eureka上面注册的 instanceId 是这样的

在通过 serviceId 相互调用时出现Unkownhost异常,不能通过主机名找到服务提供者。由于没有使用docker,又不想去配置host,所有改用ip进行服务注册。

修改为ip注册

在Eureka client添加配置

eureka:
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ipAddress}:${server.port}

重启服务...

看来这个配置根本没有获取到client的ip。猜想这个参数是否已经过时。

参考spring could采坑(一)eureka找不到${spring.cloud.client.ipAddress}

SpringCloud 2.0 已经改成 ${spring.cloud.client.ip-address} 了,于是修改

eureka:
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}

重启服务...
发现还是读不到这个值

于是想去看这个值到底从哪里读的
Spring Cloud Eureka 多网卡配置最终版 一文中看到是从 HostInfoEnvironmentPostProcessor 这个类中获取的。Google之

找到HostInfoEnvironmentPostProcessor源码

package org.springframework.cloud.client;

import java.util.LinkedHashMap;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.config.ConfigFileApplicationListener;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.cloud.commons.util.InetUtils.HostInfo;
import org.springframework.cloud.commons.util.InetUtilsProperties;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;

/**
 * @author Spencer Gibb
 */
public class HostInfoEnvironmentPostProcessor
        implements EnvironmentPostProcessor, Ordered {

    // Before ConfigFileApplicationListener
    private int order = ConfigFileApplicationListener.DEFAULT_ORDER - 1;

    @Override
    public int getOrder() {
        return this.order;
    }

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment,
            SpringApplication application) {
        InetUtils.HostInfo hostInfo = getFirstNonLoopbackHostInfo(environment);
        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
        map.put("spring.cloud.client.hostname", hostInfo.getHostname());
        map.put("spring.cloud.client.ip-address", hostInfo.getIpAddress());
        MapPropertySource propertySource = new MapPropertySource(
                "springCloudClientHostInfo", map);
        environment.getPropertySources().addLast(propertySource);
    }

    private HostInfo getFirstNonLoopbackHostInfo(ConfigurableEnvironment environment) {
        InetUtilsProperties target = new InetUtilsProperties();
        ConfigurationPropertySources.attach(environment);
        Binder.get(environment).bind(InetUtilsProperties.PREFIX,
                Bindable.ofInstance(target));
        try (InetUtils utils = new InetUtils(target)) {
            return utils.findFirstNonLoopbackHostInfo();
        }
    }
}

可以看到的确修改成了 ${spring.cloud.client.ip-address} ,这个类在# spring-cloud-commons 项目中。

于是导入

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

重启服务,已经正常通过ip注册调用


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容