SpringCloud实战之路 | 应用篇(一)服务注册中心Eureka
文章内容输出来源:拉勾教育Java高薪训练营;
注册中心介绍
在分布式环境的场景下,通常每一个服务都是会部署多个实例,服务的提供者数量往往是会动态变化的,为了保证能够弹性的动态扩容缩容,静态的LB是不适用的,这时候就需要引入注册中心进行对服务的提供者和消费者进行注册发现。
作用: 主要就是用于对服务提供者与服务消费者的解耦。注册中心用于存储服务提供者的地址等相关信息,服务消费者通过主动查询或者被动通知从注册中心中获取到服务提供者的信息,而不再需要通过硬编码的方式获取提供者的地址信息
基本原理
消费者获取提供者的注册信息有两种模式
poll模式: 服务消费者主动从注册中心中拉取服务提供者的信息
push模式: 服务消费者以订阅的方式获取提供者的信息,当服务提供者有变化时,注册中心会制动推送给服务消费者
注册中心也需要完成服务提供者的健康监控,当发现服务提供者失效时需要及时剔除;
常见注册中心对比
注册中心 | 语言 | CAP | 对外暴露接口 |
---|---|---|---|
Eureka | java | AP | HTTP |
Consul | go | CP | HTTP/DNS |
Zookeeper | java | CP | 客户端 |
Nacos | java | 支持AP/CP切换 | HTTP |
Eureka实现原理
Eureka中包含两个组件:
- Eureka Server: 负责服务发现,服务启动时会通过Eureka Client向Eureka Server中注册自己的信息。(需要自己创建工程引入jar包)
- Eureka Client: 用于与Eureka Server的交互。(在已有的微服务中引入client端相关jar包,进行配置与Eureka Server产生联系)
1.服务提供者向Eureka Server中注册服务,Eureka Server接受到注册事件会在集群和分区中进行同步,消费端可以从Eureka Server中获取服务注册信息,进行服务调用。
2.服务启动后,会周期性向Eureka Server发送心跳(默认30s)完成续约
3.Eureka Server在一定时间内没有接收到某个微服务节点的心跳会注销该服务节点(默认90s)
4.Eureka Client会缓存Eureka Server中的信息,即使Eureka Server的节点都宕掉,服务消费者也可以从缓存中找到服务提供者
Eureka搭建
parent父pom中引入spring cloud依赖
<dependencyManagement>
<dependencies>
<!--spring cloud依赖管理,引入了Spring Cloud的版本-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
(一)构建Eureka Server服务
引入maven依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud-parent</artifactId>
<groupId>com.cloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-eureka-server-8761</artifactId>
<dependencies>
<!--Eureka server依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
创建入口启动类
package com.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer8761 {
public static void main(String[] args) {
SpringApplication.run(EurekaServer8761.class,args);
}
}
创建配置文件yml
#eureka server服务端口
server:
port: 8761
spring:
application:
name: cloud-eureka-server # 应用名称,应用名称会在Eureka中作为服务名称
访问 http://localhost:8761/
(二)服务提供者引入Eureka Client
每个微服务引入maven依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud-parent</artifactId>
<groupId>com.cloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-service-user</artifactId>
<dependencies>
<!--eureka client 客户端依赖引入-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
修改微服务application.yml配置⽂件,
server:
port: 8080
#注册到Eureka服务中心
eureka:
client:
service-url:
# 注册到集群,就把多个Eurekaserver地址使用逗号连接起来即可;注册到单实例(非集群模式),那就写一个就ok
defaultZone: http://localhost:8761/eureka
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
spring:
application:
name: cloud-service-user
datasource:
jpa:
database: MySQL
show-sql: true
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl #避免将驼峰命名转换为下划线命名
management:
endpoints:
web:
exposure:
include: "*"
# 暴露健康接口的细节
endpoint:
health:
show-details: always
刷新Eureka地址:http://localhost:8761/
至此完成服务提供者注册到Eureka
Eureka实现高可用
Eureka创建集群只需要将eureka也作为服务注册到自身即可
1.修改本机host
由于是在个人计算机测试,很难模拟多主机情况,Eureka配置server集群时候需要执行host地址,所以需要修改个人电脑的host地址
127.0.0.1 EurekaServerA
127.0.0.1 EurekaServerB
2.修改eureka server配置
#eureka server服务端口
server:
port: 8761
spring:
application:
name: cloud-eureka-server # 应用名称,应用名称会在Eureka中作为服务名称
# eureka 客户端配置(和Server交互),Eureka Server 其实也是一个Client
eureka:
instance:
hostname: EurekaServerA # 当前eureka实例的主机名
client:
service-url:
# 配置客户端所交互的Eureka Server的地址(Eureka Server集群中每一个Server其实相对于其它Server来说都是Client)
# 集群模式下,defaultZone应该指向其它Eureka Server,如果有更多其它Server实例,逗号拼接即可
defaultZone: http://EurekaServerB:8762/eureka
register-with-eureka: true # 集群模式下可以改成true
fetch-registry: true # 集群模式下可以改成true
dashboard:
enabled: true
修改client端配置
server:
port: 8080
#注册到Eureka服务中心
eureka:
client:
service-url:
# 注册到集群,就把多个Eurekaserver地址使用逗号连接起来即可;注册到单实例(非集群模式),那就写一个就ok
defaultZone: http://EurekaServerA:8761/eureka,http://EurekaServerB:8762/eureka
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
spring:
application:
name: cloud-service-user
datasource:
jpa:
database: MySQL
show-sql: true
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl #避免将驼峰命名转换为下划线命名
management:
endpoints:
web:
exposure:
include: "*"
# 暴露健康接口的细节
endpoint:
health:
show-details: always