How to change single-machine version of Eureka service to cluster version of Eureka service

Eureka Cluster Principle

Fundamentals

The above figure is an official architecture diagram from eureka, which is based on cluster configuration.

  • Ereka at different nodes synchronizes data through Replicate
  • Application Service as Service Provider
  • Application Client for Service Consumer
  • Make Remote Call completes a service call

After the service is started, it registers with Eureka, which synchronizes the registration information with other Eureka Server s. When a service consumer calls a service provider, it obtains the service provider address from the service registry, caches the service provider address locally, and completes a call directly from the local cache the next time it is called.

When the service registry Eureka Server detects that the service provider is unavailable due to downtime or network reasons, it sets the service in the DOWN state in the service registry, publishes the current service provider state to subscribers, and the subscribed service consumer updates the local cache.

Eureka Single-machine Edition to Cluster Improvement Process

Create two new Eureka module projects

New modules (springcloudDemo-Eureka-7002 and springcloudDemo-Eureka-7003), refer to Eclipse for building a Maven subcontractor project and building a server for how to create new modules under the parent project


Note: The corresponding Eureka in the screenshot is that three Eureka services form the Eureka cluster

Copy from springcloudDemo-Eureka-7001

Since you are learning SpringCloud Eureka, the Eureka service doesn't do much, so copy the previous settings from the single-machine version of Eureka


The contents below are copied to the corresponding other Eureka cluster members
Special note: Please do not copy the springcloudDemo-Eureka-7001 directly from Eclipse to other projects for convenience. This will result in an error. So don't be lazy!!!

Modify the startup class names of the two new modules

This can also be modified without much impact

Modify mappings in hosts

Windows location: C:\Windows\System32\driversetc

127.0.0.1  eureka7001.com
127.0.0.1  eureka7002.com
127.0.0.1  eureka7003.com

Modify the configuration files for the three Eureka services

The following describes the configuration files for the three services:

  • springcloudDemo-Eureka-7001
    Need to connect to routing addresses of two other services
server: 
  port: 7001
 
eureka:
  instance:
    hostname: eureka7001.com #Instance name of eureka server side, hosts of C:\Windows\System32\drivers\etc have joined the mapping
  client:
    register-with-eureka: false #false means that you do not register yourself with the registry.
    fetch-registry: false #false says that my client is the registry and my responsibility is to maintain service instances without having to retrieve services
    service-url:
      #stand-alone
      #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/#Set up the address query and registration services that interact with Eureka Server depend on this address.
      #colony
      defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  • springcloudDemo-Eureka-7002
server: 
  port: 7002
 
eureka:
  instance:
    hostname: eureka7002.com #Instance name of eureka server side, hosts of C:\Windows\System32\drivers\etc have joined the mapping
  client:
    register-with-eureka: false #false means that you do not register yourself with the registry.
    fetch-registry: false #false says that my client is the registry and my responsibility is to maintain service instances without having to retrieve services
    service-url:
      #stand-alone
      #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/#Set up the address query and registration services that interact with Eureka Server depend on this address.
      #colony
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/
  • springcloudDemo-Eureka-7003
server: 
  port: 7003
 
eureka:
  instance:
    hostname: eureka7003.com #Instance name of eureka server side, hosts of C:\Windows\System32\drivers\etc have joined the mapping
  client:
    register-with-eureka: false #false means that you do not register yourself with the registry.
    fetch-registry: false #false says that my client is the registry and my responsibility is to maintain service instances without having to retrieve services
    service-url:
      #stand-alone
      #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/#Set up the address query and registration services that interact with Eureka Server depend on this address.
      #colony
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/

Service Provider Modifies Configuration File from Single Machine Edition to Cluster

Principle: When registering a service, change from registering to one to registering to more than one

	  #Single machine version
      #defaultZone: http://localhost:7001/eureka
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

test

Start three Eureka services, form an Eureka cluster, and then start the service provider

Open the browser and enter:

  • http://eureka7001.com:7001/
  • http://eureka7002.com:7002/
  • http://eureka7003.com:7003/

    You can view service 8001, registered with three Eureka clusters, and the Eureka cluster started normally.
    Note: Why http://eureka7003.com:7003/ The interface will fail because the Eureka protection mechanism does not affect it.

This completes the transformation from the previous single-machine version to the cluster Eureka. As for service consumer access, since there is only one producer, it is not obvious at this time, then a cluster of service producers will be built, and then service consumers will be tested, which makes a better comparison.

Keywords: Java Dubbo eureka Cloud Native

Added by kobmat on Tue, 14 Dec 2021 22:06:55 +0200