[SpringCloud] service registration - Eureka cluster environment construction + consumer side registration + provider side registration

Posted by dv_evan on Fri, 14 Jan 2022 01:59:14 +0100

The relationship between the three is shown in the figure:

Note: 7001 + 7002 is Eurake service cluster; 80 is the summer end of the service caller; 8001 + 8002 is the service provider. Eureka service clusters should register and watch each other when registering.

(1) Eureka service cluster environment construction and registration

  1. Find the hosts file in the path C:\Windows\System32\drivers\etc, modify the mapping configuration and add it to the hosts file
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
  1. Create module cloud Eureka server7001

  2. Import package (pom.xml)

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
  1. Write configuration (application.yml)
server:
  port: 7001

eureka:
  instance:
    hostname: eureka7001.com #Instance name of eureka server
  client:
    register-with-eureka: false     #false means that you do not register yourself with the registry.
    fetch-registry: false     #false means that my client is the registry. My responsibility is to maintain service instances and I don't need to retrieve services
    service-url:
      #Cluster points to other eureka
      defaultZone: http://eureka7002.com:7002/eureka/
      #A single machine is 7001 itself
      #defaultZone: http://eureka7001.com:7001/eureka/
  server:
    enable-self-preservation: false
    eviction-interval-timer-in-ms: 2000

Note: the url in the defaultZone is the address of the 7002 module. The purpose of this writing is that the module clusters in Eureka should register and watch each other.
Things to configure:
1. Port
2. Host name
3. Registered watch 7002

  1. Main startup class (EurekaMain7001)
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaMain7001.class, args);
    }
}

Note: the @ EnableEurekaServer annotation must be added to the main startup class. The table name of this module is the registration service module of Eureka

5. Test
Enter web address: http://eureka7001.com:7001/ You can check whether the configuration is successful

Note: the first arrow indicates that the Eureka module has registered Eureka 7002 com´╝Ť The second arrow indicates the consumer module and Provider module registered in Eureka. It is empty because there is no module registered at this time.

Note: the registration of 7002 module in Eureka is consistent with the above steps.

(2) The module in consumer is registered into Eureka

  1. Create 80 modules in consumer


2. Guide Package (pom.xml)

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
  1. Write configuration (application.yml)
server:
  port: 80

spring:
  application:
    name: cloud-order-service

eureka:
  client:
    #Indicates whether to register yourself with Eurekaserver. The default value is true.
    register-with-eureka: true
    #Whether to retrieve the existing registration information from EurekaServer. The default value is true. Single node doesn't matter. The cluster must be set to true to use load balancing with ribbon
    fetchRegistry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka

Contents to be configured:
1. Port
2.consumer name
3. Register in 7001 and 7002
4. Main startup class (OrderMain80)

@SpringBootApplication
@EnableEurekaClient
public class OrderMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderMain80.class, args);
    }
}

Note: the @ EnableEurekaClient annotation must be added to the main startup class to indicate that the module is a module registered in Eureka.

  1. test
    Since 7001 and 7002 are registered in this module, you can see module 80 on Eureka websites of both modules.
    eureka7001.com:

    eureka7002.com:

(3) The provider side registers with Eureka

  1. Create module 8001


2. Guide Package (pom.xml)

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
  1. Write configuration (application.yml)
server:
  port: 8001

spring:
  application:
    name: cloud-payment-service

eureka:
  client:
    #Indicates whether to register yourself with Eurekaserver. The default value is true.
    register-with-eureka: true
    #Whether to retrieve the existing registration information from EurekaServer. The default value is true. Single node doesn't matter. The cluster must be set to true to use load balancing with ribbon
    fetchRegistry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
  instance:
      instance-id: payment8001
      prefer-ip-address: true

Contents to be configured:
1. Port
2.provider name
3. Register in 7001 and 7002

  1. Main program class (PaymentMain8001)
@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8001 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain8001.class, args);
    }
}

Note: the @ EnableEurekaClient annotation must be written to indicate that the module is registered into Eureka.

  1. test
    eureka7001.com:


eureka7002.com:

Note: all modules in the provider can be registered into eureka in the above way.

Summary:
1. The module guide package in Eureka is different from other module guide packages. The former is spring cloud starter Netflix Eureka server and the latter is spring cloud starter Netflix Eureka client.
2. The module import methods in consumer and provider are basically the same.

Topics: Java Back-end