Spring Cloud learning Eureka service registration and discovery

Posted by Hades on Thu, 16 Dec 2021 14:37:08 +0100

Establishment of Eureka service registry

New module

Modify POM xml

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!--    Introduce custom api General package, you can use Payment payment Entity    -->
        <dependency>
            <groupId>com.yu.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!-- boot web actuator -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

Create application yml

server:
  port: 7001

eureka:
  instance:
    #Instance name of eureka server
    hostname: localhost
  client:
    #false means that you do not register yourself with the registry
    register-with-eureka: true
    #false means that its own side is the registry. Its responsibility is to maintain instances and does not need to retrieve services
    fetch-registry: false
    service-url:
      #This address is required for setting the address query service and registration service interacting with Eureka Server
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

Create main startup class

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

Start test

Register the service providers and consumers built in the previous article into eureka

Modify the POM of cloud provider payment8001 xml

 		<!--   introduce eureka client     -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

Modify yml

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka

Modify main startup class

cloud-consumer-order80 is the same as cloud-provider-payment8001. I won't write more

Test page

Eureka cluster construction

Create a new cloud Eureka server7002

  1. Change pom (copy from cloud Eureka server7001 pom)

  2. C: Add the hosts file under the path of \ windows \ system32 \ drivers \ etc

    127.0.0.1  eureka7001.com
    127.0.0.1  eureka7002.com
    

Modify yml (7001)

server:
  port: 7001

eureka:
  instance:
    #Instance name of eureka server
    hostname: eureka7001.com
  client:
    #false means that you do not register yourself with the registry
    register-with-eureka: false
    #false means that its own side is the registry. Its responsibility is to maintain instances and does not need to retrieve services
    fetch-registry: false
    service-url:
      #This address is required for setting the address query service and registration service interacting with Eureka Server
      defaultZone: http://eureka7002.com:7002/eureka/

Modify yml (7002)

server:
  port: 7001

eureka:
  instance:
    #Instance name of eureka server
    hostname: eureka7002.com
  client:
    #false means that you do not register yourself with the registry
    register-with-eureka: false
    #false means that its own side is the registry. Its responsibility is to maintain instances and does not need to retrieve services
    fetch-registry: false
    service-url:
      #This address is required for setting the address query service and registration service interacting with Eureka Server
      defaultZone: http://eureka7001.com:7001/eureka/

Copy the main startup class of 7001 to 7002

Register the provider micro service and consumer micro service into two eureka (yml)

test

  1. Start two eureka
  2. Start provider
  3. Start consumer

Provider 8001 cluster environment construction (80018002)

Refer to cloud provider payment8001 to create a new cloud provider payment8002, and modify the yml port of cloud provider payment8002 to 8002

Modify the controller of 8001 and 8002

Now that there are two providers, you need to modify the access address of the consumer

There are already two payment providers. Now you need to use the @ LoadBalanced annotation to give RestTemplate load balancing

@Configuration
public class ApplicationContextConfig {
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

test

  1. Start eureka cluster
  2. Start provider cluster
  3. Start consumer

Finally, the 8001 and 8002 interfaces will be called alternately to realize load balancing and build successfully

About eureka's self-protection

**Overview: * * by default, if EurekaServer does not receive the heartbeat of a micro service instance within a certain time, EurekaServer will log off the instance (90 seconds by default). However, in case of network partition failure (delay, jamming and congestion), the micro service and EurekaServer cannot communicate normally. The above behavior may become very dangerous. First, the micro service itself is actually healthy. At this time, the micro service should not be cancelled. Eureka solves this problem through "self-protection mode". First, when Eureka Server node loses too many clients in a short time (network partition failure may occur), the node will enter self-protection mode. If you see the following prompt on the homepage of Eureka Server, it indicates that Eureka has entered the protection mode.

How to configure disable self-protection:

eureka:
  server:
    enable-self-preservation: false

Topics: Spring Cloud eureka