What is Spring Cloud Eureka
Spring Cloud Eureka is a component in Spring Cloud. It is secondary encapsulated based on Netflix Eureka, which is mainly responsible for completing the service governance functions in the microservice framework. Spring Cloud adds Spring Boot-style automation configuration to Eureka. We only need simple reference dependencies and annotations to enable Spring Boot to easily integrate microservice applications with Eureka service governance system.
Service Governance
Service governance is the most core and basic module in the microservice framework. It is mainly used to realize the automated registration and discovery of various microservice instances.
At the beginning, there were not many microservice system services that might be built, and we can complete the call of the service through some static configuration. For example, there are two services A and B. When service A needs to call service B to complete a business operation, in order to achieve high availability of service B, whether we use complex balance on the server or load balancing on the client, we need to manually maintain a B instance list. However, with the development of the business, the system functions become more and more complex, and the corresponding microservices are becoming more and more difficult.
To solve this problem, a large number of service governance frameworks and products have been generated. These frameworks actually revolve around service registration and service discovery mechanisms to complete the automated management of microservice application instances.
Service registration
In the service governance framework, a service registration center is usually built. Each service instance unit registers its own services with the registration center, and tells the registration center a series of additional information such as the instance host location, port number, version number, communication protocol, etc., and the registration center organizes the service list according to the service name.
Service Discovery
Since operating under the service governance framework, communication and calls between services are no longer implemented by specifying specific instance addresses, but by initiating request calls to the service name. Therefore, when the service caller calls the service provider's interface, he does not know the specific service instance location. Therefore, it is necessary to first initiate a query request to the registry and obtain an instance list to achieve access to specific service instances.
Build Eureka Registration Center Service
Build a single center Eureka
All our subsequent developments will be based on the above version. It should be noted that Sring Cloud uses the SNAPSHOT version, so you need to specify the address of the repository in the pom file.
First, we create a new Spring Boot project, named it: spring-cloud-eureka, and add necessary dependencies to the pom. The specific pom file is as follows:
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.sagesource</groupId> <artifactId>spring-cloud-eureka</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-cloud-eureka</name> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Finchley.BUILD-SNAPSHOT</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependency> </dependencies> <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> </dependency> </dependency> </dependencyManagement> <build> <plugins> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories></project>Start a service registry to provide conversations to other applications through the @EnableEurekaServer annotation. This step is very simple, just add this annotation to a normal Spring Boot application to enable this function:
@SpringBootApplication@EnableEurekaServerpublic class ApplicationEurekaServer { public static void main(String[] args) { SpringApplication.run(ApplicationEurekaServer.class, args); }}Under the default configuration, the registration center server will also register itself as a client. Generally, we need to disable this function, and the modified application.yml is as follows:
# spring configspring: application: name: spring-boot-eureka# server configserver: port: 9871# eureka configureureka: instance: hostname: localhost client: register-with-eureka: false #Don't register yourself with the registration center fetch-registry: false #Don't retrieve the service service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ Since we are all running locally, for subsequent service distinction, we set the port of the service registry through server.port to run the port as: 9871
After completing the above configuration, start the application and visit http://localhost:9871. You can see the following page where the list of Instance currently registered with Eureka is empty, indicating that no service has been registered to the registration center.
I think everyone has noticed the two lines of eye-catching red words above:
EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.
Cause: Self-protection mechanism. During the operation of Eureka Server, it will count whether the proportion of heartbeat failure is less than 85% within 15 minutes. If it occurs (it is easy to satisfy during stand-alone debugging, but in fact it is usually caused by network instability in the production environment), Eureka Server will protect the current instance registration information and prompt this warning.
Since this problem is prone to occur in stand-alone situations, after turning off self-protection according to the online configuration, Eureka will still call an alarm, prompting that the safe mode is turned off, and the correctness of the instance cannot be guaranteed. Therefore, we ignore this problem for the time being and can be solved when cluster deployment is later.
Register a Service Provider
After completing the establishment of the registration center service, we can try to add an existing Spring Boot application to Eureka's service governance system.
We still take the previous spring-cloud-server as an example. We only need to modify the following configuration of application.yml:
# Application name spring: application: name: spring-cloud-server# eureka Registration center location eureka: client: service-url: defaultZone: http://localhost:9871/eureka/
After the modification is completed, the service can be started. At this time, we refresh the management page of eureka and see the list information of Instance currently registered with Eureka as follows:
This shows that our service has been successfully registered in the Registration Center.
Related example code: https://github.com/sagesource/spring-cloud-set
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.