Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to choose IP for Eureka service registration based on multiple network cards

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to choose the IP of Eureka service registration based on multiple network cards". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Eureka service registration IP selection problem scenario in multi-network card environment

Two network cards, eth0 and eth2, are configured on the server, and only the address of eth2 can be accessed by other machines. In this case, Eureka Client will automatically select eth0 as the service ip during service registration, resulting in the inability of other services to call.

Cause of the problem

Since there is no official logic for Eureka Client to detect native IP, you can only look at the source code. The source code of Eureka Client is under the eureka-client module, and the InstanceInfo class under the com.netflix.appinfo package encapsulates the native information, including the IP address. In

In Spring Cloud environment, Eureka Client does not implement the logic of detecting native IP by itself, but hands it over to the findFirstNonLoopbackAddress () method of Spring's InetUtils utility class:

Public InetAddress findFirstNonLoopbackAddress () {InetAddress result = null; try {/ / record network card minimum index int lowest = Integer.MAX_VALUE; / / get all network cards for (Enumeration 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 () / / record index} else if (result! = null) {continue } / / @ formatter:off if (! ignoreInterface (ifc.getDisplayName () {/ / is the ignored network card for (Enumeration addrs = ifc .getInetAddresses (); addrs.hasMoreElements ()) ) {InetAddress address = addrs.nextElement () 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;} try {return InetAddress.getLocalHost (); / / if none of the above logic finds a suitable network card, use JDK's InetAddress.getLocalhost ()} catch (UnknownHostException e) {log.warn ("Unable to retrieve localhost");} return null;}

As can be seen from the source code, the tool class will get all the NICs and traverse them in turn, taking the ip address of the Nic with reasonable ip address and the lowest index value as the result. If you still haven't found a suitable IP, use InetAddress.getLocalHost () as the final fallback scenario.

Solution

a. Ignore the specified network card

Spring.cloud.inetutils.gnored-interfaces [0] = eth0 # ignores eth0 and supports regular expressions

Because the application ignores invalid network cards by configuring application.properties.

b. Configure host

When the network search logic does not find a suitable ip, it will go to JDK's InetAddress.getLocalHost (). This method returns the hostname of the current host, and then parses the corresponding ip based on hostname. So the second option is to configure the native hostname and / etc/hosts files to map the native hostname directly to a valid IP address.

c. Specify IP manually

# specify that the ipeureka.instance.ip-address=# of this instance is registered with ip instead of the hostname eureka.instance.prefer-ip-address=true

d. Specify IP at startup

Java-jar-Dspring.cloud.inetutils.preferred-networks=192.168.20.123

e. Disable eth0

View the connection information of the network card

[root@localhost] # nmcli con shNAME UUID TYPE DEVICE System eth0 5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03 802-3-ethernet eth0

Disable eth0

[root@localhost] # ifdown eth0Device 'eth0' successfully disconnected.

Enable eth0

[root@localhost ~] # ifup eth0 uses IP Registration Service in Eureka

When micro-services are deployed on multiple CVMs in docker, it is found that the machine name is displayed in eureka, and then a spring boot admin monitoring platform is built, and it is found that it cannot find the corresponding hosts for each micro-service.

Find eureka.instance.prefer-ip-address=true on the Internet, use this configuration eureka to show the ip address, but it is still not enough, it still can not be connected in the monitoring platform.

You also need to configure instance- and hostname for clients to access the instance

The effect should be like this. After clicking ip, you can access the corresponding content.

The eureka server configures the hostname of the server.port=8666spring.application.name=eureka-server# service registry instance eureka.instance.hostname=xxx.xxx.xxx.67# retained service example is less than what percentage to enter the protected mode eureka.server.renewal-percent-threshold=0.5# whether to turn on the protected mode eureka.server.enable-self-preservation=true# whether to register itself with the service registry whether eureka.client.register-with-eureka=false# is enabled to obtain service registration information Because this is a single-point Eureka Server. There is no need to synchronize the data of other Eureka Server nodes, so setting it to falseeureka.client.fetch-registry=false# registration and query depends on this address Multiple eureka.client.serviceUrl.defaultZone= http://admin:password@xxx.xxx.xxx.67:8666/eureka/# separated by commas use ip instead of instance name eureka.instance.prefer-ip-address=true# to set the ID of the instance to ip:porteureka.instance.instance-id=xxx.xxx.xxx.67:$ {server.port} # here use spring security to do a basic user name and password to the registry to log in to security.basic.enabled=truesecurity.user.name=adminsecurity.user.password=password

Note that:

Eureka.instance.hostname=xxx.xxx.xxx.67eureka.instance.instance-id=xxx.xxx.xxx.67:$ {server.port}

Here I manually specify ip instead of using ${spring.cloud.client.ipAddress} to obtain the local ip, because after using docker, I found that the obtained ip is the ip assigned on the network card docker0, and the ip that begins with 172.16.xxx.xxx. There are also a lot of materials to bind the docker to the public network ip. To simplify the operation here, you can specify the ip manually.

Client configuration eureka.client.service-url.defaultZone= http://admin:password@xxx.xxx.xxx.67:8666/eureka/eureka.instance.lease-renewal-interval-in-seconds=5eureka.instance.lease-expiration-duration-in-seconds=10eureka.client.healthcheck.enable=trueeureka.instance.hostname=xxx.xxx.xxx.67# sets the ID of the instance to ip:porteureka.instance.instance-id=xxx.xxx.xxx.67:$ {server.port}

Note that the client should also write eureka.instance.instance-id and eureka.instance.hostname

So the ip address is displayed on the eureka.

For spring boot admin to work properly, you also need to configure it on spring boot admin

Admin server configures spring.application.name=admin-monitorserver.port=7001# for security, you can set the management port later # management.port=7002# now the test environment has turned off authentication, but the real environment still needs its management.security.enabled=false client configuration # turn off authentication to avoid 401 error management.security.enabled=false#admin monitoring configuration Connect to the server spring.boot.admin.url= http://xxx.xxx.xxx.96:7001# and register the display spring.boot.admin.client.prefer-ip=true as ip in spring boot admin

The key step here is to configure spring.boot.admin.client.prefer-ip=true in the client, so that spring boot admin can access each micro-service endpoint through ip, and then collect their information to monitor each micro-service.

This is the end of the content of "how to choose IP for Eureka service registration based on multiple network cards". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report