Home  >  Article  >  Java  >  In-depth analysis of springCloud's Eureka practice

In-depth analysis of springCloud's Eureka practice

无忌哥哥
无忌哥哥Original
2018-07-20 11:30:223070browse

1. First introduction to springcloud

Microservices are an architectural approach that will ultimately require a technical architecture to be implemented.

There are many ways to implement microservices, but the most popular one is Spring Cloud

What Spring is best at is integration, taking the best frameworks in the world and integrating them into your own projects middle.

The same is true for Spring Cloud. It integrates some of the most popular technologies now and implements functions such as: configuration management, service discovery, intelligent routing, load balancing, fuses, control bus, cluster status and so on. Its main components include:

  • Eureka: Registration Center

  • Zuul: Service Gateway
  • Ribbon: Load Balancing

  • Feign: Service Call

  • Hystix: fuse

Today we mainly get to know springcloud’s registration center Eureka

Here is an example in life:

Before the emergence of online ride-hailing, people could only call a taxi when going out. Some private cars want to be rented but are not qualified and are called black cars. Many people want to book a taxi, but unfortunately there are too few taxis and it is inconvenient. There are many private cars but they dare not stop them, and among the cars on the street, who knows which ones are willing to carry people. One wants and the other is willing to give, but there is a lack of introduction and management.

At this time, online ride-hailing platforms like Didi appeared. All private cars that want to carry passengers must register with Didi, and record your car model (service type) and identity information (contact information). Private cars that provide such services can be found on Didi, and they are clearly visible at a glance.

Anyone who wants to call a car at this time only needs to open the APP, enter your destination, select a car model (service type), and Didi will automatically arrange a car that meets your needs to serve you.

Back to springcloud’s Eureka, Eureka is like Didi, responsible for managing and recording service provider information. Service callers do not need to find services themselves, but tell Eureka their needs, and then Eureka will tell you the services that meet your needs. At the same time, the "heartbeat" mechanism is used to monitor the relationship between the service provider and Eureka. When a problem occurs with a service provider, Eureka will naturally remove it from the service list.

This realizes automatic registration, discovery, and status monitoring of services.

In-depth analysis of springClouds Eureka practice

Eureka: It is the service registration center (can be a cluster), exposing its own address to the outside world

Provider: Register your own information with Eureka after startup ( address, what services are provided)

Consumer: Subscribe to Eureka for a service, Eureka will send a list of all provider addresses of the corresponding service to the consumer, and regularly update

heartbeat (renewal) : Providers regularly refresh their status to Eureka through http

Practice:

Eureka registration center structure chart:

In-depth analysis of springClouds Eureka practice

##Complete the pom.xml file and add dependencies

The following is the main part of the pom file

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.0.3.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.RELEASE</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>
</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>
   </dependencies>
</dependencyManagement>

<build>
   <plugins>
      <plugin>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
   </plugins>
</build>

Edit startup class

@SpringBootApplication
@EnableEurekaServer // 声明这个应用是一个EurekaServer
public class SpringcloudEurekaServerApplication {

   public static void main(String[] args) {

      SpringApplication.run(SpringcloudEurekaServerApplication.class, args);
   }
}

Write application.yml configuration

server:
  port: 8081 # 端口
spring:
  application:
    name: eureka-server # 应用名称,会在Eureka中显示
eureka:
  client:
    register-with-eureka: false # 是否注册自己的信息到EurekaServer,默认是true
    fetch-registry: false # 是否拉取其它服务的信息,默认是true
    service-url: # EurekaServer的地址,现在是自己的地址,如果是集群,需要加上其它Server的地址。
      defaultZone: http://127.0.0.1:${server.port}/eureka

Run the project: visit http://127.0.0.1:8081

In-depth analysis of springClouds Eureka practice

At this time, an Eureka registration center is successfully built.

The above is the detailed content of In-depth analysis of springCloud's Eureka practice. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn