1. Core concepts
- Curliest, curliest, curliest, curliest
- A component in the Spring Cloud collection for service registration and discovery.
- Adhere to AP Principles (CAP Principles)
- The Eureka version must be the same as the Spring Boot version; otherwise, compatibility problems may occur. Version number can be managed by dependencyManagement (as mentioned later)
CAP principle, also known as CAP theorem, refers to Consistency, Availability, and Partition tolerance in a distributed system. == The CAP principle states that these three elements can only be achieved at most at the same time, but not at all. = =
CAP theory is an important theory in distributed architecture
- Consistency (all nodes have the same data at the same time)
- Availability (Ensuring that each request is responded to regardless of success or failure)
- The loss or failure of any information in the system will not affect the continued operation of the system
2, use,
Eureka is divided into two main bodies: Eureka Server and Eureka Client.
2.1 Eureka Server
2.1.1 Adding a Dependency
To create a new project, check “dependencies->Cloud Discovery->Eureka ServerChoice of dependency
Add Eureka Server dependencies to existing project POM files:
<! - the following content is added in the dependencies node - > < the dependency > < groupId > org. Springframework. Cloud < / groupId > <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>Copy the code
Spring-cloud-starter -eureka-server: spring-cloud-starter-netflix-eureka-server: Spring-cloud-starter -eureka-server: Spring-cloud-starter -netflix-eureka-server Currently, spring-cloud-starter-eureka-server is not recommended. Use spring-cloud-starter- Netflix-eureka-server instead.
2.1.2 Adding annotations
Add the @enableeurekaserver annotation to the project entry class.
2.1.3 Modifying the Configuration
Open the… / SRC/main/resources/application. Yml file configuration of the following (if your project is application. The properties, you can change the suffix for yml, these two configuration files are available with engineering, yml format is more intuitive) :
Eureka: client: # eureka server address service-url: defaultZone: http://127.0.0.1:8081/eureka # do you need to register to the registry (registry clusters need to be set to true) register - with - eureka: False # Whether you need to search for service information because you are a registry, so false fetch-registry: falseCopy the code
The above configuration is simple. The key information is the service port number and the Eureka Server address, which is the key information for the Eureka Client to register the service.
After the Eureka Server configuration is complete, the project can be run. If there is no problem with the operation, open http://localhost:8081/ through the browser. If the content similar to the screenshot below appears, it indicates that the service center is running successfully, and other services can be registered at this time.
2.1.4 Possible Problems
2.1.4.1 Spring-Cloud-starter-Netflix-Eureka-server Version Problem
If only the Eureka Server dependency is selected for the newly created Module, it fails to run directly. The main reason is that the spring-cloud-starter-Netflix-Eureka-server dependency does not have a version number.
Refer to the link
Main reason: Only Eureka Server dependency is selected, and the dependency information added in the POM file does not have the version number:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
Copy the code
Solution: Add the version number to the dependency information of the POM file (version number view address)
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> < artifactId > spring - the cloud - starter - netflix - eureka - server < / artifactId > < version > 2.2.3. RELEASE < / version > < / dependency > </dependencies>Copy the code
Note: The version number of Eureka needs to be the same as that of Spring Cloud, otherwise the two will not work together
2.1.4.2 java.lang.NoSuchMethodError: org.springframework.boot.builder.SpringApplicationBuilder.([Ljava/lang/Object;)V
Note that the version number of Eureka must be the same as that of Spring Cloud. Otherwise, it is difficult to run the Eureka and Spring Cloud in a compatible manner. This problem may cause similar exception messages when the versions of Eureka and Spring Cloud are inconsistent.
Workaround: Keep Eureka and Spring Cloud versions consistent; Add the following information to the project node of the POM file to remove the version information that Eureka depends on:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Copy the code
Extension note: If Web dependency is selected when the Module is created, the above information is automatically added to the POM file.
2.2 Eureka Client
2.2.1 Adding a Dependency
Eureka Discovery Client dependencies->Cloud Discovery Client Note that services usually need to provide external interfaces, so Web dependencies need to be selected at the same time. There may be mutual calls between micro-services, so Feign dependencies need to be selected.
2.2.2 Adding annotations
Add @enableEurekaclient annotation to the project entry class, indicating that it is a Eureka client and can register with the Eureka Server. If the project has access to other Eureka Client microservices, add @enableFeignClients.
2.2.3 configuration
Add the following configuration information to the project application.yml:
Name: service-news eureka: client: service-url: Fill out the registration center server address # defaultZone: http://localhost:8081/eureka # do you need to register to register registration center - with - eureka: True # Whether to search for service information in the fetch-registry: true instance: # prefer-ip-address: true # instance-id: ${spring.cloud.client.ip-address}:${server.port}Copy the code
Once the Eureka Client is configured everywhere and the project is running successfully, the service can be viewed through the registry.
After the above steps, Eureka can be simple and practical, Eureka configuration information according to personal needs. Once services are registered in the service center, they can call each other through Feign, which is described in a subsequent article.