- Mastering Spring Cloud
- Piotr Minkowski
- 337字
- 2021-08-27 18:57:05
Preferring the IP address
By default, all instances are registered under their hostname. It is a very convenient approach, on the assumption that we have DNS enabled on our network. However, it is not uncommon that DNS is not available for a group of servers used as the microservice environment in the organization. I just had that kind of situation myself. There remains nothing else to do but to add host names and their IP addresses to the /etc/hosts file on all of the Linux machines. An alternative to this solution is to change the registration process configuration settings to advertise the IP addresses of services rather than the hostname. To achieve this, the eureka.instance.preferIpAddress property should be set to true on the client side. Every service instance in the registry will still be printed out to a Eureka dashboard with instanceId containing a hostname, but if you click this link the redirection will be performed based on the IP address. The Ribbon client that is responsible for calling other services via HTTP will also follow the same principle.
If you decide to use an IP address as a primary method of determining the network location of the service, you may have a problem. The problem may arise if you have more than one network interface assigned to your machine. For example, in one organization where I have been working, there were different networks for a management mode (a connection from my workstation to the server) and for a production mode (a connection between two servers). In consequence, each server machine had two network interfaces assigned with different IP prefixes. To select the right interface, you can define a list of ignored patterns in the application.yml configuration file. For example, we would like to ignore all interfaces where the name starts with eth1:
spring: cloud: inetutils: ignoredInterfaces: - eth1*
There is also another way to get that effect. We can define network addresses that should be preferred:
spring: cloud: inetutils: preferredNetworks: - 192.168