Process diagram of spring cloud building Eureka

Time:2021-7-30

This article mainly introduces the process diagram of spring cloud building Eureka. It is introduced in great detail through the example code, which has certain reference value for everyone’s study or work. Friends in need can refer to it

Establishment of Eureka server

Eureka is the registry of spring cloud, which provides the functions of service registration and service discovery.

Using idea to quickly create an Eureka application

File – NewProject-Spring Initalizr

1. Utilization https://start.spring.io Create spring cloud Eureka application

Fill in the Maven and other information of the application, and then go to the next step

Select Eureka server, and our build is based on spring boot version 2.2.0-release

After selecting the path, finish creating the project

2. You can see that in the process of building the project, the package of Eureka server required by our department has been introduced into the project in the POM file


<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

3. Add configuration (use YML, and change application.properties to application.yml)

spring:
 application:
  name: spring-eureka
server:
 port: 8761

#Spring Eureka registered address
eureka:
 client:
  service-url:
   defaultZone: http://127.0.0.1:8761/eureka/
  Register with Eureka: false # whether to register with Eureka
  Fetch registry: false # whether to obtain synchronization information from Eureka. A single section can be set to false
 server:
  Occurrence interval timer in MS: 10000 # clearing invalid node time
  Enable self preservation: false # whether to enable self-protection. Eureka will count that if the percentage of heart beat failures in 15 minutes is less than 85%, the protection mechanism will be triggered and the service providers will not be removed. If the service registry is closed, the unavailable instances will be removed correctly

4. Add annotation @ enableeurekaserver for startup class


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class SpringEurekaApplication {

  public static void main(String[] args) {
    SpringApplication.run(SpringEurekaApplication.class, args);
  }

}

5. Start

6. Start the configuration of multiple Eureka instances

You only need to set multiple URLs in the service URL, separated by commas

Each application is registered on Eureka

1. Import configuration


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

2. Application.yml configuration


eureka:
 client:
  service-url:
   defaultZone: http://localhost:8761/eureka/
 instance:
  prefer-ip-address: true

3. Startup class annotation


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class SpringUserApplication {

  public static void main(String[] args) {
    SpringApplication.run(SpringUserApplication.class, args);
  }

}

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.

Recommended Today

A detailed explanation of the differences between Perl and strawberry Perl and ActivePerl

Perl is the abbreviation of practical extraction and report language “practical report extraction language”. Application of activestateperl and strawberry PERL on Windows platformcompiler。 Perl   The relationship between the latter two is that C language and Linux system have their own GCC. The biggest difference between activestate Perl and strawberry Perl is that strawberry Perl […]