Eureka是帮助我们维护所有服务的信息,以便服务之间的相互调用
1 Eureka的快速入门
1.1、创建一个父工程
并且在父工程中指定SpringCloud的版本,并且将packaging修改为pom。
<packaging>pom</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
1.2、创建Eureka的server,
创建springboot工程,并且导入依赖,在启动类中之间添加,编写yml配置文件。
(1) 导入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
(2)启动类添加注解
@SpringBootApplication
@EnableEurekaServer //Eureka的注解
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class,args);
}
}
(3) 编写application.yml配置文件
server:
port: 8761 #指定服务的端口号
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false #当前的Eureka服务为单机版
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
1.3 创建EurekaClient
1、创建Maven工程,修改为Springboot
2、导入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
3、在启动类上添加注解
@SpringBootApplication
@EnableEurekaClient
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class,args);
}
}
4、编写配置文件
#指定Eureka服务地址
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
#指定服务的名称
spring:
application:
name: CUSTOMER
2 Eureka的安全性
实现Eureka认证
1、导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2、编写配置类,过滤路径
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//忽略掉路径eureka/**
http.csrf().ignoringAntMatchers("/eureka/**");
super.configure(http);
}
}
3、编写配置文件,添加用户名和密码
#指定用户名和密码
security:
user:
name: root
password: root
4、其他服务注册到Eureka上,需要添加用户名和密码
#指定Eureka服务地址
eureka:
client:
service-url:
defaultZone: http://用户名:密码@localhost:8761/eureka
3 Eureka的高可用
如果程序正在运行,突然Eureka宕机,会发生的情况:
(1)如果调用方已经访问过一次被调用方,Eureka宕机不会影响功能
(2)如果没用访问过,则Eureka的宕机会造成当前功能的不可用
搭建Eureka的高可用
1、准备多台Eureka服务器
2、让服务注册到多台Eureka中
- 修改配置文件,用逗号隔开多个Eureka服务端的地址
3、让多台Eureka之间相互通讯
eureka:
instance:
hostname: localhost
client:
registerWithEureka: true #注册到Eureka上
fetchRegistry: true #从Eureka拉取信息
serviceUrl:
#填写另一个Eureka服务的地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
4、Eureka的细节
1、EurekaClient启动时,将自己的信息注册到EurekaServer上,EurekaServer中就会存储EurekaClient的注册信息。
2、当EurekaClient需要调用服务时,本地没有注册信息的缓存时,会去EurekaServer中获取注册信息。
3、EurekaClient会通过一个心跳的方式,和EurekaServer进行连接,默认30秒,EurekaClient会发送一次心跳请求,如果超过九十秒还没有发送,EurekaServer就认为该EurekaClient已经宕机了,会将该EurekaClient的信息从注册表中移除。
eureka:
instance:
hostname: localhost
#指定EurekaClient心跳请求的时间
lease-renewal-interval-in-seconds: 30 #心跳的间隔
lease-expiration-duration-in-seconds: 90 #多久没法送,就认为宕机
4、EurekaClient会每隔30秒会去EurekaServer取更新本地的注册表。
eureka:
client:
registry-fetch-interval-seconds: 30 #更新注册表的时间
5、Eureka的自我保护机制。统计15分钟内,一个服务的心跳发送比例低于85%,EurekaServer就会开启自我保护机制。
- 不会从EurekaServer中移除长时间没有收到心跳的服务。
- EurekaServer还是可以正常提供服务的。
- 网络比较稳定时,EurekaServer才会开始将自己的信息同步到其他节点
eureka:
server:
enable-self-preservation: true #开启自我保护机制
6、CAP定理。C代表一致性,A代表可用性,P代表容错性。在分布式的环境下,只能满足两个。而且分区性和容错性在分布式环境下,是必须要满足的,因此只能再AC之间权衡。
- 如果选择了CP,保证了一致性,但是可能会造成系统在一定时间内是不可用的。如果同步数据的时间比较长,可能会造成比较大的损失。
- Eureka是一个AP的效果,高可用的集群,Eureka集群是无中心的,因此EurekaServer服务宕机了几个,也不会影响系统的使用。不需要重新取推举一个master。但是这也会导致在一定时间内,数据可能不是一致的。