We’ve been using Eureka to register services, but you probably don’t care much about how the service registers with Eureka Server. Or IP address?

Build the project

We took the SpringCloud component from the previous section: Hengboy-spring-cloud-eureka-register-away and simply modify the application.yml configuration file, as shown below:

# service name
spring:
  application:
    name: hengboy-spring-cloud-eureka-register-away

# service provider port number
server:
  port: 20001

Configure Eureka Server information
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10000/eureka/
  # Custom instance number
  instance:
    instance-id: ${spring.application.name}:${server.port}:@project.version@
Copy the code

In the above configuration, no changes have been made to the registration mode. If you start now, of course, you will use the default registration mode.

Viewing the Default Mode

We still use the SpringCloud component: build the source code for the Eureka Service Registry as the Service registry (Eureka Server) for testing in this chapter.

Test steps:
  1. Start the service registry
  2. Start the chapter project
  3. accesshttp://localhost:10000Open theService RegistryManagement interface
  4. Click services List services to view the address bar address

When we click hengboy-Spring-Cloud-Eureka-Register-Away :20001: V1.0 service name, it will jump to the monitoring information interface of the service, but we did not add monitoring dependency or configuration, so the page 404 cannot be accessed after jumping here. Even so we can still see the jump web site is http://192.168.1.75:20001/actuator/info, it had been also confirmed the Client to the Eureka Server to register when the default is the IP Address.

So if you want to use a host name to register services, how to configure? Read on.

The configuration uses the host name

If we use the hostname to register the service, we only need to modify the eureka.instance.hostname configuration information in the application.yml configuration file, as follows:

Configure Eureka Server information
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10000/eureka/
  # Custom instance number
  instance:
    instance-id: ${spring.application.name}:${server.port}:@project.version@
    Configure the host name registration service
    hostname: node1
Copy the code

Node1 is one of my natively configured host names

  • OS X/LinuxChange the host name in the system

I use MAC OS X as the operating environment. Therefore, modify the mapping of the host name and IP address in the /etc/hosts file, as shown in the following:

# #
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
# #
127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost
127.0.0.1       node1
127.0.0.1       node2
Copy the code
  • WindowsChange the host name in the system

If you are running a Windows operating system, you can modify the contents of the C: Windows\System32\drivers\etc\hosts file and add mappings.

After changing the host name, do not forget that the host name is required to take effect, the most effective way is to restart your computer to take effect.

Next we need to follow the following steps to testHost name registrationHas it taken effect?
  1. Restart the project in this chapter
  2. The refreshEureka ServerManagement Platform Interface
  3. Click the service name to see the jump address

We can find the path of the jump by the original way to modify the default http://192.168.1.75:20001/actuator/info into http://node1:20001/actuator/info, you can see is to use the form of the host name registered service!! !

Configure the preferred IP address

If you cannot use hostname for some reason, you can choose not to configure eureka.instance.hostname. If you still want to use IP Address for service registration after configuring eureka.instance.hostname. In this case, you can use the eureka.instance.prefer-ip-address parameter. If this parameter is set to true, the IP address is preferred for service registration. The configuration is as follows:

Configure Eureka Server information
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10000/eureka/
  # Custom instance number
  instance:
    instance-id: ${spring.application.name}:${server.port}:@project.version@
    Configure the host name registration service
    hostname: node1
    IP address registration is preferred
    prefer-ip-address: true
Copy the code
Specific test process with the aboveThe configuration uses the host nameConsistent, you can try to run tests.

Since we can use IP to register service preferentially, we want to register according to the specified IP address how to configure?

The specified IP address is used

The specified IP address is also easy to configure. You can change the registered IP address by setting the value of eureka.instance.ip-address. Based on the configuration file of the previous step, we made the changes as follows:

Configure Eureka Server information
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10000/eureka/
  # Custom instance number
  instance:
    instance-id: ${spring.application.name}:${server.port}:@project.version@
    Configure the host name registration service
    hostname: node1
    IP address registration is preferred
    prefer-ip-address: true
    Configure to use the specified IP addressIP - address: 127.0.0.1Copy the code

After the configuration file is modified, perform the following steps to check whether the modification is invalid:

  1. Restart the project in this chapter
  2. The refreshEureka ServerManagement Platform Interface
  3. Click the service name to view the jump address information

We found that the jump bar have been used for we configure IP address – the address parameter, the address is: http://127.0.0.1:20001/actuator/info.

Note: If prefer-ip-address: true is not enabled after the ip-address parameter is set, the host name or the default registration method will still be used.

conclusion

Our way through several different services registered to fully explain the Eureka Client during registration to the service registry using host information, the registration ways also has certain priority order, the knowledge points in the next chapter we combine had the source code for all the registered ways respectively and priority order.

Source location

This chapter source code has been uploaded to Heng Yu teenager code cloud, please study in combination with the source code, thank you for reading.

  • SpringBoot matching source address: access code cloud view source code, access GitHub view source code
  • SpringCloud source code address (The source code of this chapter is here) :Access code cloud to view the source code

Face to face with Hengyu youth

If you like heng Yu teenager related articles, then go to wechat public number (Heng Yu teenager) follow me!! Of course, you can also go to SpringCloud code cloud source code project bottom scan wechat public qr code to follow me, thank you for reading!!

Study catalog recommendation

  • For a series of articles on SpringCloud visit: Directory: The SpringCloud Core Technology Learning directory
  • For SpringBoot related articles, visit: Directory: SpringBoot learning directory
  • For a series of QueryDSL articles, visit the QueryDSL Generic Query Framework learning directory
  • For articles in the SpringDataJPA series, visit: Directory: SpringDataJPA Learning directory

Open source information

During this period of time, I have been writing relevant open source frameworks, devoting myself to the framework upgrade and open source plan used by the company, and upgrading and reconstructing the tools and plug-ins used by the company and opening source.

  • Code Buildercode-builderCode generators are based on the template files you provide (currently supportedfreemarker) automatically generate entity classes, can greatly improve the efficiency of development.Gitee address:Gitee.com/hengboy/cod… Making the address:Github.com/hengyuboy/c…
  • Persistence Framework (MyBatis-Enhance)mybatis-enhanceIs a rightmybatisThe enhanced encapsulation of the framework provides a series of internal methods to perform operations on single-table data, and multi-table data is providedDSLMode to perform operations.Gitee address:Gitee.com/hengboy/myb… Making the address:Github.com/hengyuboy/m…
  • Automatic paging plug-inMyBatis-PageableIs a plug-in for automatic paging, based onMyBatisInternal pluginsInterceptorInterceptor written, interceptExecutor.queryThe two overloaded methods of compute the paging information as well as the database based on the configurationDialectAutomatic execution of different query statements to complete the total number of statistics.Gitee address:Gitee.com/hengboy/myb…