"Eureka Registry"

What is a registry

The distributed service framework is deployed on multiple different machines, and different machines are registered in their own state in the registry.
When the service starts, it will link to the registry and write the service data (service name|IP|port) into the registry
After the registration center receives the user service data, it dynamically maintains the service list.
When a user calls a service consumer, the consumer performs load balancing based on the information in the current service list, and selects one of the services for access.
In order to ensure the correctness of the service list, the registry uses a heartbeat detection mechanism to monitor all service producers in real time

Insert picture description here


Eureka Registry

What is the Eureka registry

Spring Cloud integrates it in its sub-project Spring Cloud Netflix to realize Spring Cloud service registration and discovery, and also provides E capabilities such as load balancing and failover

Eureka's operating process

Insert picture description here


Deploy Eureka in the project

创建新的springboot项目,引入依赖

1-Eureka dependency

  • xml dependency

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>
  • springboot visual dependency

Insert picture description here


2. ed

Insert picture description here

spring:
  application:
    name: eureka-server
    
server:
  port: 2001
  
eureka:
  server:
    enable-self-preservation: false
  instance:
    hostname: eureka1
  client:
    register-with-eureka: false
    fetch-registry: false

  • hostname —— Eureka cluster servers are distinguished by hostname

  • enable-self-preservation-eureka's self-preservation status

The percentage of heartbeat failures, whether it exceeds 85% within 15 minutes, if it exceeds the situation, Eureka Server will protect the current instance registration information and prompt a warning. Once it enters the protection mode, Eureka Server will try to protect it. The information in the service registry will no longer delete the data in the service registry. That is, no microservices will be logged out

  • register-with-eureka-do not register with itself


3. Main program

Insert picture description here

@EnableEurekaServer
@SpringBootApplication
public class Sp05EurekaApplication {
    
    

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

}

添加 @EnableEurekaServer 注解

The @EnableEurekaServer annotation indicates the current startup class, which belongs to the registry of eureka

Modify the host file and add the eureka domain name

127.0.0.1       eureka1
127.0.0.1       eureka2

4. Start and access the test

Address: http://eureka1:2001

The URL appears to indicate success

Insert picture description here


service provider service provider (the business server is registered with Eureka)

Insert picture description here

把微服务(业务服务器) 注册到 eureka 服务器

Steps:

  • pom.xml add eureka dependency
  • application.yml add eureka registration configuration
  • The main program enables the eureka client
  • Start the service and view the registration information in eureka

1-pom.xml add eureka client dependency

xml dependency

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

Visual dependency

在pom文件中 Alt+Ins

Insert picture description here
Insert picture description here


2-application.yml add eureka registration configuration

eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka

Insert picture description here

心跳间隔时间,默认 30 秒

拉取注册信息间隔时间,默认 30 秒

  • defaultZone —— The default location, which can be modified to a specific geographic location, such as: beiJing, shangHai, shenZhen, etc., indicating the deployment location of the eureka server, which needs to be provided by the cloud server

3-The main program enables the service registration to discover the client

修改 item-service(业务服务器)的主程序

@EnableDiscoveryClient
@SpringBootApplication
public class Sp02ItemserviceApplication {
    
    

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

}


4-Start, and visit eureka to view registration information

Insert picture description here


High availability of eureka and "service providers"

Insert picture description here

Item-service high availability (provider, business server)

Start parameter --server.portcan override the port configuration yml

1-Configure startup parameters

The provider's port number is 8001

--server.port=8001

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

2-Start the test

访问 eureka 查看 item-service 注册信息

Simultaneously start Eureka
Insert picture description here

Insert picture description here

  • Visit two ports to test
    http://localhost:8001/35
    http://localhost:8002/35

正常运行那么说明成功


eureka high availability

创建两个Eureka服务器的 profile 配置文件

Insert picture description here

application-eureka1.yml

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    #1 链接 2
    service-url:
      defaultZone: http://eureka2:2002/eureka
  instance:
    hostname: eureka1

application-eureka2.yml

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    #2 链接 1
    service-url:
      defaultZone: http://eureka1:2001/eureka
  instance:
    hostname: eureka2

Start configuration parameters --spring.profiles.activeand--server.port

  • eureka1 startup parameters:
--spring.profiles.active=eureka1 --server.port=2001
  • eureka2 startup parameters:
--spring.profiles.active=eureka2 --server.port=2002

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

启动两台Eureka服务器

Insert picture description here


Visit eureka server to view registration information

Insert picture description here

Insert picture description here
Insert picture description here


When the provider (business server) registers, register with two Eureka servers at the same time

eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka

Insert picture description here

当一个 eureka 服务宕机时,仍可以连接另一个

Guess you like

Origin blog.csdn.net/weixin_45103228/article/details/114033476