Spring Cloud Eureka service registration and service discovery super detailed (additional - source code implementation case - and implementation logic diagram)

Eureka

This article first describes the application scenarios and code implementation cases of Eureka. Multiple service modules are registered in Euraka, and the call implementation between services will be explained in the next article!

What functions can Eureka components achieve

Eureka mainly does:

  1. 注册发现中心
  2. 服务注册与发现的组件

What is the CAP principle?

When it comes to Eureka, CAP has to be mentioned, so what is the CAP principle, let's take a look together!
CAP 原则:Also known as the CAP theorem, it refers to in a distributed system.

Three characteristics of the CAP principle :
一致性 (Consistency):In a cluster, the data of the three machines are consistent.
可用性I(Availability):When a node hangs up, the entire cluster can continue to provide external services.
分区容错性 (Partition tolerance):The data in each machine may be temporarily inconsistent due to reasons such as the computer room network or partition.
(This feature is inevitable) The CAP principle means that these three elements can only achieve at most two points at the same time, and it is impossible to take care of all three.

Having said that, let me mention the difference between Eurekaand Zookeeper?
Zookeeper:
Follow the CP principle
Eureka:
AP principle: pay attention to AP high availability

Service registration code practice

Overall core implementation diagram
insert image description here

Build a registration center

Next, let me take a detailed look at how to quickly realize the use of Eureka through the code.
insert image description here
You only need to build the project I marked in the red box. registration center

First of all, build the directory structure. The Maven project
注意:新增项目的时候选择Java8
is built according to the one on my picture. I won’t go into the new project process here one by one. If you don’t understand, you can private message me!
The first step
is to create a new registration center. 01-eureka-server
insert image description here
The second step
is to modify EurekaServerApplicationthe startup class and add @EnableEurekaServerannotations, which means that to open the Eureka registration center, only one local 添加开启Eureka注解function needs to be modified in the startup class.
The source code is as follows:

package com.powernode;

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

@SpringBootApplication
@EnableEurekaServer //开启Eureka的注册中心的功能
public class EurekaServerApplication {
    
    

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

}

The third step
is to modify
the source code of the yml file (configuration file) as follows:
I have added comments one by one for what each configuration means, and first modify it in my way.

#单机
server:
    port: 8761 # eureka的默认端口
spring:
    application:
      name: eureka-server # 应用名称 不要使用特殊字符
eureka: #Eureka的配置分三类 server client 实例的  eureka-server既是服务端又是客户端
    server: #服务端每间隔多少毫秒定期删除的操作(默认是3000030秒))
      eviction-interval-timer-in-ms: 10000
      renewal-percent-threshold: 0.85 # 续约百分比 超过85的应用没有和你续约 那么erueka不会踢除任何应用
    instance: # 实例的配置
      instance-id: ${
    
    eureka.instance.hostname}:${
    
    spring.application.name}:${
    
    server.port}} # 主机名称 : 应用名称 : 端口号
#      hostname: localhost # 主机名称 或者 服务的IP
#      prefer-ip-address: true #以IP的形式显示具体的服务信息
#      lease-renewal-interval-in-seconds: 5 # 服务实例的续约的时间间隔

The fourth step is
to modify the pom file , because this piece needs to specify the spring Cloud and spring Boot versions. Modify according to the box I marked in red
注意:01-eureka-server的pom文件

insert image description here
insert image description here

Service A Build

The project name corresponding to service A is: 02-eureka-client-a, don't change the wrong place!
The first step
is to modify the yml configuration file first.
The source code is as follows:
I have added comments one by one for what each configuration means, and first change it in my way.

server:
  port: 8702 # 客户端端口没有要求
spring:
  application:
    name: eureka-client-a
# 注册 发送信息
eureka:
  client:
    service-url: # 指定注册地址
      defaultZone: http://localhost:8761/eureka
    register-with-eureka: true # 可以不往eureka-server注册
    fetch-registry: true # 应用是否去拉取服务列表到本地
    # 每个10秒中去注册中心重新进行拉取 时间越短脏毒越少 性能消耗大
    registry-fetch-interval-seconds: 10 # 为了缓解服务列表的脏毒问题
  instance:
    hostname: localhost # 应用的主机名称 最好谢主机ip
    instance-id: ${
    
    eureka.instance.hostname}:${
    
    spring.application.name}:${
    
    server.port}
    prefer-ip-address: true # 显示ip
    lease-renewal-interval-in-seconds: 10 # 实例续约的时间

The second step
is to modify the pom file and modify it according to the figure
1
insert image description here

Service B Build

The project name corresponding to service A is: 02-eureka-client-b, don't change the wrong place!
Similar to service A,
the first step
is to modify the yml configuration file first.
The source code is as follows:
I have added comments one by one for what each configuration means, and first change it in my way.

server:
  port: 8703 # 客户端端口没有要求
spring:
  application:
    name: eureka-client-b
# 注册 发送信息
eureka:
  client:
    service-url: # 指定注册地址
      defaultZone: http://localhost:8761/eureka

The second step
is to modify the pom file and modify it according to the figure
insert image description here
insert image description here

start service

Start the registry

Start the registration center first 01-eureka-server, and 服用A和服务Bstart
the startup method: click the startup file, then click the green triangle to select the running mode or debugging mode, and you can
insert image description here
see the picture below, indicating that the startup has been successful, and the return status code is 204.
注意:重点来了非常重点, directly enter in the address bar of the browserhttp://localhost:8761/

insert image description here
If you http://localhost:8761/return to this page after typing, 恭喜你Eureka注册中心启动成功!then go to start the service.
insert image description here

Start service A

This is still the way to start, both in run mode and debug mode.
insert image description here
After the startup is successful, it will be displayed as shown in the figure below, and the status display is 204.
insert image description here
注意:重点来了非常重点, directly enter in the address bar of the browser http://localhost:8761/, or you have opened this address just now, directly press F5 to refresh the page. It can be clearly seen that there is a service with an application name of:
above the registration center, which is registered. Very good, continue to start service B.EUREKA-CLIENT-A
insert image description here

start service B

Service B is still started in the same way as service A. We can clearly see that there are currently 三个服务正在运行registration centers, service A and service B. Go back to the browser, still under the same URL, and press F5 to refresh.
insert image description here
很好, you can see that service B is also registered.

insert image description here

conclusion

So far, the realization of the registration function of a registration center and two service modules has been completed. According to my method to build projects and code implementation, bloggers who do not understand private messages, you can see that I have other components. This
is Commonly used Spring Cloud组件, there are also the services of each component in the second picture, nacos file configuration center construction, handwritten gateway, fuse, etc., are all source codes, bloggers in need can private message me, no charge, if you have If you don't understand, I will answer for you for free, I hope it can help everyone!
This is the end of the sharing in this issue, and the next issue will share the rabbit to realize remote calling.
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/xiaohua616/article/details/132000558