spring cloud consul Example Method for Using ip to Register Services

  • 2021-07-06 11:05:45
  • OfStack

When I tested spring cliud using consul as the registry, I found that when services were registered, they were all registered with hostname, such as:

Registered 1 commonservice, in consul as follows:


{
  "ID":"commonservice123",
  "address":"testcommonserver"
  ........
}

This is definitely wrong.

I have a service payservice that needs to call commonservice. The address of commonservice obtained by payservice from consul is testcommonserver, while the server address of payservice is on 121.57. 68.98. This server cannot resolve the ip address of the server where hostname is testcommonserver and cannot call commonservie. At this time, the following error will be reported:

unKnownHostException
.......

To solve this problem, I need to register the service as ip when registering the service. My test environment is:

spring cloud Finch1ey.SR2 consul v1.4.3

Modify the bootstrap. yml configuration file:


spring:
 cloud:
  consul:
   host: xxx.xxx.xxx.xxxx
   port: 8500
   discovery:
    prefer-ip-address: true // This must be matched 
    tags: version=1.0
    instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}
    healthCheckInterval: 15s
    health-check-url: http://${spring.cloud.client.ip-address}:${server.port}/actuator/health

${spring. cloud. client. ip-address} This attribute is built into spring cloud to get ip. Different spring cloud versions may vary slightly. If you want to determine what your version is, you can check this file:

HostInfoEnvironmentPostProcessor


  @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);
  }

At this time, start the project test again and find that the registered address has changed:


{
  "ID":"commonservice123",
  "address":"10.52.xx.xx"
  ........
}

The registered address becomes the intranet address of the service. If other services and commonservice are in the same network, they can be accessed through the intranet, which is also possible. However, if the intranet cannot be accessed, other services still cannot be accessed. At this time, it is necessary to register the service with ip of the public network.
Modify the bootstrap. yml configuration file:


spring:
 cloud:
  consul:
   host: xxx.xxx.xxx.xxx
   port: 8500
   config:
    data-key: data
    format: yaml
   discovery:
    prefer-ip-address: true // This must be matched 
    tags: version=1.0
    instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}
    healthCheckInterval: 15s
    health-check-url: http://${spring.cloud.client.ip-address}:${server.port}/actuator/health
  inetutils:
   preferred-networks:
    -  Public network ip1
    -  Public network ip2

It can be seen that an inetutils configuration is added, which is the network tool class of spring cloud. The meaning of this configuration is that if multiple ip (intranet and extranet) are acquired when acquiring ip, ip existing in ip configured by me will be preferentially selected, so that after re-testing, it will be found that when registering service, it will become public ip.


Related articles: