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
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
Deploy Eureka in the project
创建新的springboot项目,引入依赖
1-Eureka dependency
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2. ed
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
3. Main program
@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
service provider service provider (the business server is registered with Eureka)
把微服务(业务服务器) 注册到 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
2-application.yml add eureka registration configuration
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
心跳间隔时间,默认 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
High availability of eureka and "service providers"
Item-service high availability (provider, business server)
Start parameter --server.port
can override the port configuration yml
1-Configure startup parameters
The provider's port number is 8001
--server.port=8001
2-Start the test
访问 eureka 查看 item-service 注册信息
Simultaneously start Eureka
- Visit two ports to test
http://localhost:8001/35
http://localhost:8002/35
正常运行那么说明成功
eureka high availability
创建两个Eureka服务器的 profile 配置文件
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.active
and--server.port
--spring.profiles.active=eureka1 --server.port=2001
--spring.profiles.active=eureka2 --server.port=2002
启动两台Eureka服务器
Visit eureka server to view registration information
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