Spring Cloud ip configuration register can not find $ {spring.cloud.client.ipAddress}

Multiple windows machine deployment Spring Cloud projects registered top instanceId Eureka is such a
7995157-a634b1b46565b8f4.png

Unkownhost appear abnormal when calling each other by serviceId, the host name can not be found by the service provider. Since no docker, do not want to configure the host, all use ip conduct service registration.
Modify the ip register

Add a profile in Eureka client

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

Restart service ...
7,995,157-e6c697271a18b5ea.png

It seems this configuration did not get into the client's ip. I guess whether this parameter is outdated.

Reference spring could Pit (a) can not find the $ {spring.cloud.client.ipAddress} Eureka

SpringCloud 2.0 has been changed to $ {spring.cloud.client.ip-address}, so modified

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

Restart Service ...
found or can not read this value

So in the end I want to see the value read from where
Spring Cloud Eureka multi-card configuration to see the final version of the article is taken from HostInfoEnvironmentPostProcessor this class. Google's

Source find 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();
            }
        }
    }

We can see that indeed modified into $ {spring.cloud.client.ip-address}, this class # spring-cloud-commons project.

Then import

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

Restart the service, it has been the normal registration call by ip

 

Guess you like

Origin blog.csdn.net/rubbertree/article/details/92761563