spring cloud EurekaClient multiple network adapters ip configuration and source code analysis (rpm)

https://blog.csdn.net/qq_30062125/article/details/83856655

 

 

1 Introduction
to spring cloud, each service instance need to register to Eureka registry.
Usually the ip registered, namely eureka.instance.prefer-ip-address = true.
However, if the service instance environment where there are multiple network cards, often registered ip ip past is not what we want appears.

2, the configuration instructions to help solve
the case for the above, we generally have several different ideas to solve.

2.1, a method: direct configuration eureka.instance.ip-address
such as: eureka.instance.ip-address = 192.168.1.7
directly configure a complete IP, generally applicable to a single scene environment, the lack of favorable support for complex scenes.

Specific implementation can refer org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration # eurekaInstanceConfigBean
and org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean # getHostName,
will not be described herein. If clear, the source logical analysis may look at the back, and then back to look at similar ideas.

2.2 Method II: increasing inetutils configuration
corresponding to the configuration org.springframework.cloud.commons.util.InetUtilsProperties, which comprises:

Configuration instructions
spring.cloud.inetutils.default-hostname default host name will be used only to resolve the error
spring.cloud.inetutils.default-ip-address default ip address will be used only to resolve the error
spring.cloud.inetutils. ignored-interfaces configured to ignore the network card address, with a plurality of divided
ip ip address or prefix spring.cloud.inetutils.preferred-networks matches a regular, with a plurality of divided, is the intersection relationship
spring.cloud.inetutils.timeout- ip host computing seconds timeout information, a second default
spring.cloud.inetutils.use-only-site-local- interfaces within a network ip only
exemplified:
using only the ip beginning at 192.168, a plurality of items is a note intersection relationship needs are met.
^ = 192-Networks spring.cloud.inetutils.preferred \ .168 \. [\ D] + \. [\ D] + $
. 1
using the / etc / hosts in host name mapping ip, in this kind of environment docker swarm in relatively easy to use.
# Configure a just could not exist ip, will come InetAddress.getLocalHost () logic.
Networks = none-spring.cloud.inetutils.preferred
. 1
2
negative NIC en0 and en1
# ignored-interfaces configured regular expression
spring.cloud.inetutils.ignored-= en0 the interfaces, EN1
. 1
2
the only network addresses
# 1918 to follow the RFC
# 10/8 prefix
# 172.16 / 12 prefix
# 192.168 / 16 prefix
spring Site--only-.cloud.inetutils.use-local = the interfaces to true
. 1
2
. 3
. 4
. 5
generally enough to use these types.

3, source code analysis
under the main analyze why this configuration can take effect. We will look down from the very beginning to start automatically configured. To see ip with part off, you can jump directly to the specified directory.

The version used here is spring-boot 1.5.13.RELEASE, spring cloud Dalston.SR5, different versions, there will be some differences.

3.1 server registration instructions
eureka server api common explanation see: https: //blog.csdn.net/qq_30062125/article/details/83829357

@EnableEurekaServer server code from the inlet, api Jersey implemented using sub ,, wherein the resource loader. Specific reference may be: https: //blog.csdn.net/qq_30062125/article/details/83758334

Our main concern when the transfer of registration application examples hostname, such as: <hostName> 192.168.1.7 </ hostName>

3.2 The client initiates a registration instructions
spring boot eureka client client logic can refer https://blog.csdn.net/qq_30062125/article/details/83833006

Our concern here is org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration # eurekaInstanceConfigBean, this is the information service instance.

@Bean
@ConditionalOnMissingBean(value = EurekaInstanceConfig.class, search = SearchStrategy.CURRENT)
public EurekaInstanceConfigBean eurekaInstanceConfigBean(InetUtils inetUtils) {
RelaxedPropertyResolver relaxedPropertyResolver = new RelaxedPropertyResolver(env, "eureka.instance.");
String hostname = relaxedPropertyResolver.getProperty("hostname");
boolean preferIpAddress = Boolean.parseBoolean(relaxedPropertyResolver.getProperty("preferIpAddress"));
// ip解析主要在这一步
EurekaInstanceConfigBean instance = new EurekaInstanceConfigBean(inetUtils);
instance.setNonSecurePort(this.nonSecurePort);
instance.setInstanceId(getDefaultInstanceId(this.env));
instance.setPreferIpAddress(preferIpAddress);

if (this.managementPort != this.nonSecurePort && this.managementPort != 0) {
if (StringUtils.hasText(hostname)) {
instance.setHostname(hostname);
}
String statusPageUrlPath = relaxedPropertyResolver.getProperty("statusPageUrlPath");
String healthCheckUrlPath = relaxedPropertyResolver.getProperty("healthCheckUrlPath");
if (StringUtils.hasText(statusPageUrlPath)) {
instance.setStatusPageUrlPath(statusPageUrlPath);
}
if (StringUtils.hasText(healthCheckUrlPath)) {
instance.setHealthCheckUrlPath(healthCheckUrlPath);
}
String scheme = instance.getSecurePortEnabled() ? "https" : "http";
instance.setStatusPageUrl(scheme + "://" + instance.getHostname() + ":"
+ this.managementPort + instance.getStatusPageUrlPath());
instance.setHealthCheckUrl(scheme + "://" + instance.getHostname() + ":"
+ this.managementPort + instance.getHealthCheckUrlPath());
}
return instance;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Since the above category EurekaInstanceConfigBean arranged @ConfigurationProperties ( "eureka.instance"), so that the bean generation process, the logic ConfigurationPropertiesBindingPostProcessor, injects configuration parameter file.

The bean will be processed into InstanceInfo, stored in the ApplicationInfoManager, ApplicationInfoManager will be injected into CloudEurekaClient. Finally, com.netflix.discovery.DiscoveryClient # DiscoveryClient assignment logic, logic for subsequent processing. \

org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration.EurekaClientConfiguration#eurekaApplicationInfoManager 源码:

@Bean
@ConditionalOnMissingBean(value = ApplicationInfoManager.class, search = SearchStrategy.CURRENT)
public ApplicationInfoManager eurekaApplicationInfoManager(
EurekaInstanceConfig config) {
// 先加工成InstanceInfo
InstanceInfo instanceInfo = new InstanceInfoFactory().create(config);
return new ApplicationInfoManager(config, instanceInfo);
}
1
2
3
4
5
6
7
8
DiscoveryClient部分源码如下:

@Inject
DiscoveryClient(ApplicationInfoManager applicationInfoManager, EurekaClientConfig config, AbstractDiscoveryClientOptionalArgs args,
Provider<BackupRegistry> backupRegistryProvider) {
......

this.applicationInfoManager = applicationInfoManager;
// 前面放入的InstanceInfo
InstanceInfo myInfo = applicationInfoManager.getInfo();

config = ClientConfig;
staticClientConfig = ClientConfig;
TransportConfig config.getTransportConfig = ();
// parameters assigned to instanceInfo, register logic for use
instanceInfo = MyInfo;
......
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
registration logic

/**
* Register with the eureka service by making the appropriate REST call.
*/
boolean register() throws Throwable {
logger.info(PREFIX + appPathIdentifier + ": registering service...");
EurekaHttpResponse<Void> httpResponse;
try {
// instanceInfo就是上面赋值的实例信息
httpResponse = eurekaTransport.registrationClient.register(instanceInfo);
} catch (Exception e) {
logger.warn("{} - registration failed {}", PREFIX + appPathIdentifier, e.getMessage(), e);
throw e;
}
if (logger.isInfoEnabled()) {
logger.info("{} - registration status: {}", PREFIX + appPathIdentifier, httpResponse.getStatusCode());
}
return httpResponse.getStatusCode() == 204;
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
3.3 IP parsing the configuration described
can be seen from the above application example of the information registered to it is through EurekaInstanceConfigBean eureka process. ip resolved mainly in the new EurekaInstanceConfigBean (inetUtils):

EurekaInstanceConfigBean constructor

EurekaInstanceConfigBean public (inetutils inetutils) {
this.inetUtils = inetutils;
// host information acquired, which contains the analytical ip
this.hostInfo this.inetUtils.findFirstNonLoopbackHostInfo = ();
// ip address that is obtained from a place in hostInfo.
= this.hostInfo.getIpAddress this.ipAddress ();
this.hostname this.hostInfo.getHostname = ();
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
@Override
public String getHostName (Boolean Refresh) {
IF (the this Refresh &&!. hostInfo.override) {
this.ipAddress this.hostInfo.getIpAddress = ();
this.hostname this.hostInfo.getHostname = ();
}
// Get host, if the configuration preferIpAddress, ip address to use.
? return this.preferIpAddress this.ipAddress: this.hostname;
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
finally achieved by org.springframework.cloud.commons.util.InetUtils # findFirstNonLoopbackAddress

public InetAddress findFirstNonLoopbackAddress() {
InetAddress result = null;
try {
int lowest = Integer.MAX_VALUE;
for (Enumeration<NetworkInterface> nics = NetworkInterface
.getNetworkInterfaces(); nics.hasMoreElements();) {
NetworkInterface ifc = nics.nextElement();
if (ifc.isUp()) {
log.trace("Testing interface: " + ifc.getDisplayName());
if (ifc.getIndex() < lowest || result == null) {
lowest = ifc.getIndex();
}
else if (result != null) {
continue;
}

// @formatter:off
// 网卡忽略逻辑
if (!ignoreInterface(ifc.getDisplayName())) {
for (Enumeration<InetAddress> addrs = ifc
.getInetAddresses(); addrs.hasMoreElements();) {
InetAddress address = addrs.nextElement();
// ip忽略逻辑
if (address instanceof Inet4Address
&& !address.isLoopbackAddress()
&& !ignoreAddress(address)) {
log.trace("Found non-loopback interface: "
+ ifc.getDisplayName());
result = address;
}
}
}
// @formatter:on
}
}
}
catch (IOException ex) {
log.error("Cannot get first non-loopback address", ex);
}

if (result != null) {
return result;
}

{the try
// ip when the rule matches than when directly using the logical access information.
the InetAddress.getLocalHost return ();
}
the catch (UnknownHostException E) {
log.warn ( "Unable to Retrieve localhost");
}

return null;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
3.3.1 Ignore card
does not match InetUtilsProperties # ignoredInterfaces configured regular expression NIC ignored

/ * ** for Testing / Boolean ignoreInterface (String interfaceName) {
for (String REGEX: this.properties.getIgnoredInterfaces ()) {
IF (interfaceName.matches (REGEX)) {
log.trace ( "Ignoring interface:" + interfaceName) ;
return to true;
}
}
return to false;
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
3.3.2 ip ignored
if ip network must, by org.springframework.cloud.commons.util.InetUtilsProperties # useOnlySiteLocalInterfaces control.
Match ip rule, intersection, you must match the number of regular expressions org.springframework.cloud.commons.util.InetUtilsProperties # preferredNetworks configuration, or exact match at the beginning, are not met, it is ignored.

/** for testing */ boolean ignoreAddress(InetAddress address) {

// whether the network must be IP
IF (this.properties.isUseOnlySiteLocalInterfaces () && address.isSiteLocalAddress ()!) {
Log.trace ( "Ignoring address:" + address.getHostAddress ());
return to true;
}

// match ip, a rule do not match, no match
for (String regex: this.properties.getPreferredNetworks ()) {
(!. address.getHostAddress () The matches (regex) && address.getHostAddress () startsWith (regex)!.) IF {
log.trace ( "Ignoring address:" + address.getHostAddress ());
return to true;
}
}
return to false;
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
17
network ip rule checking



























Find local host name
if the host name is localhost direct return address, which is ipv4 127.0.0.1, ipv6 is 1 ::
If not, you need to take the local domain name resolution. DNS Priority will get ip address of the local hosts file configuration. When configured as a host name of the hosts, it will read the ip address configuration.
If the local is not found, continue to take the domain name resolution.
Here, how to set ip spring cloud eureka client manager, we basically clear. Well, it's over.
----------------
Disclaimer: This article is CSDN blogger "This is a lazy" in the original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/qq_30062125/article/details/83856655

Guess you like

Origin www.cnblogs.com/xiaohanlin/p/11593667.html