"Spring Cloud" Note 4: Registry Center - Eureka Component

This article has participated in the "Newcomer Creation Ceremony" event to start the road of gold creation together.

The learning and related materials of all courses are from the dark horse programmers of station b. Thanks to the dark horse programmers for their selfless help to our newbies, thank you! ! ! Dark Horse Programmer-----yyds

@ [toc]

1. Service consumers and service providers

Before we start, we need to understand two relatively simple concepts. Suppose there are three services, namely service A, service B, and service C.

1. We say that services that provide interfaces for other services are called service providers. 2. Services that use other service interfaces are called service consumers. 3. Whether a service is a consumer or a provider is relative to another service.

Take a simple example

There is now a chain of services: service A calls service B, and service B calls service C. Then it can be clearly concluded that

  • Service A is the service consumer
  • Service C is the service provider
  • Service B is a service provider relative to service A, and service B is a service consumer relative to service C

insert image description here

2. Introduction and principle of Eureka

2.1. Problems caused by remote invocation of services

insert image description hereRemember what I said in the last note, it can be seen that all URL mappings are implemented by hard coding, that is to say, each URL needs to be written in the code by the programmer, and the service is less acceptable, but if the service is Reaching a terrifying cluster environment, relying on manpower alone is already a very large workload, and it is not easy to maintain, which is also one of the problems caused by remote calls.

And hard coding will also lead to another one. We said that a microservice does not necessarily have only one instance, just like a class cannot have only one instance, then some people may ask, isn't an instance very good, the whole Isn't that more troublesome to maintain?

But please think about it, what if this service instance hangs, doesn't it mean that this service cannot provide service interfaces for other services, and then let the programmers issue update announcements and wait for the dead of night to repair the server?

Don't be kidding, isn't this more tiring, and the user experience is extremely bad. And multiple instances can prevent such emergencies for us. If instance 1 of service A hangs, but we still have instance 2, we can still continue. At this time, we can repair the instance whenever we want. Moreover, multiple instances can also meet the conditions of load balancing, preventing an instance from being subjected to excessive concurrent requests and improving the response speed.

Back to our topic, what does this have to do with hardcoding. Let's take a look at the code we wrote

insert image description here

你会发现我们把端口写死了,如果8081端口的实例挂了怎么办,我们又要去修改代码?我们又怎么做到多实例端口的读取呢?我们又要怎么监控知道哪个端口实例挂了?我们要怎么做到实例切换和负载均衡呢?

insert image description here

insert image description here

所有的这些问题,SpringCloud为我们提供了一个组件——Eureka,我们习惯地称它为注册中心

2.2、Eureka的原理

1、首先我们要明白,Eureka是分为两种环境的,也就是Eureka-server(服务端),和      Eureka-client(客户端)

2、Eureka-server的作用:记录和管理这些服务及其实例

insert image description here

在SpringCloud里面引入了Eureka组件后,当user-service的每一个实例启动的时候,就会做一件事情:将自己的信息注册到Eureka-server上,Eureka记录名字和相关IP和端口

当然Eureka-server也会记录服务消费者的每一个实例信息,因为只要是微服务,都有可能在未来成为服务提供者。

insert image description here

那么这个时候,相对于服务消费者而言,现在我要获取服务提供者的相关IP和端口信息,那么这个时候,服务消费者就不会直接去找服务提供者索要,而是将请求信息发送给Eureka-server,问问它有没有我所需要的服务信息。如果Eureka-server上存在相关的服务实例信息,则将服务状态健康的发送给服务消费者。

insert image description here

那如果服务消费者发现Eureka-server给它的实例有多个,那它该挑哪一个进行访问呢?这个时候就需要用到负载均衡的原理来进行访问(轮询?随机?...

insert image description here

那可能这个时候又有人问了,我访问的服务实会不会是已经挂掉的? 其实并不会,因为对于每个属于Eureka-client的微服务实例而言,每一个实例都会隔一定的周期向Eureka-server发送心跳包(一般是30秒),以确保我自己还活着。

insert image description here 对于哪些已经挂掉的服务实例,那么Eureka就会将它从列表中剔除,这个时候服务消费者再次向Eureka-server拉取服务实例列表的时候,就不会获取到哪些已经挂掉的服务实例了。

最后,我们将上面遗留的问题解决一下:

insert image description here

三、Eureka服务环境搭建

在原有的大项目上再创建一个module项目,这个项目名字就叫eureka-server

insert image description here insert image description here

找到我们新创建的eureka-server项目,点击里面的pom.xml 我们引入eureka-server的相关maven依赖

insert image description here

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
</dependencies>
复制代码

第一次下载可能有点慢,耐心稍等一下。

创建相关包、文件和类

insert image description here

在==EurekaApplication.java==文件下写入如下代码,作为启动类

package cn.itcast.eureka;

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

// 添加 @EnableEurekaServer 作为Eureka-server的开关
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class,args);
    }
}

复制代码

在==eureka-server==的==application.yml==文件内加入下面内容

server:
  port: 10086 # 服务的端口
spring:
  application:
    name: eurekaserver # 服务名称,也就是微服务的名称
  freemarker:
    prefer-file-system-access: false
eureka:
  client:
    fetch-registry: false # 是否从eureka-server上拉取服务信息
    register-with-eureka: false  # 是否将自己注册到eureka-server上,默认是true,为true是为了以后的eureka集群
    service-url:
      defaultZone: http://localhost:10086/eureka

复制代码

好,那现在就可以启动我们的eureka-server服务了。

insert image description here 启动的eureka-server服务成功

insert image description here

接着在浏览器里面输入:localhost:10086 回车,我们就可以看到eureka的主页面了。

insert image description here

我们可以看到注册到eureka的实例是空列表,这个时候我们可以试试将eureka-server自己配置上去

insert image description here

修改下面的结果为true后重新运行,刷新页面就可以看到结果了

insert image description here

insert image description here

四、Eureka服务注册

服务注册和上面的eureka的注册功能类似,我们可以尝试将user-service与order-service注册上去

user-service的注册

进入它的==pom.xml==文件里面,加入下面的依赖

insert image description here

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

进入它的==application.yml==文件,我们加入下面的信息

insert image description here

然后进入==UserApplication.java==文件

insert image description here

The same is true for the configuration of order-service, except that the name in the application.yml file in order-service needs to be changed to: orderservcie

Refresh the instance list of eureka and find that it has been registered

insert image description here

Five, Eureka service discovery

We want order-service to pull a list of all relevant instances based on user-service's application name.

We need to configure the content of user-service, which can run in multiple instances

insert image description here

insert image description here

insert image description here

insert image description here

Refresh the browser to see two instances

insert image description hereNow order-service needs to access user-services of multiple instances, so configure load balancing for order-service , as follows

insert image description here

Finally, we need to modify the IP and port we wrote to death, because now the whole process is taken over by eureka, we only need to write the service name registered in eureka, the following file

insert image description hereRerun the order-service project

Open a browser, now we revisit the URL that previously requested the order data

Let's try a few more

insert image description hereFrom 101 to 106, we see that the results are normal

In fact, looking at the access logs of these six times, it can be seen that each instance has been accessed three times, so it can be guessed that the default load balancing mode in springCloud is polling

What exactly needs to be looked at the source code later

The eureka notes of this issue are written here to encourage each other

It's not too much to give three consecutive, thank you~

Guess you like

Origin juejin.im/post/7084802999201038367